Commit 247780a4 authored by rdecoupe's avatar rdecoupe
Browse files

add a logger

parent 539c5244
#!/usr/bin/env python #!/usr/bin/env python
""" """
@brief Collect Tweets
@author Remy Decoupes
@copyright
Connect to Twitter stream using Twitter API and filter tweets which have to be retrieved with Connect to Twitter stream using Twitter API and filter tweets which have to be retrieved with
- Account to follow : accountsFollowed.csv - Account to follow : accountsFollowed.csv
- Hashtag to follow : keywordsFilter.csv - Hashtag to follow : keywordsFilter.csv
...@@ -9,21 +13,52 @@ To install and run this script : please follow instructions from README.md ...@@ -9,21 +13,52 @@ To install and run this script : please follow instructions from README.md
""" """
import tweepy import tweepy
import sys import sys
import logging
from logging.handlers import RotatingFileHandler
def exitscript(): def exitscript(logger, message):
print("The program encountered an error.\nEnd of execution") """
Log error and exit script
:param logger: a logger object
:param message: print a message
:return:
"""
logger.error("The program encountered an error")
logger.error(msg)
logger.error("End of execution.")
sys.exit(1) sys.exit(1)
try: def logsetup():
import credentials """
except ImportError: Initiate a logger object :
print('it seems there is no file named :"credentials.py"') - Log in file : collectweets.log
exitscript() - also print on screen
:return: logger object
"""
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s :: %(levelname)s :: %(message)s')
file_handler = RotatingFileHandler('collectweets.log', 'a', 1000000, 1)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)
return logger
if __name__ == '__main__': if __name__ == '__main__':
print("Collect tweets : start") print("Collect tweets : start")
# initialize a logger :
logger = logsetup()
# try import credentials of MOODTwitter account
try:
import credentials
except ImportError:
msg = 'it seems there is no file named :"credentials.py"'
exitscript(logger, msg)
# Access and authorize on MOOD twitter Account # Access and authorize on MOOD twitter Account
try: try:
auth = tweepy.OAuthHandler(credentials.consumer_key, credentials.consumer_secret) auth = tweepy.OAuthHandler(credentials.consumer_key, credentials.consumer_secret)
...@@ -33,10 +68,10 @@ if __name__ == '__main__': ...@@ -33,10 +68,10 @@ if __name__ == '__main__':
user = api.get_user('twitter') user = api.get_user('twitter')
print(user.screen_name) print(user.screen_name)
except tweepy.TweepError as twe: except tweepy.TweepError as twe:
print("Wrong credentials: please check credentials.py ") msg = "Wrong credentials: please check credentials.py "
except Exception as e: except Exception as e:
print("Please double check credentials.py :" + e) msg = "Please double check credentials.py :" + e
finally: finally:
exitscript() exitscript(logger, msg)
print("Collect tweets : end") print("Collect tweets : end")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment