def band(*args): print "Adding band '%s'..." % args[0] try: r = facebook.get(args[0], fields=config.app_fields_band) l, c, s = r.pop('location', None), None, None if l is not None: c = dict(name=l['city']) s = dict(abbr=l['state']) elif 'hometown' in r: c, s = PushPin.locate(r['hometown']) while c is None or s is None: l = raw_input( 'Location for %s could not be determined. Please provide: ' % r['name']) c, s = PushPin.locate(l) if c is None or s is None: print "Invalid location" db.add_node('band', **r) if c is not None and s is not None: if not db.check_node('city', 'name', c['name']): city(c) if not db.check_relationship( db.get_node('city', 'name', c['name']), 'is_in', db.get_node('state', 'abbr', s['abbr'])): connect.city_to_state(c['name'], s['abbr']) connect.band_to_city(r['username'], c['name']) except error.types as e: error.handle(e, args[0]) else: print "%s successfully added to database." % r['name']
def city(c): print "Adding city '%s'..." % c['name'] try: db.add_node('city', **c) except error.types as e: error.handle(e, c['name']) else: print "%s successfully added to database." % c['name']
def city_to_state(city, state): print "Connecting %s to %s..." % (city, state) try: city = database.get_node('city', 'name', city) state = database.get_node('state', 'abbr', state) database.add_relationship(city, 'is_in', state) except error.types as e: error.handle(e) else: print "Done."
def band_to_band(band1, band2): print "Connecting %s and %s..." % (band1, band2) try: band1 = database.get_node('band', 'username', band1) band2 = database.get_node('band', 'username', band2) database.add_relationship(band1, 'knows', band2) except error.types as e: error.handle(e) else: print "Done."
def venue_to_city(venue, city): print "Connecting %s to %s..." % (venue, city) try: venue = database.get_node('venue', 'username', venue) city = database.get_node('city', 'name', city) database.add_relationship(venue, 'is_in', city) except error.types as e: error.handle(e) else: print "Done."
def band_to_city(band, city): print "Connecting %s to %s..." % (band, city) try: band = database.get_node('band', 'username', band) city = database.get_node('city', 'name', city) database.add_relationship(band, 'is_from', city) except error.types as e: error.handle(e) else: print "Done."
def venue(*args): print "Adding venue '%s'..." % args[0] try: r = facebook.get(args[0], **{'fields': config.app_fields_venue}) r, c, s = parse.location(r) db.add_node('venue', **r) if c is not None and s is not None: if not db.check_node('city', 'name', c): city(c) connect.city_to_state(c, s) connect.venue_to_city(r['username'], c) except error.types as e: error.handle(e, args[0]) else: print "%s successfully added to database." % r['name']
def handle_since(start_response, route): """ Handler for the /bitcoin/api/since/<timestamp>/ end-point. It returns JSON containing bitcoin price data since the timestamp specified. """ if 'timestamp' in route: timestamp = int(route['timestamp']) if timestamp <= 0: # Invalid number requested by user return error.handle(start_response, '400 Bad Request', "<i>timestamp</i> should be greater than 0.") else: return error.handle(start_response, '400 Bad Request', "<i>timestamp</i> not passed in URL.") db = common.get_db() conn = sqlite3.connect(db) cursor = conn.cursor() data = [] for values in cursor.execute('''SELECT "time", "buy", "sell" FROM "prices" WHERE "time" > ?''', (timestamp,)): ts = values[0] buy = values[1] sell = values[2] data.append({'t': ts, 'b': buy, 's': sell}) conn.close() response = json.dumps({'data': data}) start_response('200 OK', [('Content-Type', 'application/json')]) return [response]
def application(env, start_response): # We extract the URL of the request made to this server: base_uri = wsgiref.util.application_uri(env)[:-1] # We strip off the trailing / since we will need it to be part of the url we extract full_uri = wsgiref.util.request_uri(env) m = re.match(base_uri + "(/.*)", full_uri) if m: url = m.group(1) # The extracted url with the domain part removed. It is now in a form that routes can use route = router.match(url) # We use the router to match the url to the connections established earlier if route: # A route matching the incoming URL exists. We use its handler function return route['handler'](start_response, route) else: return error.handle(start_response, '400 Bad Request', "Invalid path.") else: return error.handle(start_response, '400 Bad Request', "Regex extraction from full URI failed.")
def handle(start_response, route): """ We pass in the function "start_response" which when called triggers the start of the response. """ if 'num' in route: num = int(route['num']) if num <= 0: # Invalid number requested by user return error.handle(start_response, '400 Bad Request', "<i>num</i> should be greater than 0.") else: num = 12 # The default value of num when none is specified db = common.get_db() conn = sqlite3.connect(db) cursor = conn.cursor() records = [] # Construct a list of tuples with each tuple of the format (time, buy, sell) for values in cursor.execute('''SELECT "time", "buy", "sell" FROM "prices" ORDER BY "time" DESC LIMIT ?''', (num,)): t = values[0] buy = values[1] sell = values[2] ts = common.format_time(t) records.append({'time': ts, 'buy': buy, 'sell': sell}) bi, si = maxima(records) # get the indices of the maxima records[bi]['min_buy'] = True # Append boolean values to the records corresponding to the maxima records[si]['max_sell'] = True template = common.get_template('recent.html') response = template.render({'rows': records}).encode("utf-8") conn.close() start_response('200 OK', [('Content-Type', 'text/html')]) return [response]
from py2neo import Graph, Node, Relationship import config import error try: db = Graph(password=config.db_password) except error.types as e: error.handle(e) def get_node(label, key, value): node = db.find_one(label, key, value) return node def get_child_nodes(parent_label, key, value, child_label): nodes = db.data("match (a:%s)-[]-(b:%s) where a.%s = '%s' return b" % (parent_label, child_label, key, value)) nodes = [node['b'] for node in nodes] return nodes def get_all_nodes(label): nodes = db.find(label) return nodes def get_related_nodes(left_label, right_label): nodes = db.data("match ({0}:{0})-[]-({1}:{1}) return {0}, {1}".format(left_label, right_label)) return nodes