def event(event): """ Add an event to the Neo4J database. :param event: the id of the event being added to the database :return: the event node returned from the database """ print "Adding event '%s' to database..." % event['name'] e = fb.get(event['id'], fields=config.app_fields_events) # Strip place property: if 'place' in e.keys(): del e['place'] dr = db.get_node('event', 'id', e['id']) db.add_node('event', 'id', **e) if dr is None: print "Event '%s' successfully added to database." % e['name'] else: print "Event '%s' already exists. Existing record updated." % e['name'] dr = db.get_node('event', 'id', e['id']) return dr
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 band(username): """ Add a band to the Neo4J database. :param username: the username for the band being added to the database :return b: the band node returned from the database :return l: the band's location object from the facebook node, if it exists """ username = username.lower() print "Adding band '%s'..." % username r = fb.get(username, fields=config.app_fields_band) l = r.pop('location', None) dr = db.get_node('band', 'username', username) r['username'] = r['username'].lower() db.add_node('band', 'username', **r) if dr is None: print "%s successfully added to database." % r['name'] else: print "%s already exists. Existing record updated." % r['name'] dr = db.get_node('band', 'username', username) return dr, l
def city(name): """ Add a city to the Neo4J database. :param name: the name of the city being added :return c: the city node returned from the database """ c = db.get_node('city', 'name', name) if c is None: print "Adding city '%s'..." % name db.add_node('city', 'name', name=name) c = db.get_node('city', 'name', name) else: # Retrieve state, get viewport: print "city '%s' already exists, updating..." % name c = db.get_node('city', 'name', name) r = db.get_relationship(c, 'is_in', None) s = r[0].end_node() l = pushpin.get_bounds('%s, %s' % (c['name'], s['abbr'])) for key in l.keys(): c[key] = l[key] db.add_node('city', 'name', **c) return c
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_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_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 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_city(band, city): """ Create an 'is_from' relationship between a band and city. :param band: the username of the band. :param city: the name of the city. """ band = db.get_node('band', 'username', band) city = db.get_node('city', 'name', city) if not db.check_relationship(band, 'is_from'): print 'Connecting %s to %s...' % (band['name'], city['name']) db.add_relationship(band, 'is_from', city)
def event_to_band(event, band): """ create a 'featuring' relationship between an event and a band. :param event: the event dictionary. :param band: the band dictionary. """ e = db.get_node('event', 'id', event['id']) b = db.get_node('band', 'username', band['username']) if not db.check_relationship(e, 'featuring', b): print "Connecting event to %s..." % unicodedata.normalize( 'NFKD', b['name']) db.add_relationship(e, 'featuring', b)
def event_to_venue(event, venue): """ Create an 'at' relationship between an event and a venue. :param event: the event dictionary. :param venue: the venue dictionary. """ e = db.get_node('event', 'id', event['id']) v = db.get_node('venue', 'id', venue['id']) if not db.check_relationship(e, 'at'): print "Connecting event to %s..." % unicodedata.normalize( 'NFKD', v['name']) db.add_relationship(e, 'at', v)
def city_to_state(city, state): """ Create an 'is_in' relationship between a city and a state. :param city: the name of the city. :param state: the name or abbreviation of the state. """ city = db.get_node('city', 'name', city) if len(state) == 2: state = db.get_node('state', 'abbr', state) else: state = db.get_node('state', 'name', state) if not db.check_relationship(city, 'is_in'): print 'Connecting %s to %s...' % (city['name'], state['name']) db.add_relationship(city, 'is_in', state)
def venue_to_city(venue, city): """ Create an 'is_in' relationship between a venue and a city. :param venue: the venue dictionary. :param city: the city dictionary. """ if 'username' in venue.keys(): venue = db.get_node('venue', 'username', venue['username']) else: venue = db.get_node('venue', 'id', venue['id']) city = db.get_node('city', 'name', city['name']) if not db.check_relationship(venue, 'is_in'): print 'Connecting %s to %s...' % (unicodedata.normalize( 'NFKD', venue['name']), city['name']) db.add_relationship(venue, 'is_in', city)
def venue(username=None, id=None): """ Add a venue to the Neo4J database. :param username: the username/handle for the venue being added to the database :param id: the id for the venue being added to the database :return v: the venue node returned from the database :return l: the venue's location object from the facebook node """ if username is not None: username = username.lower() search_value = username search_by = 'username' else: search_value = id search_by = 'id' print "Adding venue with %s '%s'..." % (search_by, search_value) r = fb.get(search_value, fields=config.app_fields_venue) l = r.pop('location', None) if l is not None: for key in l.keys(): r[key] = l[key] dr = db.get_node('venue', search_by, search_value) if 'username' in r.keys(): r['username'] = r['username'].lower() db.add_node('venue', 'id', **r) if dr is None: print "%s successfully added to database." % r['name'] else: print "%s already exists. Existing record updated." % r['name'] dr = db.get_node('venue', search_by, search_value) return dr, l
def events_by_band(band_handle): # Check the database for the specified band (if applicable): if not db.check_node('band', 'username', band_handle): raise Exception('%s not found in the database. Import it first.' % band_handle) else: by_band = db.get_node('band', 'username', band_handle) # Get a list of all bands, excluding the one provided: bands = [band for band in db.get_all_nodes('band') if band is not by_band] # Get a list of all venues: venues = list(db.get_all_nodes('venue')) print "Scanning for events featuring %s..." % by_band['name'] # Retrieve all events from the facebook graph for the given band: events = fb.get_attr(by_band['username'], 'events', fields=config.app_fields_events) # Create an event map event_maps = [EventMap(event=event, bands=[by_band]) for event in events] # For each event in the event map... for map in event_maps: # Search for known bands and append them where found: for band in bands: if band['name'] in map.event['name']\ or ( band['name'] in map.event['description'] if 'description' in map.event else False): map.bands.append(band) # Search for known venues and add them where found: for venue in venues: if 'place' in map.event and 'id' in map.event['place'] and venue[ 'id'] == map.event['place']['id']: map.venue = venue break # For unknown venues, mark them as new and add: if not map.venue: if 'place' in map.event: map.venue = map.event['place'] map.new_venue = True # Rule out events without a venue: event_maps = [e for e in event_maps if e.venue and 'id' in e.venue.keys()] # Rule out events that are already in the database: database_event_ids = [e['id'] for e in db.get_all_nodes('event')] event_maps = [ e for e in event_maps if e.event['id'] not in database_event_ids ] # Create Event Model event_model = EventModel(event_maps, band=by_band) # Open the events explorer: try: EventsExplorer(event_model, band=by_band) except: pass # Show the results of the explorer: print "Results:" print "%i events found." % event_model.count print "%i events excluded." % (event_model.count - event_model.count_included) print "%i events to be imported." % event_model.count_included print "" # If specified, commit all events and their respective relationships: if event_model.commit_all is True: print "Adding %i events to the database:" % event_model.count_included for map in [e for e in event_model.events if e.commit is True]: # Add new venues where applicable: if map.new_venue is True: print "Venue not found in database." map.venue, l = commands.add.venue(id=map.venue['id']) c = commands.add.city(l['city']) commands.connect.venue_to_city(map.venue, c) commands.connect.city_to_state(c['name'], l['state']) e = commands.add.event(map.event) commands.connect.event_to_venue(e, map.venue) for b in map.bands: # print b['name'], b['include'] if map.bands.is_included(b): commands.connect.event_to_band(e, b) # b['include'] = True # print b['name'], b['include'] else: print "Cancelling..."
def get(self, id): return db.get_node('event', 'id', id)
def get(self, username): return db.get_node('band', 'username', username)
def get(self, name): return db.get_node('city', 'name', name)
def get(self, username): return db.get_node('venue', 'username', username)