Instantly share code, notes, and snippets.

Embed
What would you like to do?
Search Hidden City Tickets using Python and Neo4j

This is the code for my project on searching Hidden City Tickets using Python and Neo4j.

Full article can be found over at https://wp.me/p1UBz7-EL

def createNonStopInboundRate(origin, destination, flight_number,dtime, atime, price, distance, final_destination):
query = '''
MERGE (orig:Airport {code: {orig}, month: {month}, day:{day}})
MERGE (dest:Airport {code: {dest}, month: {month}, day:{day}})
MERGE (f:Flight{flight_number:{flight_number}, month: {month}, day:{day}})
MERGE (m:Month{month:{month}})-[:HAS_DAY]->(d:Day{day:{day}})
WITH orig, dest, f, m, d
CREATE UNIQUE (m)-[:HAS_DAY]->(d)-[:HAS_AIRPORT{month: {month}, day:{day}}]->(orig)-[:HAS_FLIGHT{month:{month}, day:{day}, departure_time:{departure_time}}]->(f)-[:TO{month:{month}, day:{day}, departure_time: {departure_time}, arrival_time:{arrival_time}, price: {price}, duration: {duration}, distance: {distance}, final_destination: {final_destination}}]->(dest)
'''
parameters = {
'month': int(float(datetime.datetime.fromtimestamp(dtime).strftime('%m'))),
'day': int(float(datetime.datetime.fromtimestamp(dtime).strftime('%d'))),
'orig': origin,
'dest': destination,
'flight_number': flight_number,
'price': price,
'duration': (atime-dtime),
'distance': distance,
'departure_time': dtime,
'arrival_time': atime,
'final_destination':final_destination
}
try:
g = Graph(user='dbuser', password='dbpassword')
g.run(query, parameters)
except Exception:
print 'Redudant pattern ignored.'
print ("{} -> {} at {} EUR, flight code {} duration={}, departs at {} and arrivs at : {} on 2018/{}/{}".format(parameters['orig'], parameters['dest'], parameters['price'], parameters['flight_number'], parameters['duration'], parameters['departure_time'], parameters['arrival_time'], parameters['month'], parameters['day']))
def executeMultiStopQuery(origin, destination):
s = kiwi.Search()
res = s.search_flights(flyFrom=origin, to=destination, dateFrom=arrow.utcnow().shift(days=+1).format('DD/MM/YYYY'), dateTo=arrow.utcnow().shift(days=+20).format('DD/MM/YYYY'), partner='picky', directFlights=0, asc=1)
res = res.json()
for r in res['data']:
print '#'*20
flight_number = ''
for i, route in enumerate(r['route']):
if i != len(r['route']) and i != 0:
flight_number += "-"
flight_number += route['airline']+str(route['flight_no'])
print("[{}] {}{}: {} -> {}, price: {} EUR, distance: {}").format(i,route['airline'],route['flight_no'], route['flyFrom'], route['flyTo'], r['price'], r['distance'])
# Distance represents total distance!
createNonStopInboundRate(origin, route['flyTo'], flight_number, r['dTimeUTC'], route['aTimeUTC'], r['price'], r['distance'], destination)
def getConnectionAirports(hiddencity):
list_airports = []
with open('routes.csv', 'rb') as csvfile:
airportreader = csv.DictReader(csvfile)
for row in airportreader:
if row['Source Airport'] == hiddencity:
list_airports.append(row['Destination Airport'])
return list_airports
MATCH con= (:Month{month:6})-[:HAS_DAY]->(:Day{day:6})-[:HAS_AIRPORT]->(:Airport{code:'GSO'})-[r*..6]->(:Airport{code:'EWR'}) return con
MATCH con= (:Month{month:6})-[:HAS_DAY]->(:Day{day:6})-[:HAS_AIRPORT]->(:Airport{code:'GSO'})-[:HAS_FLIGHT]->(:Flight{})-[f:TO]->(:Airport{code:'EWR'}) return con order by f.price limit 1
for destination in getConnectionAirports('EWR'):
executeMultiStopQuery('GSO', destination)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment