-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
67 lines (56 loc) · 2.03 KB
/
main.py
File metadata and controls
67 lines (56 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys, getopt
import requests
import json
import sqlite3
import datetime
import time
import os
API_URL = 'https://public-api.blablacar.com/api/v2/trips'
DB_PATH = os.path.join(os.path.dirname(__file__), 'bla.db')
def getTripsData(apiKey, fromName, toName, dateBegin = None):
headers = {'accept': 'application/json', 'key': apiKey}
params = {'fields': 'trips,seats,seats_left,departure_date,permanent_id,price', 'locale': 'en_GB', 'cur': 'EUR', 'fn': fromName, 'tn': toName}
if dateBegin:
params["db"] = str(dateBegin)
r = requests.get(API_URL, params=params, headers=headers)
try:
data = r.json()
except ValueError as err:
print str(err)
sys.exit(2)
return data["trips"]
def storeDataToSql(cityFrom, cityTo, trips):
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
# search_city_from text, search_city_to text, trip_id text, modify_date integer, total_seats integer, seats_left integer, departure_date integer, price real
for trip in trips:
departureDate = int(time.mktime(datetime.datetime.strptime(trip["departure_date"], "%d/%m/%Y %H:%M:%S").timetuple()))
data = (cityFrom, cityTo, trip["permanent_id"], int(time.time()), trip["seats"], trip["seats_left"], departureDate, trip["price"]["value"])
c.execute('insert into trips values (?,?,?,?,?,?,?,?)', data)
conn.commit()
conn.close()
def main(argv):
apiKey = ''
fromName = ''
toName = ''
dateBegin = time.strftime("%Y-%m-%d")
try:
opts, args = getopt.getopt(argv, "d:")
if len(args) < 3:
raise ValueError('Please, specify blabla car ApiKey, city-from-name and city-to-name')
apiKey = args[0]
fromName = args[1]
toName = args[2]
except getopt.GetoptError as err:
print str(err)
sys.exit(2)
except ValueError as err:
print str(err)
sys.exit(2)
for opt, arg in opts:
if opt == '-d':
dateBegin = arg
tripsData = getTripsData(apiKey, fromName, toName, dateBegin)
storeDataToSql(fromName, toName, tripsData)
print time.strftime("%Y-%m-%d %H:%M:%S -"), 'added %d entries' % len(tripsData), 'from %s to %s' % (fromName, toName)
main(sys.argv[1:])