-
Notifications
You must be signed in to change notification settings - Fork 2
/
cache.py
94 lines (73 loc) · 2.57 KB
/
cache.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
import myutils
import avia
import trains
from myjson import jsonify
import json
import codecs
def select_cities(cities):
fav_cities = [
'Москва',
'Санкт-Петербург',
'Калининград',
'Челябинск',
'Омск',
'Новосибирск',
'Тюмень',
'Владивосток'
]
new = []
for c in cities:
#if c['country_code'] == 'RU':
if c['name'] in fav_cities:
new.append(c)
return new
print('left %d cities with country_code "RU"' % len(cities))
#cache_avia(new)
#f = codecs.open('cities_ru.json', 'w', 'utf-8')
#f.writelines(json.dumps(new, indent = 2, ensure_ascii = False))
#f.close()
def cache_trains(cities):
tot = pow(len(cities),2)
f = codecs.open('trains_cache.json', 'w', 'utf-8', buffering = 0)
f.write('[')
for a in cities:
for b in cities:
if not a==b:
tries = 10
results = { }
while not 'route' in results:
results = trains.get_tickets(a['name'], b['name'], '2017-01-25')
tries -= 1
if tries <= 0:
break
tot -= 1
if 'route' in results:
print('found %d paths from %s to %s' % (len(results['route']['paths']), a['name'], b['name']), '\t', tot)
f.write(json.dumps(results['route'], ensure_ascii = False)+',\n')
else:
print('SKIPPED %s %s\n\t%s' % (a['name'], b['name'], str(results)))
f.write(']')
f.close()
def cache_avia(cities):
tot = pow(len(cities),2)
f = codecs.open('avia_cache.json', 'w', 'utf-8', buffering = 0)
f.write('[')
for a in cities:
for b in cities:
if not a==b:
tries = 10
results = { }
while not 'route' in results:
results = avia.get_tickets(a['name'], b['name'], '2017-01-25')
tries -= 1
if tries <= 0:
break
tot -= 1
if 'route' in results:
print('found %d paths from %s to %s' % (len(results['route']['paths']), a['name'], b['name']), '\t', tot)
f.write(json.dumps(results['route'], ensure_ascii = False)+',\n')
else:
print('SKIPPED %s %s\n\t%s' % (a['name'], b['name'], str(results)))
f.write(']')
f.close()