-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtwee_msg.py
More file actions
44 lines (33 loc) · 1.24 KB
/
twee_msg.py
File metadata and controls
44 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# -*- coding: utf-8 -*-
import tweepy
import configparser
CONFIG_FILE = '../twee_msg.ini'
# Reading config
config = configparser.ConfigParser()
with open(CONFIG_FILE) as configfile:
config.read_file(configfile)
auth = tweepy.OAuthHandler(config['APP']['CONSUMER_KEY'],config['APP']['CONSUMER_SECRET'])
# Check if Auth tocken exists
if not config['APP']['AUTH_SECRET'] or not config['APP']['AUTH_TOKEN']:
try:
redirect_url = auth.get_authorization_url()
print redirect_url
except tweepy.TweepError:
print 'Error! Failed to get request token.'
# Example w/o callback (desktop)
verifier = raw_input('Go to URL above, authorize and enter the code:')
try:
auth.get_access_token(verifier)
except tweepy.TweepError:
print 'Error! Failed to get access token.'
config['APP']['AUTH_SECRET'] = auth.access_token_secret
config['APP']['AUTH_TOKEN'] = auth.access_token
with open(CONFIG_FILE,'w') as configfile:
config.write(configfile)
else:
auth.set_access_token(config['APP']['AUTH_TOKEN'], config['APP']['AUTH_SECRET'])
# Doing main part
api = tweepy.API(auth)
for m in api.direct_messages():
print m.text
api.send_direct_message(screen_name='andjelx',text='This is Twitter')