def list_point(type): points_dict = [] points = Point.query(Point.type == type).order(Point.timestamp, Point.pointid).fetch() for point in points: points_dict.append(point.to_dict()) return Response(json.dumps(points_dict), mimetype='application/json');
def import_photos(username, photoset_title, api_key, api_secret): flickr_api.set_keys(api_key=api_key, api_secret=api_secret) user = flickr_api.Person.findByUserName(username) photosets = user.getPhotosets() for photoset in iter(photosets): if photoset.title == photoset_title: photos = photoset.getPhotos() for photo in iter(photos): photo_id = int(photo.id) point = Point.query(Point.pointid == photo_id).get() if point is None: latitude = None longitude = None timestamp = None title = photo.title thumb_url = None photo_url = None photopage = None info = photo.getInfo() taken = info[u"taken"] timestamp = datetime.strptime(taken, "%Y-%m-%d %H:%M:%S") urls = info[u"urls"][u"url"] for url in urls: if url[u"type"] == "photopage": photopage = url[u"text"] break if u"location" in info: location = info[u"location"] # locality = location[u'locality'] # region = location[u'region'] # country = location[u'country'] latitude = float(location[u"latitude"]) longitude = float(location[u"longitude"]) # title = "%s, %s, %s" % (locality, region, country) sizes = photo.getSizes() thumb_url = sizes[u"Square"][u"source"] photo_url = sizes[u"Medium"][u"source"] try: point = Point( title=title, latitude=latitude, longitude=longitude, type="photo", timestamp=timestamp, pointid=photo_id, thumb=thumb_url, photo=photo_url, resource=photopage, ) point.put() except AttributeError: pass except Exception as e: logging.error(e.args[0]) return Response(json.dumps({"status": "ok"}), status=200, mimetype="application/json")
def import_media(access_token, client_secret): global api api = InstagramAPI(access_token=access_token.encode('ascii','ignore'), client_secret=client_secret.encode('ascii','ignore')) m = hashlib.md5() media = get_media() for item in media: pointid = int(m.hexdigest()[:8], 16) point = Point.query(Point.pointid == pointid).get() if point is None: m.update(item.id) title = None desc = None video = None if hasattr(item, 'caption'): if item.caption is not None: desc = item.caption.text if hasattr(item, 'location'): if item.location.name is not None: title = item.location.name if item.location.point is not None: latitude = item.location.point.latitude longitude = item.location.point.longitude timestamp = item.created_time thumb = item.images.get('thumbnail').url photo = item.images.get('standard_resolution').url if hasattr(item, 'videos'): video = item.videos.get('standard_resolution').url resource = item.link try: point = Point( title=title, latitude=latitude, longitude=longitude, type='photo', timestamp=timestamp, pointid=pointid, thumb=thumb, photo=photo, video=video, resource=resource, desc=desc ) point.put() except AttributeError: pass except Exception as e: logging.error(e.args[0]) return Response(json.dumps({ 'status': 'ok' }), status=200, mimetype='application/json');
def list_point(type): points_dict = memcache.get('{}:points'.format(type)) if points_dict is None: points_dict = [] points = Point.query(Point.type == type).order(Point.timestamp, Point.pointid).fetch() for point in points: points_dict.append(point.to_dict()) if not memcache.add('{}:points'.format(type), points_dict, 43200): logging.error('Memcache set failed.') return Response(json.dumps(points_dict), mimetype='application/json');
def load_tracker(): tracker_url = Config.query(Config.name == 'tracker_url').order(-Config.date_added).get() if tracker_url is None: return Response(json.dumps({ 'error': 'tracker_url configuration was not found.' }), status=500, mimetype='application/json'); tracker_type = Config.query(Config.name == 'tracker_type').order(-Config.date_added).get() if tracker_type is None: return Response(json.dumps({ 'error': 'tracker_type configuration was not found.' }), status=500, mimetype='application/json'); if tracker_type.value == 'delorme': last_date = None last_point = Point.query(Point.type == 'tracker').order(-Point.timestamp).get() if last_point is not None: last_date = last_point.to_dict().timestamp return delorme.load_data(tracker_url.value, last_date) elif tracker_type.value == 'spot': return Response(json.dumps({ 'error': 'tracker not supported.' }), status=400, mimetype='application/json'); else: return Response(json.dumps({ 'error': 'tracker not supported.' }), status=400, mimetype='application/json');
def load_data(url, last_date=None): if last_date is not None: url = url + last_date.strftime('?d1=%Y-%m-%dT%H:%MZ') obj = urllib2.urlopen(url) root = parser.parse(obj).getroot() for placemark in root.Document.Folder.Placemark: try: point = None extended_data = placemark.ExtendedData.Data pointid = None event = None elevation = None velocity = None course = None text = None type = 'tracker' for data in extended_data: if data.attrib['name'] == 'Id': pointid = int(data.value.text) elif data.attrib['name'] == 'Event': event = data.value.text.encode('utf-8') elif data.attrib['name'] == 'Elevation': elevation = data.value.text.encode('utf-8') elif data.attrib['name'] == 'Velocity': velocity = data.value.text.encode('utf-8') elif data.attrib['name'] == 'Course': course = data.value.text.encode('utf-8') elif data.attrib['name'] == 'Text': text = data.value.text if text is not None: text = text.encode('utf-8') if pointid is not None: point = Point.query(Point.pointid == pointid).get() if point is None: title = event coordinates = placemark.Point.coordinates.text.split(',') latitude = float(coordinates[1]) longitude = float(coordinates[0]) timestamp = datetime.strptime(placemark.TimeStamp.when.text, "%Y-%m-%dT%H:%M:%SZ") if text is not None: desc = text type = 'message' else: desc = '' if elevation is not None: desc = desc + "Elevation: {elevation}<br>".format(elevation=elevation) if velocity is not None: desc = desc + "Velocity: {velocity}<br>".format(velocity=velocity) if course is not None: desc = desc + "Course: {course}<br>".format(course=course) point = Point( title=title, latitude=latitude, longitude=longitude, type=type, timestamp=timestamp, pointid=pointid, desc=desc ) point.put() except AttributeError: pass except Exception as e: logging.error(e.args[0]) return Response(json.dumps({ 'status': 'ok' }), status=200, mimetype='application/json');