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

add a logger

parent 539c5244
#!/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
- Account to follow : accountsFollowed.csv
- Hashtag to follow : keywordsFilter.csv
......@@ -9,21 +13,52 @@ To install and run this script : please follow instructions from README.md
"""
import tweepy
import sys
import logging
from logging.handlers import RotatingFileHandler
def exitscript():
print("The program encountered an error.\nEnd of execution")
def exitscript(logger, message):
"""
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)
try:
import credentials
except ImportError:
print('it seems there is no file named :"credentials.py"')
exitscript()
def logsetup():
"""
Initiate a logger object :
- Log in file : collectweets.log
- 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__':
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
try:
auth = tweepy.OAuthHandler(credentials.consumer_key, credentials.consumer_secret)
......@@ -33,10 +68,10 @@ if __name__ == '__main__':
user = api.get_user('twitter')
print(user.screen_name)
except tweepy.TweepError as twe:
print("Wrong credentials: please check credentials.py ")
msg = "Wrong credentials: please check credentials.py "
except Exception as e:
print("Please double check credentials.py :" + e)
msg = "Please double check credentials.py :" + e
finally:
exitscript()
exitscript(logger, msg)
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