tagmyloc.py (view raw)
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 |
#!/usr/bin/env python3
import asyncio
import uvloop
import os
from sanic import Sanic
import sanic.response
from sanic.log import log as logging
#import jinja2
import requests
import shared
import json
def locationtags_500px(lat, lon, radius=0.5, num=10):
tags = []
if not lat or not lon:
return tags
logging.info("requesting locationtags from 500px for '%s, %s'", lat, lon)
params = {
'rpp': 100,
'geo': "%s,%s,%skm" % (lat, lon, radius),
'consumer_key': shared.config.get('500px', 'api_key'),
'tags': 1,
}
r = requests.get('https://api.500px.com/v1/photos/search',params=params)
try:
results = json.loads(r.text)
except Exception as e:
logging.error('failed to load results for 500px request: %s', e)
logging.error('request was: %s', r.url)
return tags, r.status_code
_temp = {}
for p in results.get('photos', []):
for t in p.get('tags', []):
if not t or not len(t):
continue
curr = _temp.get(t, 1)
_temp[t] = curr+1
for w in sorted(_temp, key=_temp.get, reverse=True):
tags.append(w)
return tags[:num], 200
def locationtags_flickr(lat, lon, radius=0.5, num=10):
tags = []
if not lat or not lon:
return tags
logging.info("requesting locationtags from Flickr for '%s, %s'", lat, lon)
params = {
'method': 'flickr.photos.search',
'api_key': shared.config.get('flickr', 'api_key'),
'has_geo': 1,
'lat': lat,
'lon': lon,
'radius': radius,
'extras': ','.join(['tags','machine_tags']),
'per_page': 500,
'format': 'json',
'nojsoncallback': 1
}
r = requests.get('https://api.flickr.com/services/rest/',params=params)
try:
results = json.loads(r.text)
#logging.debug("flickr response: %s", results)
except Exception as e:
logging.error('failed to load results for Flickr request: %s', e)
logging.error('request was: %s', r.url)
return tags, r.status_code
_temp = {}
for p in results.get('photos', {}).get('photo', {}):
for t in p.get('tags', '').split(' '):
if not t or not len(t):
continue
curr = _temp.get(t, 1)
_temp[t] = curr+1
for w in sorted(_temp, key=_temp.get, reverse=True):
tags.append(w)
return tags[:num], 200
#return tags
def RequestHandler(lat, lon, rad, num=20):
ftags, status = locationtags_flickr(lat, lon, rad, num)
fivehtags, status = locationtags_500px(lat, lon, rad, num)
return sanic.response.json({
'flickr': ftags,
'500px': fivehtags,
}, status=status)
if __name__ == '__main__':
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
app = Sanic()
@app.route("/tagmyloc")
async def search(request, methods=["GET"]):
lat = request.args.get('lat')
lon = request.args.get('lon')
rad = request.args.get('rad')
return RequestHandler(lat, lon, rad)
app.run(host="127.0.0.1", port=8003, debug=True)
|