collectTweets.py 2.4 KB
Newer Older
rdecoupe's avatar
rdecoupe committed
1
2
3
#!/usr/bin/env python

"""
rdecoupe's avatar
rdecoupe committed
4
5
6
7
@brief Collect Tweets
@author Remy Decoupes
@copyright

rdecoupe's avatar
rdecoupe committed
8
9
10
11
12
13
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

To install and run this script : please follow instructions from README.md
"""
rdecoupe's avatar
rdecoupe committed
14
15
import tweepy
import sys
rdecoupe's avatar
rdecoupe committed
16
17
import logging
from logging.handlers import RotatingFileHandler
rdecoupe's avatar
rdecoupe committed
18
19


rdecoupe's avatar
rdecoupe committed
20
21
22
23
24
25
26
27
28
29
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.")
rdecoupe's avatar
rdecoupe committed
30
31
32
    sys.exit(1)


rdecoupe's avatar
rdecoupe committed
33
34
35
36
37
38
39
40
41
42
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')
rdecoupe's avatar
rdecoupe committed
43
    file_handler = RotatingFileHandler('log/collectweets.log', 'a', 1000000, 1)
rdecoupe's avatar
rdecoupe committed
44
45
46
47
    file_handler.setLevel(logging.DEBUG)
    file_handler.setFormatter(formatter)
    logger.addHandler(file_handler)
    stream_handler = logging.StreamHandler()
Rémy Decoupes's avatar
Rémy Decoupes committed
48
49
    # Only display on screen INFO
    stream_handler.setLevel(logging.INFO)
rdecoupe's avatar
rdecoupe committed
50
51
    logger.addHandler(stream_handler)
    return logger
rdecoupe's avatar
rdecoupe committed
52
53
54

if __name__ == '__main__':
    print("Collect tweets : start")
rdecoupe's avatar
rdecoupe committed
55
56
    # initialize a logger :
    logger = logsetup()
Rémy Decoupes's avatar
Rémy Decoupes committed
57

rdecoupe's avatar
rdecoupe committed
58
59
    # try import credentials of MOODTwitter account
    try:
rdecoupe's avatar
rdecoupe committed
60
        from params import credentials
rdecoupe's avatar
rdecoupe committed
61
62
63
    except ImportError:
        msg = 'it seems there is no file named :"credentials.py"'
        exitscript(logger, msg)
Rémy Decoupes's avatar
Rémy Decoupes committed
64

rdecoupe's avatar
rdecoupe committed
65
66
67
68
69
70
    # Access and authorize on MOOD twitter Account
    try:
        auth = tweepy.OAuthHandler(credentials.consumer_key, credentials.consumer_secret)
        auth.set_access_token(credentials.access_token, credentials.access_token_secret)
        api = tweepy.API(auth)
        # Get the User object for twitter...
Rémy Decoupes's avatar
Rémy Decoupes committed
71
72
        accountused = api.me()
        logger.info("Log with: "+accountused.name)
rdecoupe's avatar
rdecoupe committed
73
    except tweepy.TweepError as twe:
Rémy Decoupes's avatar
Rémy Decoupes committed
74
75
        msg = "Wrong credentials: please check credentials.py"
        exitscript(logger, msg)
rdecoupe's avatar
rdecoupe committed
76
    except Exception as e:
rdecoupe's avatar
rdecoupe committed
77
78
        msg = "Please double check credentials.py :" + e
        exitscript(logger, msg)
rdecoupe's avatar
rdecoupe committed
79

Rémy Decoupes's avatar
Rémy Decoupes committed
80
    logger.info("Collect tweets : proceeded normally")