collectTweets.py 1.23 KB
Newer Older
rdecoupe's avatar
rdecoupe committed
1
2
3
4
5
6
7
8
9
#!/usr/bin/env python

"""
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
10
11
12
13
14
15
16
17
18
19
import tweepy
import sys


def exitscript():
    print("The program encountered an error.\nEnd of execution")
    sys.exit(1)


try:
rdecoupe's avatar
rdecoupe committed
20
21
22
    import credentials
except ImportError:
    print('it seems there is no file named :"credentials.py"')
rdecoupe's avatar
rdecoupe committed
23
    exitscript()
rdecoupe's avatar
rdecoupe committed
24
25
26

if __name__ == '__main__':
    print("Collect tweets : start")
rdecoupe's avatar
rdecoupe committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
    # 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:
        print("Wrong credentials: please check credentials.py ")
    except Exception as e:
        print("Please double check credentials.py :" + e)
    finally:
        exitscript()

    print("Collect tweets : end")