This repository has been archived by the owner on May 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_fav.py
174 lines (133 loc) · 4.5 KB
/
auto_fav.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import sys
import time
from pathlib import Path
import tweepy
from loguru import logger
def read_secret(path_secret="secret"):
# TODO: change de secret format
with open(path_secret) as f:
secrets = [i.strip()[1:-1] for i in f.readlines()]
CONSUMER_KEY = secrets[1]
CONSUMER_SECRET = secrets[3]
ACCESS_TOKEN = secrets[6]
ACCESS_TOKEN_SECRET = secrets[8]
return CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET
def create_api(path_secret):
CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET = read_secret(
path_secret
)
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)
api = tweepy.API(
auth,
compression=True,
retry_errors=False,
wait_on_rate_limit=True,
wait_on_rate_limit_notify=True,
)
return api
def get_jsons(result):
return [i._json for i in result]
def save_knows_users_ids(path_know_users_ids):
"""
Save to a csv file 'know_users_ids'.
path_know_users_ids: pathlib.Path
Path to the file.
"""
with path_know_users_ids.open("w") as f:
for user_id in know_users_ids:
f.write(f"{user_id}\n")
def search_knows_users_ids(path_know_users_ids):
"""
If the file exists, read the file. Else recreate the file
from the last favs and all the followers.
path_know_users_ids: pathlib.Path
Path to the file.
"""
if path_know_users_ids.is_file():
with path_know_users_ids.open() as f:
know_users_ids = set(line.strip() for line in f.readlines())
else:
know_users_ids = set(
result._json["user"]["id"]
for result in tweepy.Cursor(api.favorites).items()
)
me = api.me()._json
me_followers_ids = api.followers_ids(me["id"])
know_users_ids.update(set(me_followers_ids))
# the bot id
know_users_ids.update(set(["1264287566284705794"]))
return know_users_ids
def filter_result(result, know_users_ids):
"""Util to filter tweets in the search."""
result = result._json
if (
"retweeted_status" in result
or result["user"]["id_str"] in know_users_ids
or result["favorited"]
):
return False
else:
return True
def reduce_result(result):
"""Util to extract the useful info from the tweet."""
result = result._json
new_result = {}
new_result["id"] = result["id"]
new_result["user"] = {}
new_result["user"]["id_str"] = result["user"]["id_str"]
new_result["user"]["screen_name"] = result["user"]["screen_name"]
return new_result
def get_result():
know_users_ids = search_knows_users_ids(path_know_users_ids)
last_favs = get_jsons(api.favorites())
if last_favs:
since_id = last_favs[0]["id"]
else:
since_id = None
# We only a bit of information, not all the tweet.
search_results = [
reduce_result(result)
for result in tweepy.Cursor(
api.search, q=q, since_id=since_id, count=100
).items()
if filter_result(result, know_users_ids)
]
return search_results
path = Path(sys.argv[0]).parent
path_know_users_ids = path / "know_users_ids.csv"
logger.add(
path / "auto_fav.log",
format="{time:YYYY:MM:DD HH:mm:ss} - {level} - {message}",
rotation="5 MB",
retention=5,
)
api = create_api(path_secret=path / "secrets")
q = "(mafalda AND quino) OR (#mafalda -#quino) OR (-#mafalda #quino)"
search_results = get_result()
logger.info(f"Find {len(search_results)} new tweets to fav")
new_favs = 0
skipped_favs = 0
new_users_ids = set()
try:
for result in reversed(search_results):
user_id = result["user"]["id_str"]
if user_id in new_users_ids:
logger.info(f"This user {user_id} have a tweet with a fav alredy, skip.")
skipped_favs += 1
continue
api.create_favorite(result["id"])
new_users_ids.update([user_id])
logger.info(
f"New fav: user id {user_id} screen_name {result['user']['screen_name']}, tweet_id {result['id']}"
)
new_favs += 1
# tenemos mil en una ventana de 24 horas
# aprox uno cada 90 seg
# (60*60*24) / 1000 ~= 86.4
time.sleep(90)
except Exception as error:
logger.error(f"Error: {str(error)}")
know_users_ids.update(new_users_ids)
save_knows_users_ids(path_know_users_ids)
logger.info(f"Total favs: {new_favs}. Total skipped: {skipped_favs}")