Skip to content
This repository was archived by the owner on Nov 2, 2025. It is now read-only.

Commit 46d06ba

Browse files
authored
Merge pull request #35 from edoardottt/devel
Devel
2 parents 19708de + 4089742 commit 46d06ba

File tree

4 files changed

+65
-32
lines changed

4 files changed

+65
-32
lines changed

errors.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,41 @@
1414

1515
import time
1616
import logging
17+
import twitter
18+
import urllib
1719

1820

1921
def error_handler(e):
2022
"""
21-
This function handles all the TwitterHTTPError errors
23+
This function handles all the twitterbot2 errors
24+
- twitter.api.TwitterHTTPError
25+
- urllib.error.URLError
26+
- generic
2227
"""
28+
2329
logger = logging.getLogger("__main__")
24-
logger.error(str(e.e) + " on " + e.uri)
30+
if e.__class__ == twitter.api.TwitterHTTPError:
31+
logger.error(str(e.e) + " on " + e.uri)
2532

26-
# == 429 TOO MANY REQUESTS -> Sleep for one hour
27-
if str(e.e.code) == "429":
28-
logger.info("Sleeping for one hour.")
29-
time.sleep(60 * 60)
30-
return
33+
# == 429 TOO MANY REQUESTS -> Sleep for one hour
34+
if str(e.e.code) == "429":
35+
logger.info("Sleeping for one hour.")
36+
time.sleep(60 * 60)
37+
return
3138

32-
# == 403 FORBIDDEN -> Sleep for ten seconds
33-
if str(e.e.code) == "403":
34-
logger.info("Sleeping for ten seconds.")
35-
time.sleep(10)
36-
return
39+
# == 403 FORBIDDEN -> Sleep for ten seconds
40+
if str(e.e.code) == "403":
41+
logger.info("Sleeping for ten seconds.")
42+
time.sleep(10)
43+
return
44+
elif e.__class__ == urllib.error.URLError:
45+
logger.error(str(e.reason))
46+
logger.info("Sleeping for five minutes.")
47+
time.sleep(5 * 60)
3748

3849
# for other types of issues with specific behaviour
3950
# add here an additional handler
40-
4151
# == DEFAULT ==
42-
logger.info("Sleeping for ten seconds.")
43-
time.sleep(10)
52+
else:
53+
logger.info("Sleeping for ten seconds.")
54+
time.sleep(10)

input.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def get_args():
4444
help="Search for tweets with a defined keyword.",
4545
)
4646

47+
parser.add_argument(
48+
"-nu",
49+
"--no-user",
50+
action="store_true",
51+
help="Don't like and retweet user tweets.",
52+
)
53+
4754
group.add_argument(
4855
"-s",
4956
"--stats",

twitterbot2.py

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def likes_rt_home(bot, logger, tweet_count, likes_count, retweet_count):
121121

122122
try:
123123
home = get_home(bot)
124-
except twitter.api.TwitterHTTPError as e:
124+
except Exception as e:
125125
errors.error_handler(e)
126126

127127
if home is not None:
@@ -132,12 +132,12 @@ def likes_rt_home(bot, logger, tweet_count, likes_count, retweet_count):
132132

133133
try:
134134
likes_count = put_like(bot, tweet_home, logger, likes_count)
135-
except twitter.api.TwitterHTTPError as e:
135+
except Exception as e:
136136
errors.error_handler(e)
137137

138138
try:
139139
retweet_count = retweet_tweet(bot, tweet_home, logger, retweet_count)
140-
except twitter.api.TwitterHTTPError as e:
140+
except Exception as e:
141141
errors.error_handler(e)
142142

143143
time.sleep(2)
@@ -152,7 +152,7 @@ def likes_rt_user(bot, logger, tweet_count, likes_count, retweet_count):
152152
"""
153153
try:
154154
home = get_friend_home(bot, globals.user)
155-
except twitter.api.TwitterHTTPError as e:
155+
except Exception as e:
156156
errors.error_handler(e)
157157

158158
if home is not None:
@@ -163,20 +163,20 @@ def likes_rt_user(bot, logger, tweet_count, likes_count, retweet_count):
163163

164164
try:
165165
likes_count = put_like(bot, tweet_home, logger, likes_count)
166-
except twitter.api.TwitterHTTPError as e:
166+
except Exception as e:
167167
errors.error_handler(e)
168168

169169
try:
170170
retweet_count = retweet_tweet(bot, tweet_home, logger, retweet_count)
171-
except twitter.api.TwitterHTTPError as e:
171+
except Exception as e:
172172
errors.error_handler(e)
173173

174174
time.sleep(2)
175175

176176
return tweet_count, likes_count, retweet_count
177177

178178

179-
def crawl_timeline(bot, logger):
179+
def crawl_timeline(bot, logger, no_user):
180180
"""
181181
This is the handle function of the -t or --timeline option.
182182
"""
@@ -219,9 +219,14 @@ def crawl_timeline(bot, logger):
219219
logger.info("Sleeping for one minute.")
220220
time.sleep(60)
221221

222-
tweet_count, likes_count, retweet_count = likes_rt_user(
223-
bot, logger, tweet_count, likes_count, retweet_count
224-
)
222+
if no_user:
223+
tweet_count, likes_count, retweet_count = likes_rt_home(
224+
bot, logger, tweet_count, likes_count, retweet_count
225+
)
226+
else:
227+
tweet_count, likes_count, retweet_count = likes_rt_user(
228+
bot, logger, tweet_count, likes_count, retweet_count
229+
)
225230

226231
# update the values in the database
227232
today = datetime.datetime.today().strftime("%Y-%m-%d")
@@ -230,7 +235,7 @@ def crawl_timeline(bot, logger):
230235
# retrieve the up-to-date followers count
231236
try:
232237
followers_count = followers(bot, globals.bot_user)
233-
except twitter.api.TwitterHTTPError as e:
238+
except Exception as e:
234239
errors.error_handler(e)
235240

236241
# if there aren't data, creates a record in the statistics table
@@ -290,7 +295,7 @@ def likes_rt_search(bot, logger, keyword, tweet_count, likes_count, retweet_coun
290295
return tweet_count, likes_count, retweet_count
291296

292297

293-
def crawl_keyword(bot, logger, keyword):
298+
def crawl_keyword(bot, logger, keyword, no_user):
294299
"""
295300
This is the handle function of the -k or --keyword option.
296301
"""
@@ -330,12 +335,18 @@ def crawl_keyword(bot, logger, keyword):
330335
tweet_count, likes_count, retweet_count = likes_rt_search(
331336
bot, logger, keyword, tweet_count, likes_count, retweet_count
332337
)
338+
333339
logger.info("Sleeping for one minute.")
334340
time.sleep(60)
335341

336-
tweet_count, likes_count, retweet_count = likes_rt_user(
337-
bot, logger, tweet_count, likes_count, retweet_count
338-
)
342+
if no_user:
343+
tweet_count, likes_count, retweet_count = likes_rt_search(
344+
bot, logger, keyword, tweet_count, likes_count, retweet_count
345+
)
346+
else:
347+
tweet_count, likes_count, retweet_count = likes_rt_user(
348+
bot, logger, tweet_count, likes_count, retweet_count
349+
)
339350

340351
# update the values in the database
341352
today = datetime.datetime.today().strftime("%Y-%m-%d")
@@ -379,7 +390,7 @@ def crawl_keyword(bot, logger, keyword):
379390
time.sleep(15 * 60)
380391
logger.info("Sleeping for 15 minutes.")
381392
time.sleep(15 * 60)
382-
except twitter.api.TwitterHTTPError as e:
393+
except Exception as e:
383394

384395
if tweet_count != 0:
385396
db.update_stat(
@@ -438,6 +449,7 @@ def main():
438449
args=(
439450
bot,
440451
logger,
452+
args.no_user,
441453
),
442454
)
443455
t2 = Thread(target=server.app.run, kwargs={"host": "0.0.0.0"})
@@ -454,6 +466,7 @@ def main():
454466
bot,
455467
logger,
456468
args.keyword,
469+
args.no_user,
457470
),
458471
)
459472
t2 = Thread(target=server.app.run, kwargs={"host": "0.0.0.0"})

usage.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def usage():
1616
"""
17-
usage: twitterbot2.py [-h] [-v | -t | -k KEYWORD | -s STATS | -oc OUTPUT_CSV | -oj OUTPUT_JSON | -oh OUTPUT_HTML]
17+
usage: twitterbot2.py [-h] [-v | -t | -k KEYWORD | -nu | -s STATS | -oc OUTPUT_CSV | -oj OUTPUT_JSON | -oh OUTPUT_HTML]
1818
1919
Twitterbot v2
2020
@@ -24,6 +24,7 @@ def usage():
2424
-t, --timeline Search for tweets in the bot and user's timeline.
2525
-k KEYWORD, --keyword KEYWORD
2626
Search for tweets with a defined keyword.
27+
-nu, --no-user Don't like and retweet user tweets.
2728
-s STATS, --stats STATS
2829
Show the statistics of the inputted bot (username).
2930
-oc OUTPUT_CSV, --output-csv OUTPUT_CSV
@@ -43,6 +44,7 @@ def usage():
4344
print(" -t, --timeline Search for tweets in the bot and user's timeline.")
4445
print(" -k KEYWORD, --keyword KEYWORD")
4546
print(" Search for tweets with a defined keyword.")
47+
print(" -nu, --no-user Don't like and retweet user tweets.")
4648
print(" -s STATS, --stats STATS")
4749
print(" Show the statistics of the inputted bot (username).")
4850
print(" -oc OUTPUT_CSV, --output-csv OUTPUT_CSV")

0 commit comments

Comments
 (0)