Skip to content

Commit

Permalink
🌟 Improve Logging
Browse files Browse the repository at this point in the history
  • Loading branch information
waihankan committed Mar 22, 2021
1 parent dba8092 commit 10d98b7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
19 changes: 18 additions & 1 deletion script/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,20 @@
import sys
import os
import tweepy
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(asctime)s:%(name)s: %(message)s")

file_handler = logging.FileHandler("twitter.log")
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(formatter)

stream_handler = logging.StreamHandler()
logger.addHandler(file_handler)
logger.addHandler(stream_handler)


def login(api_key, api_secret, access_token, token_secret):
Expand All @@ -20,13 +34,15 @@ def login(api_key, api_secret, access_token, token_secret):
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
try:
if api.verify_credentials():
print("ok")
logger.debug("API OK")
sys.exit(0)
else:
logger.error("Bad Authentication Error")
os.remove("config.py")

except tweepy.TweepError as e:
print(e)
logger.error(e.reason)
os.remove("config.py")


Expand Down Expand Up @@ -344,6 +360,7 @@ def main():
window = App(root, logo, config)

except ImportError:
logger.warning("API not found. Open api window")
window = api_window(root)

root.mainloop()
Expand Down
42 changes: 30 additions & 12 deletions script/twitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
import re
import os
import sys
import logging

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

formatter = logging.Formatter("%(asctime)s:%(name)s: %(message)s")

file_handler = logging.FileHandler("twitter.log")
file_handler.setLevel(logging.WARNING)
file_handler.setFormatter(formatter)

stream_handler = logging.StreamHandler()
logger.addHandler(file_handler)
logger.addHandler(stream_handler)


class Twitter():
Expand All @@ -26,10 +40,10 @@ def checkAuth(self):
wait_on_rate_limit_notify=True)

if self.api.verify_credentials():
print("Valid Credentials")
logger.info("Valid Credentials")
return True
else:
print("Invalid Credentials")
logger.info("Invalid Credentials")
sys.exit(1)

def _is_retweet(self, tweet):
Expand Down Expand Up @@ -57,28 +71,28 @@ def _like_and_retweet(self, tweet):
if not tweet.favorited:
tweet.favorite()
self.like_counter += 1
print("like tweet")
logger.info("like tweet")
else:
print("already liked")
logger.info("already liked")

if not tweet.retweeted:
tweet.retweet()
content = "* " +tweet.full_text[:48].replace('\n', ' ')
content = content.replace('RT @', '')
self.log.append(content)
self.retweet_counter += 1
print("retweet")
logger.info("retweet")
else:
content = "# " +tweet.full_text[4:49].replace('\n', ' ')
self.log.append(content)
print("retweeted post")
logger.info("retweeted post")


def _quote_tweet(self, tweet):
if not tweet.favorited:
tweet.favorite()
self.like_counter += 1
print("like tweet")
logger.info("like tweet")

if tweet.is_quote_status:
try:
Expand All @@ -92,7 +106,7 @@ def _quote_tweet(self, tweet):
else:
self.url = "https://twitter.com/" + self.username + "/status/" + str(tweet.id)

print(self.url)
logger.info(self.url)
quoted_tweet = self.status + "#WhatsHappeningInMyanmar \n" + self.url
try:
self.api.update_status(quoted_tweet)
Expand All @@ -104,16 +118,17 @@ def _quote_tweet(self, tweet):

except tweepy.TweepError as e:
self.log.append("! Duplicate")
print(e.reason)
logger.warning(e.reason)
else:
self.log.append("! Duplicate")
print("already liked")
logger.warning("Duplicate")

def like_and_retweet(self):
try:
self.user = self.api.get_user(self.username)
if not re.match(r"^\d+$", self.tweets_num):
self.log.append("Please Enter a valid number of tweets. [1-100]")
logger.warning("Invalid number of tweets. [1-100]")
return True

self.timeline = self.api.user_timeline(self.username, count=self.tweets_num
Expand All @@ -129,14 +144,15 @@ def like_and_retweet(self):
if self._check_hashtag(tweet.full_text):
self._like_and_retweet(tweet)

print("All good. Like {like} tweets; Retweet {retweet} tweets."
logger.info("All good. Like {like} tweets; Retweet {retweet} tweets."
.format(like=self.like_counter, retweet=self.retweet_counter))

self.log.append(">> Successfully Like {like} tweets; Retweet {retweet} tweets.\n"
.format(like=self.like_counter, retweet=self.retweet_counter))

except tweepy.TweepError as e:
self.log.append(e.reason)
logger.warning(e.reason)


def like_and_quote_tweet(self):
Expand All @@ -145,6 +161,7 @@ def like_and_quote_tweet(self):
self.user = self.api.get_user(self.username)
if not re.match(r"^\d+$", self.tweets_num):
self.log.append("Please Enter a valid number of tweets. [1-100]")
logger.warning("Invalid number of tweets. [1-100]")
return True

self.timeline = self.api.user_timeline(self.username, count=self.tweets_num
Expand All @@ -155,14 +172,15 @@ def like_and_quote_tweet(self):
if self._check_hashtag(tweet.full_text):
self._quote_tweet(tweet)

print("All good. Like {like} tweets; Q_Tweet {q_tweet} tweets."
logger.info("All good. Like {like} tweets; Q_Tweet {q_tweet} tweets."
.format(like=self.like_counter, q_tweet=self.q_tweet_counter))

self.log.append(">> Successfully Like {like} tweets; Q_Tweet {q_tweet} tweets.\n"
.format(like=self.like_counter, q_tweet=self.q_tweet_counter))

except tweepy.TweepError as e:
self.log.append(e.reason)
logger.warning(e.reason)


def get_log(self):
Expand Down

0 comments on commit 10d98b7

Please sign in to comment.