-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
109 lines (87 loc) · 3.33 KB
/
worker.py
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import tweepy
from tweepy import Stream
from tweepy import OAuthHandler
from instamojo import Instamojo
import re
import os
import psycopg2
import urlparse
# Twitter Consumer keys and access tokens, used for OAuth
consumer_key = 'nZEzUToqKZcMIWu4nSNXnq6Kq'
consumer_secret = 'xZpwdeiE4FnhQ5E4SE7O3KKa3FCzNWiPfDGRvrIPyHZKvpo4ZH'
access_token = '3183114434-w4opn1VCE4DONH3aTybI0BjMVcdfphNrlbBVP9R'
access_token_secret = 'bz5RRCPpg1qCsRehFCzgoeqlKtKq45rSvyZ9fMGuLCYwb'
#Connecting to database
urlparse.uses_netloc.append("postgres")
url = urlparse.urlparse("postgres://jstgsgfleazuvu:nJGYg0dT6AYMNbqdBkV3bIf-Q8@ec2-184-73-165-195.compute-1.amazonaws.com:5432/dbaklh7r4800dg")
#Database Open/Close methods
def opendb():
return psycopg2.connect(
database=url.path[1:],
user=url.username,
password=url.password,
host=url.hostname,
port=url.port
)
def closedb(conn):
conn.commit()
conn.close()
#Tweet pattern
pattern = r'#sell (.*) which is (.*) and costs (\d+) (\w+)'
class StdOutListener(tweepy.streaming.StreamListener):
''' Handles data received from the stream. '''
def on_status(self, status):
text = status.text.encode('ascii','ignore')
if "#sell " in text:
twitter_id = str(status.author.id_str)
twitter_handle = str(status.author.screen_name)
try:
m = re.match(pattern, text)
params = m.groups()
instamojo_auth = user_details[twitter_id]["instamojo_auth"]
api = Instamojo(api_key='4dcb3d45a65808a290e7b79336b4c5be',
auth_token=instamojo_auth)
# Create a Instamojo Link.
response = api.link_create(title=params[0],
description=params[1],
base_price=params[2],
currency=params[3])
# URL of the created link
url = str(response['link']['url'])
#Printing to console
print twitter_handle
print text
print url
# Saving details to Tweets database
conn = opendb()
c = conn.cursor()
c.execute("INSERT INTO Tweets VALUES ('%s','%s','%s')"%(twitter_handle,text,url))
closedb(conn)
print "Database Updated"
except:
pass
return True
def on_error(self, status_code):
print('Got an error with status code: ' + str(status_code))
return True # To continue listening
def on_timeout(self):
print('Timeout...')
return True # To continue listening
#Select twitter_ids to follow
userconn = opendb()
userdb = userconn.cursor()
userdb.execute('SELECT * FROM Users')
users = userdb.fetchall()
user_details = {}
twitter_ids = []
for user in users:
twitter_ids.append(str(user[1]))
user_details[str(user[1])] = {"twitter_handle":str(user[0]),"instamojo_auth":str(user[2])}
closedb(userconn)
#Initializing stream
listener = StdOutListener()
auth = OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
stream = Stream(auth, listener)
stream.filter(follow=twitter_ids)