-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.py
81 lines (75 loc) · 2.48 KB
/
server.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
import requests
import json
import os
import settings
import exif
import db
import logger
l = logger.setup(__name__)
sender = 0
def post(endpoint, payload, resend=False):
"""
POST a json payload to the photostreamer-server
"""
cfg = settings.config()
url = cfg.get('server', 'url') + endpoint
try:
r = requests.post(url,
data=json.dumps(payload), headers={'content-type':'application/json'})
if r.status_code == 200:
l.debug("POSTed %s to photostreamer-server endpoint %s.", payload, endpoint)
return True
else:
l.error("Received HTTP status code %d while POSTing to %s.",
r.status_code, url)
if resend == False:
post_later(endpoint, payload)
return False
except requests.ConnectionError:
l.exception("Network error trying to POST to %s.", url)
if resend == False:
post_later(endpoint, payload)
return False
def post_later(endpoint, payload):
"""
Store the POST data in the database for sending later.
"""
sql = db.connect()
posts = sql['posts']
postId = posts.insert(dict(endpoint=endpoint, payload=json.dumps(payload)))
l.warning("Failed to send %s to photostreamer-server endpoint %s. Saved for resend as ID %d.",
payload, endpoint, postId)
def get(endpoint):
"""
GET a json payload from the photostreamer-server
"""
cfg = settings.config()
url = cfg.get('server', 'url') + endpoint
try:
r = requests.get(url)
if r.status_code == 200:
l.debug("photostreamer-server endpoint %s responded with %s", endpoint, r.json())
return r.json()
else:
l.error("Received HTTP status code %d while trying to reach %s.",
r.status_code, url)
return False
except requests.ConnectionError:
l.error("Network error trying to GET %s.", url)
return False
def post_thumb(local_file, s3_file, key):
"""
Notify photostreamer-server that a new thumbnail photo has been posted
to S3.
"""
l.debug("Notifying photostreamer-server of thumbnail photo %s", key)
tags = exif.parse(local_file)
filesize = os.path.getsize(local_file)
payload = {
"sender" : sender,
"filesize": filesize,
"fileid": key,
"thumbnail": s3_file.generate_url(expires_in=0, query_auth=False),
"exif": tags
}
success = post('/photo/thumb', payload)