forked from houko/fastTwitter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
unfollow.py
68 lines (55 loc) · 1.71 KB
/
unfollow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#! /usr/bin/python3
# coding=utf-8
"""
把今天最好的表现当作明天最新的起点..~
いま 最高の表現 として 明日最新の始発..~
Today the best performance as tomorrow newest starter!
Created by IntelliJ IDEA.
author: xiaomo
github: https://github.com/syoubaku
email: [email protected]
QQ_NO: 83387856
Date: 2018/1/27 10:25
Description: 取消关注(此操作请慎用)
Copyright(©) 2017 by xiaomo.
"""
import logging.config
from tweepy import TweepError
from app_config import log_config_url, unfollow_url
from tweepy_api import get_api
logging.config.fileConfig(log_config_url)
# create logger
logger = logging.getLogger(__name__)
# 主函数
def main():
api = get_api()
me = api.me()
uid_list = api.friends_ids(me.screen_name)
for uid in uid_list:
friendship = unfollow(api, uid)
if friendship:
logger.info("unfollow %s" % friendship.screen_name)
bak_following(friendship)
else:
logger.warning('%s unfollow fail...' % friendship.screen_name)
# 取消关注的实际操作
def unfollow(api, uid):
try:
friend = api.get_user(uid)
friendship = api.destroy_friendship(friend.screen_name)
if friendship:
logger.info("unfollow %s" % friendship.screen_name)
return friendship
except TweepError as e:
logger.error("error: %s" % e)
return None
# 备份被取消的关注者,可以使用批量关注再加回来
def bak_following(user):
with open(unfollow_url, 'a+', encoding="utf-8") as f:
f.write(user.screen_name)
f.write(" ")
f.write(user.screen_name)
f.write("\n")
# 入口
if __name__ == '__main__':
main()