-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlonelyapp.py
249 lines (225 loc) · 8.42 KB
/
lonelyapp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import MySQLdb, json, requests, uuid, os
from instagram import client, subscriptions
from StringIO import StringIO
from PIL import Image
api = None
reactor = None
publicImageDir = os.path.join('uploads', 'instagram_images')
imageDir = os.path.join(os.path.dirname(__file__), 'public', publicImageDir)
imageDirOriginals = os.path.join(imageDir, 'originals')
try:
os.makedirs(imageDirOriginals)
except OSError:
if not os.path.isdir(imageDirOriginals):
raise
def get_config(filename):
with open(filename) as json_file:
return json.load(json_file)
def get_database_connection():
print "Connecting to database"
conn = None
try:
conn = MySQLdb.connect(host=DATABASE_CONFIG['host'], user=DATABASE_CONFIG['username'], passwd=DATABASE_CONFIG['password'], db=DATABASE_CONFIG['db'])
except Exception, e:
print str(e)
finally:
return conn
def get_reactor():
print "Creating subscriptions reactor"
global reactor
if not reactor is None:
print "Returned existing reactor object"
return reactor
print "Created new subscriptions reactor"
return subscriptions.SubscriptionsReactor()
def get_api():
print "Creating Instagram API"
global api
if not api is None:
print "Returned existing api object"
return api
print "Created new api object"
return client.InstagramAPI(**CONFIG)
def process_request(x_hub_signature, raw_response):
try:
#reactor = get_reactor()
global reactor
reactor.process(CONFIG['client_secret'], raw_response, x_hub_signature)
except subscriptions.SubscriptionVerifyError:
print "Signature mismatch"
def process_tag_update(result):
# Fetch images with tag
payload = {'count': 1, 'client_id': CONFIG['client_id']}
r = requests.get('https://api.instagram.com/v1/tags/' + result['object_id'] + '/media/recent', params=payload)
if r.status_code == 200:
data = r.json()
try:
for entry in data['data']:
if entry['type'] == 'image':
try:
# Collect data
entry_id = entry['id']
username = entry['user']['username']
image_url = entry['images']['standard_resolution']['url']
imageFilename = str(uuid.uuid4()) + '.jpg'
# Fetch image and save to disk
r = requests.get(image_url)
i = Image.open(StringIO(r.content))
i.save(os.path.join(imageDirOriginals, imageFilename))
# Fetch random entry from database
randomEntry = get_random_instagram_entry()
if randomEntry is None:
# Save info in database and return
print "Saving image info for first instagram image"
save_instagram_entry(username, imageFilename, image_url, entry_id)
print "Saved first tag update to database"
else:
# Save info in database and carry on
print "Saving image info before pairing"
current_id = save_instagram_entry(username, imageFilename, image_url, entry_id, randomEntry['id'])
if current_id > 0:
# Cropping original image
width, height = i.size
left = width/4
top = 0
right = 3 * left
bottom = height
cropped = i.crop((left, top, right, bottom))
# Cropping paired image
i2 = Image.open(os.path.join(imageDirOriginals, randomEntry['image_filename']))
cropped2 = i2.crop((left, top, right, bottom))
# Combine images and save the resulting image to disk
combined = Image.new('RGB', (640, 480))
combined.paste(cropped, (0, 0))
combined.paste(cropped2, (320, 0))
combined.save(os.path.join(imageDir, imageFilename))
# Save paired info database
save_lonely_feed_entry(username, randomEntry['username'], imageFilename, 'instagram', current_id)
print "Saved tag update to database"
else:
print "Duplicate entry, not saved to database"
except Exception, e:
print "Error processing tag update"
print str(e)
except Exception, e:
print "Error processing tag update"
print str(e)
else:
print "Error when getting data from API"
def get_random_photobooth_entry():
try:
conn = get_database_connection()
cur = conn.cursor(MySQLdb.cursors.DictCursor)
cur.execute("""SELECT r1.id, r1.username, r1.image_filename FROM photobooth_posts AS r1 JOIN (SELECT (RAND() * (SELECT MAX(id) FROM photobooth_posts)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 1;""")
data = cur.fetchone()
cur.close()
except Exception, e:
print "Error getting random photobooth entry"
print str(e)
data = None
finally:
if conn is not None and conn.open:
conn.close()
return data
def get_random_instagram_entry():
try:
conn = get_database_connection()
cur = conn.cursor(MySQLdb.cursors.DictCursor)
cur.execute("""SELECT r1.id, r1.username, r1.image_filename FROM posts AS r1 JOIN (SELECT (RAND() * (SELECT MAX(id) FROM posts)) AS id) AS r2 WHERE r1.id >= r2.id ORDER BY r1.id ASC LIMIT 1;""")
data = cur.fetchone()
cur.close()
except Exception, e:
print "Error getting random instagram entry"
print str(e)
data = None
finally:
if conn is not None and conn.open:
conn.close()
return data
def save_photobooth_entry(username, imageFilename, randomEntryId=0):
print "Saving photobooth entry"
try:
conn = get_database_connection()
cur = conn.cursor()
cur.execute("""INSERT INTO photobooth_posts (username, image_filename, paired_id) VALUES (%s, %s, %s)""", (username, imageFilename, randomEntryId))
insert_id = cur.lastrowid
cur.close()
conn.commit()
return insert_id
except Exception, e:
print "Error saving photobooth entry"
print str(e)
finally:
if conn is not None and conn.open:
conn.close()
return 0
def save_instagram_entry(username, imageFilename, imageUrl, postId, randomEntryId=0):
print "Saving instagram entry"
try:
conn = get_database_connection()
cur = conn.cursor()
cur.execute("""INSERT INTO posts (username, image_filename, post_id, image_url, paired_id) VALUES (%s, %s, %s, %s, %s)""", (username, imageFilename, postId, imageUrl, randomEntryId))
insert_id = cur.lastrowid
cur.close()
conn.commit()
return insert_id
except Exception, e:
print "Error saving photobooth entry"
print str(e)
finally:
if conn is not None and conn.open:
conn.close()
return 0
def save_lonely_feed_entry(username, username2, image_filename, source, source_id):
print "Saving lonely feed entry"
try:
conn = get_database_connection()
cur = conn.cursor()
cur.execute("""INSERT INTO lonely_feed (left_username, right_username, image_filename, source, source_id) VALUES (%s, %s, %s, %s, %s)""", (username, username2, image_filename, source, source_id))
insert_id = cur.lastrowid
cur.close()
conn.commit()
return insert_id
except Exception, e:
print "Error saving lonely feed entry"
print str(e)
finally:
if conn is not None and conn.open:
conn.close()
return 0
def get_feed(count, lastId):
try:
par = (int(lastId), int(count))
conn = get_database_connection()
cur = conn.cursor(MySQLdb.cursors.DictCursor)
cur.execute("SELECT id, left_username, right_username, image_filename, source FROM lonely_feed WHERE id > %s ORDER BY id DESC LIMIT %s", par)
data = cur.fetchall()
cur.close()
return json.dumps(data, encoding="iso-8859-1")
except Exception, e:
print str(e)
return str(e)
finally:
if conn is not None and conn.open:
conn.close()
def get_entry(entryId):
try:
conn = get_database_connection()
cur = conn.cursor(MySQLdb.cursors.DictCursor)
cur.execute("SELECT id, left_username, right_username, image_filename, source FROM lonely_feed WHERE id = %s", (int(entryId),))
data = cur.fetchall()
cur.close()
return data
except Exception, e:
return str(e)
finally:
if conn is not None and conn.open:
conn.close()
print "Reading configuration files"
CONFIG = get_config('config/api.json')
# APP_CONFIG = get_config('config/app.json')
DATABASE_CONFIG = get_config('config/db.json')
api = get_api()
reactor = get_reactor()
print "Registering subscription callback"
reactor.register_callback(subscriptions.SubscriptionType.TAG, process_tag_update)