Skip to content

Commit

Permalink
Merge pull request #35 from edoardottt/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
edoardottt authored Oct 13, 2021
2 parents 19708de + 4089742 commit 46d06ba
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 32 deletions.
41 changes: 26 additions & 15 deletions errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,41 @@

import time
import logging
import twitter
import urllib


def error_handler(e):
"""
This function handles all the TwitterHTTPError errors
This function handles all the twitterbot2 errors
- twitter.api.TwitterHTTPError
- urllib.error.URLError
- generic
"""

logger = logging.getLogger("__main__")
logger.error(str(e.e) + " on " + e.uri)
if e.__class__ == twitter.api.TwitterHTTPError:
logger.error(str(e.e) + " on " + e.uri)

# == 429 TOO MANY REQUESTS -> Sleep for one hour
if str(e.e.code) == "429":
logger.info("Sleeping for one hour.")
time.sleep(60 * 60)
return
# == 429 TOO MANY REQUESTS -> Sleep for one hour
if str(e.e.code) == "429":
logger.info("Sleeping for one hour.")
time.sleep(60 * 60)
return

# == 403 FORBIDDEN -> Sleep for ten seconds
if str(e.e.code) == "403":
logger.info("Sleeping for ten seconds.")
time.sleep(10)
return
# == 403 FORBIDDEN -> Sleep for ten seconds
if str(e.e.code) == "403":
logger.info("Sleeping for ten seconds.")
time.sleep(10)
return
elif e.__class__ == urllib.error.URLError:
logger.error(str(e.reason))
logger.info("Sleeping for five minutes.")
time.sleep(5 * 60)

# for other types of issues with specific behaviour
# add here an additional handler

# == DEFAULT ==
logger.info("Sleeping for ten seconds.")
time.sleep(10)
else:
logger.info("Sleeping for ten seconds.")
time.sleep(10)
7 changes: 7 additions & 0 deletions input.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ def get_args():
help="Search for tweets with a defined keyword.",
)

parser.add_argument(
"-nu",
"--no-user",
action="store_true",
help="Don't like and retweet user tweets.",
)

group.add_argument(
"-s",
"--stats",
Expand Down
45 changes: 29 additions & 16 deletions twitterbot2.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def likes_rt_home(bot, logger, tweet_count, likes_count, retweet_count):

try:
home = get_home(bot)
except twitter.api.TwitterHTTPError as e:
except Exception as e:
errors.error_handler(e)

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

try:
likes_count = put_like(bot, tweet_home, logger, likes_count)
except twitter.api.TwitterHTTPError as e:
except Exception as e:
errors.error_handler(e)

try:
retweet_count = retweet_tweet(bot, tweet_home, logger, retweet_count)
except twitter.api.TwitterHTTPError as e:
except Exception as e:
errors.error_handler(e)

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

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

try:
likes_count = put_like(bot, tweet_home, logger, likes_count)
except twitter.api.TwitterHTTPError as e:
except Exception as e:
errors.error_handler(e)

try:
retweet_count = retweet_tweet(bot, tweet_home, logger, retweet_count)
except twitter.api.TwitterHTTPError as e:
except Exception as e:
errors.error_handler(e)

time.sleep(2)

return tweet_count, likes_count, retweet_count


def crawl_timeline(bot, logger):
def crawl_timeline(bot, logger, no_user):
"""
This is the handle function of the -t or --timeline option.
"""
Expand Down Expand Up @@ -219,9 +219,14 @@ def crawl_timeline(bot, logger):
logger.info("Sleeping for one minute.")
time.sleep(60)

tweet_count, likes_count, retweet_count = likes_rt_user(
bot, logger, tweet_count, likes_count, retweet_count
)
if no_user:
tweet_count, likes_count, retweet_count = likes_rt_home(
bot, logger, tweet_count, likes_count, retweet_count
)
else:
tweet_count, likes_count, retweet_count = likes_rt_user(
bot, logger, tweet_count, likes_count, retweet_count
)

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

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


def crawl_keyword(bot, logger, keyword):
def crawl_keyword(bot, logger, keyword, no_user):
"""
This is the handle function of the -k or --keyword option.
"""
Expand Down Expand Up @@ -330,12 +335,18 @@ def crawl_keyword(bot, logger, keyword):
tweet_count, likes_count, retweet_count = likes_rt_search(
bot, logger, keyword, tweet_count, likes_count, retweet_count
)

logger.info("Sleeping for one minute.")
time.sleep(60)

tweet_count, likes_count, retweet_count = likes_rt_user(
bot, logger, tweet_count, likes_count, retweet_count
)
if no_user:
tweet_count, likes_count, retweet_count = likes_rt_search(
bot, logger, keyword, tweet_count, likes_count, retweet_count
)
else:
tweet_count, likes_count, retweet_count = likes_rt_user(
bot, logger, tweet_count, likes_count, retweet_count
)

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

if tweet_count != 0:
db.update_stat(
Expand Down Expand Up @@ -438,6 +449,7 @@ def main():
args=(
bot,
logger,
args.no_user,
),
)
t2 = Thread(target=server.app.run, kwargs={"host": "0.0.0.0"})
Expand All @@ -454,6 +466,7 @@ def main():
bot,
logger,
args.keyword,
args.no_user,
),
)
t2 = Thread(target=server.app.run, kwargs={"host": "0.0.0.0"})
Expand Down
4 changes: 3 additions & 1 deletion usage.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

def usage():
"""
usage: twitterbot2.py [-h] [-v | -t | -k KEYWORD | -s STATS | -oc OUTPUT_CSV | -oj OUTPUT_JSON | -oh OUTPUT_HTML]
usage: twitterbot2.py [-h] [-v | -t | -k KEYWORD | -nu | -s STATS | -oc OUTPUT_CSV | -oj OUTPUT_JSON | -oh OUTPUT_HTML]
Twitterbot v2
Expand All @@ -24,6 +24,7 @@ def usage():
-t, --timeline Search for tweets in the bot and user's timeline.
-k KEYWORD, --keyword KEYWORD
Search for tweets with a defined keyword.
-nu, --no-user Don't like and retweet user tweets.
-s STATS, --stats STATS
Show the statistics of the inputted bot (username).
-oc OUTPUT_CSV, --output-csv OUTPUT_CSV
Expand All @@ -43,6 +44,7 @@ def usage():
print(" -t, --timeline Search for tweets in the bot and user's timeline.")
print(" -k KEYWORD, --keyword KEYWORD")
print(" Search for tweets with a defined keyword.")
print(" -nu, --no-user Don't like and retweet user tweets.")
print(" -s STATS, --stats STATS")
print(" Show the statistics of the inputted bot (username).")
print(" -oc OUTPUT_CSV, --output-csv OUTPUT_CSV")
Expand Down

0 comments on commit 46d06ba

Please sign in to comment.