<s> import json <EOL> import math <EOL> import re <EOL> import sys <EOL> import uuid <EOL> from urllib2 import urlopen <EOL> from urllib2 import unquote <EOL> from difflib import SequenceMatcher <EOL> from os import environ as env <EOL> from mongokit import Connection <EOL> from flask import session <EOL> import wheelmap <EOL> import simple4sq as fq <EOL> fq . cred = ( env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] ) <EOL> class FoursquareVenue : <EOL> """<STR_LIT>""" <EOL> __slots__ = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def __init__ ( self , json_venue ) : <EOL> """<STR_LIT>""" <EOL> self . foursquare_id = json_venue [ '<STR_LIT:id>' ] <EOL> self . lat = json_venue [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> self . lng = json_venue [ '<STR_LIT:location>' ] [ '<STR_LIT>' ] <EOL> self . name = json_venue [ '<STR_LIT:name>' ] <EOL> ( self . xtile , self . ytile ) = osm_deg2num ( self . lat , self . lng , <NUM_LIT:16> ) <EOL> def osm_deg2num ( lat_deg , lon_deg , zoom ) : <EOL> """<STR_LIT>""" <EOL> lat_rad = math . radians ( lat_deg ) <EOL> n = <NUM_LIT> ** zoom <EOL> xtile = int ( ( lon_deg + <NUM_LIT> ) / <NUM_LIT> * n ) <EOL> ytile = int ( ( <NUM_LIT:1.0> - math . log ( math . tan ( lat_rad ) + ( <NUM_LIT:1> / math . cos ( lat_rad ) ) ) / math . pi ) / <NUM_LIT> * n ) <EOL> return ( xtile , ytile ) <EOL> def get_venues ( endpoint , page = <NUM_LIT:0> , params = { } ) : <EOL> """<STR_LIT>""" <EOL> per_page = <NUM_LIT:10> <EOL> params [ '<STR_LIT>' ] = per_page <EOL> params [ '<STR_LIT>' ] = page * per_page <EOL> fq_r = fq . request ( endpoint , params = params ) <EOL> if endpoint == '<STR_LIT>' : <EOL> items = fq_r [ '<STR_LIT:list>' ] [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> elif endpoint == '<STR_LIT>' : <EOL> items = fq_r [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> elif endpoint == '<STR_LIT>' : <EOL> items = fq_r [ '<STR_LIT>' ] [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> else : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> venues = [ ] <EOL> for item in items : <EOL> venues . append ( FoursquareVenue ( item [ '<STR_LIT>' ] ) ) <EOL> return venues <EOL> def search_wheelmap ( lat , lng , interval , name , n ) : <EOL> """<STR_LIT>""" <EOL> from_lat = lat - interval <EOL> to_lat = lat + interval <EOL> from_lng = lng - interval <EOL> to_lng = lng + interval <EOL> name = re . sub ( r'<STR_LIT>' , '<STR_LIT>' , name ) <EOL> wheelmap_client = wheelmap . Wheelmap ( env [ '<STR_LIT>' ] ) <EOL> bbox = ( from_lng , from_lat , to_lng , to_lat ) <EOL> nodes = wheelmap_client . nodes_collection ( bbox = bbox , per_page = n ) <EOL> max_name_match = <NUM_LIT:0.0> <EOL> for node in nodes : <EOL> if node . name and name : <EOL> name_match = SequenceMatcher ( None , node . name , name ) . ratio ( ) <EOL> if name_match > max_name_match : <EOL> max_node = node <EOL> max_name_match = name_match <EOL> if max_name_match > <NUM_LIT> : <EOL> return max_node <EOL> else : <EOL> return False <EOL> def get_fq_user ( ) : <EOL> """<STR_LIT>""" <EOL> user = fq . request ( '<STR_LIT>' ) [ '<STR_LIT:user>' ] <EOL> user_icon = user [ '<STR_LIT>' ] [ '<STR_LIT>' ] + '<STR_LIT>' + user [ '<STR_LIT>' ] [ '<STR_LIT>' ] <EOL> return ( user [ '<STR_LIT>' ] , user_icon ) <EOL> def json_node_search ( name , lat , lng ) : <EOL> """<STR_LIT>""" <EOL> node = search_wheelmap ( lat , lng , <NUM_LIT> , name , <NUM_LIT:200> ) <EOL> json_response = { } <EOL> if node : <EOL> json_response [ "<STR_LIT>" ] = True <EOL> json_response [ "<STR_LIT>" ] = node . node_id <EOL> json_response [ "<STR_LIT:name>" ] = node . name <EOL> json_response [ "<STR_LIT>" ] = node . lat <EOL> json_response [ "<STR_LIT>" ] = node . lng <EOL> json_response [ "<STR_LIT>" ] = node . wheelchair <EOL> json_response [ "<STR_LIT>" ] = node . wheelchair_description <EOL> json_response [ "<STR_LIT>" ] = node . website <EOL> json_response [ "<STR_LIT>" ] = node . phone <EOL> json_response [ "<STR_LIT>" ] = node . node_type <EOL> json_response [ "<STR_LIT:address>" ] = node . get_html_address ( ) <EOL> else : <EOL> json_response [ "<STR_LIT>" ] = False <EOL> return json . dumps ( json_response , indent = <NUM_LIT:4> , separators = ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) ) <EOL> def update_wheelchair_status ( node_id , wheelchair_status ) : <EOL> wheelmap_client = wheelmap . Wheelmap ( env [ '<STR_LIT>' ] if '<STR_LIT>' in env else env [ '<STR_LIT>' ] ) <EOL> return wheelmap_client . nodes_update_wheelchair ( node_id = node_id , wheelchair = wheelchair_status ) <EOL> def mongodb_get_users ( ) : <EOL> """<STR_LIT>""" <EOL> connection = Connection ( env [ '<STR_LIT>' ] , int ( env [ '<STR_LIT>' ] ) ) <EOL> if '<STR_LIT>' in env and '<STR_LIT>' in env : <EOL> connection [ env [ '<STR_LIT>' ] ] . authenticate ( env [ '<STR_LIT>' ] , env [ '<STR_LIT>' ] ) <EOL> return connection [ env [ '<STR_LIT>' ] ] . users <EOL> def fq_logged_in ( ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in session : <EOL> collection = mongodb_get_users ( ) <EOL> user = collection . find_one ( { '<STR_LIT>' : unicode ( session [ '<STR_LIT>' ] ) } ) <EOL> if user : <EOL> fq . access_token = user [ '<STR_LIT>' ] <EOL> return True <EOL> return False <EOL> def fq_add_comment ( venueId , text , url ) : <EOL> params = { '<STR_LIT:text>' : text , '<STR_LIT>' : venueId } <EOL> if url : <EOL> params [ '<STR_LIT:url>' ] = url <EOL> fq . request ( '<STR_LIT>' , params = params , method = '<STR_LIT:POST>' ) <EOL> return True <EOL> def user_login ( code ) : <EOL> """<STR_LIT>""" <EOL> users = mongodb_get_users ( ) <EOL> session_key = uuid . uuid1 ( ) <EOL> fq . download_token ( code ) <EOL> data = { '<STR_LIT>' : { '<STR_LIT>' : unicode ( session_key ) , '<STR_LIT>' : unicode ( fq . access_token ) } } <EOL> foursquare_id = fq . request ( '<STR_LIT>' ) [ '<STR_LIT:user>' ] [ '<STR_LIT:id>' ] <EOL> users . update ( { '<STR_LIT>' : unicode ( foursquare_id ) } , data , upsert = True ) <EOL> return session_key <EOL> def user_disconnect ( ) : <EOL> if '<STR_LIT>' in session : <EOL> users = mongodb_get_users ( ) <EOL> to_delete = users . find_one ( { '<STR_LIT>' : unicode ( session [ '<STR_LIT>' ] ) } ) <EOL> users . remove ( to_delete ) <EOL> session . pop ( '<STR_LIT>' , None ) <EOL> if fq . access_token : <EOL> fq . access_token = False </s>
