Skip to content

Instantly share code, notes, and snippets.

@Ishibasystems
Created July 19, 2016 14:39
Show Gist options
  • Save Ishibasystems/d02dba7953b781b7686a434d0822503b to your computer and use it in GitHub Desktop.
Save Ishibasystems/d02dba7953b781b7686a434d0822503b to your computer and use it in GitHub Desktop.
#!/usr/local/bin/python3
# -*- coding: utf-8 -*-
import tweepy
from sys import exit
from time import sleep
from os.path import exists
from modules import pushbullet, text, machine
from traceback import format_exc
from itertools import zip_longest
conf = machine.set()
base = conf['machine']['basedir']
list_log = base + 'tweepy-peacemaker.log'
section = 'pushbullet'
acs = (conf[section]['acs'], conf['machine'][section])
acc_name = 'Ishibasystems'
CS_key = conf[acc_name]['app_key']
CS_sec = conf[acc_name]['app_sec']
AC_key = conf[acc_name]['usr_key']
AC_sec = conf[acc_name]['usr_sec']
def main():
auth = tweepy.OAuthHandler(CS_key, CS_sec)
auth.set_access_token(AC_key, AC_sec)
api = tweepy.API(auth_handler = auth)
# ログから確認
note = text.read(list_log)
users_log_p = set()
users_log_m = set()
users_log_c = set()
users_log_k = set()
names = dict()
for line in note:
if '\t' not in line:
continue
m = line.split('\t')[0]
n = m[1:]
l = m[0:1]
names[n] = line.split('\t')[1]
if l == '*': users_log_k.add(n) #レコーダー
if l == '#': users_log_c.add(n) #サスペンド
if l == '|': users_log_p.add(n) #フォロー
if l == '-': users_log_m.add(n) #フォロワー
if l == '+':
users_log_p.add(n) #相互
users_log_m.add(n)
# tweepyから確認
users_p = set()
users_m = set()
try:
for friendids in tweepy.Cursor(api.friends_ids, count = 5000).pages():
sleep(5)
users_p.update(list(map(str, friendids)))
for followerids in tweepy.Cursor(api.followers_ids, count = 5000).pages():
sleep(5)
users_m.update(list(map(str, followerids)))
except:
print(format_exc() + "API.friends_ids or API.followers_ids Error!")
exit(1)
# 変化を確認
user_all = users_log_m.union(users_m).union(users_log_p).union(users_p)
friend = set()
follow = set()
refollow = set()
unfollow = set()
suspend = set()
unsuspend = set()
block = set()
for user in user_all:
# フォロー減 / フォロワー減
if user in users_log_p.difference(users_p) or user in users_log_m.difference(users_m):
# アンフォロー・ブロック・アカウント凍結・アカウント削除(→アカウントにアクセスできるか確認)
users_log_c.add(user)
suspend.add(user)
# フォロー増 / フォロワー増
if user in users_p.difference(users_log_p) or user in users_m.difference(users_log_m):
if user in users_log_c:
# アカウント復活
users_log_c.difference_update([user])
unsuspend.add(user)
try: api.update_status('@' + names[user].split('@')[-1] + ' おかえりなさいませ')
except: print(format_exc() + "API.update_status Error!: " + user)
else:
# フォロワー増
if user in users_m.difference(users_log_m):
if user in users_log_k:
# リフォローされました
refollow.add(user)
else:
# フォローされました(→ユーザー名を確認)
follow.add(user)
'''
if user not in users_p:
try:
api.create_friendship(user) # フォロー返し
users_p.add(user)
sleep(5)
except:
print(format_exc() + "API.create_friendship Error!: " + user)
'''
# フォロー増
if user in users_p.difference(users_log_p):
if user not in users_m.difference(users_log_m):
# 自発フォロー(→ユーザー名を確認)
friend.add(user)
# ユーザー情報の確認(API Rate Limitsを抑えるためSuspend、Follow、Friend一気に)
for users_list in zip_longest(*[iter(suspend.union(follow).union(friend))]*100):
try:
users = api.lookup_users(user_ids=users_list)
sleep(5)
except tweepy.error.TweepError as e:
if e.api_code != 17 or len(follow) != 0 or len(friend) != 0:
# No user matches for specified terms.ではない
# もしくは 確認したユーザーが全てSuspendでないなら
print(format_exc() + "API.lookup_users Error!")
continue
for user in users:
if user.id_str in follow.union(friend):
# ユーザー名を確認
names[user.id_str] = user.name+'@'+user.screen_name
if user.id_str in suspend:
# アカウントにアクセスできる(アンフォロー・ブロック)
users_log_c.difference_update([user.id_str])
suspend.difference_update([user.id_str])
if user.id_str in users_log_p.difference(users_p):
# ブロックされました
block.add(user.id_str)
'''
try:
api.destroy_block(user.id_str) # 報復ブロック
sleep(5)
except:
print(format_exc() + "API.destroy_block Error!: " + names[user.id_str])
'''
else:
# アンフォローされました
unfollow.add(user.id_str)
if user.id_str in users_p:
try:
api.destroy_friendship(user.id_str) # リムーブ返し
users_p.difference_update([user.id_str])
sleep(5)
except:
print(format_exc() + "API.destroy_friendship Error!: " + names[user.id_str])
for user in suspend:
#api.update_status('@' + names[user].split('@')[-1] + ' 大丈夫ですか? #お前らしくないツイートをしろ')
pass
# ログに記録
note = list(map(int, names.keys()))
note.sort()
for m in range(len(note)):
n = str(note[m])
if n in users_p and n in users_m: l = '+'
elif n in users_p: l = '|'
elif n in users_m: l = '-'
elif n in users_log_c: l = '#'
else: l = '*'
note[m] = l + n + '\t' + names[n]
text.write(list_log, '\n'.join(note) + '\n')
# pushbulletに報告
if len(follow ): pushbullet.note(acs, 'Follow', '\n'.join([names[m] for m in follow ]))
if len(refollow ): pushbullet.note(acs, 'Refollow', '\n'.join([names[m] for m in refollow ]))
if len(unfollow ): pushbullet.note(acs, 'Unfollow', '\n'.join([names[m] for m in unfollow ]))
if len(suspend ): pushbullet.note(acs, 'Suspend', '\n'.join([names[m] for m in suspend ]))
if len(unsuspend): pushbullet.note(acs, 'Unsuspend', '\n'.join([names[m] for m in unsuspend]))
if len(block ): pushbullet.note(acs, 'Block', '\n'.join([names[m] for m in block ]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment