-
Notifications
You must be signed in to change notification settings - Fork 0
/
scraper.py
63 lines (58 loc) · 1.31 KB
/
scraper.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
import urllib2
import sys
import time
import datetime
import pickle
import flightmod
import config
def readStrings(url):
objects = []
try:
req = urllib2.Request(url)
site = urllib2.urlopen(req)
sitestr = site.read()
start = sitestr.find(config.startstring) + len(config.startstring)
end = sitestr.find(config.endstring)
depth = 0
for i in range(start, end):
if sitestr[i] == '[':
if depth == 0:
start = i
depth += 1
if sitestr[i] == ']':
depth -= 1
if depth == 0:
end = i+1
objects.append(sitestr[start:end])
except Exception as e:
print e
return objects
def strToFlight(string):
flight = flightmod.Flight()
splits = string.split(",")
for i in range(0, len(splits[:])):
splits[i] = splits[i].strip(" ,\"[]")
flight.flight = splits[1]
flight.airline = splits[4]
flight.aircraft = splits[5]
flight.departure = int(splits[7])
flight.arrival = int(splits[8])
flight.origin = splits[11]
return flight
def readFlights(url):
flights = []
strings = readStrings(url)
for string in strings:
flights.append(strToFlight(string))
return flights
def save(flights, filename):
outfile = open(filename, "w+")
pickle.dump(flights, outfile)
outfile.close()
def load(filename):
try:
infile = open(filename, "r")
return pickle.load(infile)
infile.close()
except:
return []