collectTweets.py 2.3 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
43
44
45
46
47
48
49
50
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
rdecoupe's avatar
rdecoupe committed
51
52
53

if __name__ == '__main__':
    print("Collect tweets : start")
rdecoupe's avatar
rdecoupe committed
54
55
56
57
58
59
60
61
    # 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)
rdecoupe's avatar
rdecoupe committed
62
63
64
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...
        user = api.get_user('twitter')
        print(user.screen_name)
    except tweepy.TweepError as twe:
rdecoupe's avatar
rdecoupe committed
71
        msg = "Wrong credentials: please check credentials.py "
rdecoupe's avatar
rdecoupe committed
72
    except Exception as e:
rdecoupe's avatar
rdecoupe committed
73
        msg = "Please double check credentials.py :" + e
rdecoupe's avatar
rdecoupe committed
74
    finally:
rdecoupe's avatar
rdecoupe committed
75
        exitscript(logger, msg)
rdecoupe's avatar
rdecoupe committed
76
77

    print("Collect tweets : end")