· 7 years ago · Jan 11, 2018, 08:36 PM
1from twitter import *
2import os
3
4APP_KEY,APP_SECRET = 'appkey123', 'appsecret123'
5
6
7MY_TWITTER_CREDS = os.path.expanduser('my_app_credentials')
8if not os.path.exists(MY_TWITTER_CREDS):
9 oauth_dance("crypto sentiments", APP_KEY, APP_SECRET,
10 MY_TWITTER_CREDS)
11
12oauth_token, oauth_secret = read_token_file(MY_TWITTER_CREDS)
13
14auth = OAuth(
15 consumer_key=APP_KEY,
16 consumer_secret=APP_SECRET,
17 token=oauth_token,
18 token_secret=oauth_secret
19)
20
21twitter_userstream = TwitterStream(auth=auth, domain='userstream.twitter.com')
22for msg in twitter_userstream.user():
23 print(msg)
24 if 'direct_message' in msg:
25 print (msg['direct_message']['text'])