forked from iBurnApp/iBurn-iOS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscraper.py
More file actions
158 lines (121 loc) · 4.18 KB
/
Copy pathscraper.py
File metadata and controls
158 lines (121 loc) · 4.18 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/python
# Scrapes data from the burningman website, and serializes it into json
import lxml.html
import urllib
import sys
import re
import urllib2
import json
class Pointer(object):
def __init__(self, pointer):
self.p = pointer
@property
def t(self):
return self.p.tail
def has(self, strings):
for string in strings:
if self.t.find(string) >= 0:
return string
return None
def inct(self, num=1):
for i in range(num):
self.p = self.p.getnext()
return self.p.tail
def incc(self, num=1):
for i in range(num):
self.p = self.p.getnext()
return self.p.text_content()
def _clean_string(str):
if str:
str = re.sub(r'^[\n\t\s]+', '', str)
str = re.sub(r'[\n\t\s]+$', '', str)
str = str.replace(" (dot) ", ".")
str = str.replace(" (at) ", "@")
str = re.sub(r"[\n\t\s]+\s+[\n\t\s]+", "\n\n", str)
if str.find("by ") == 0:
str = str[4:]
str = str.split(", ")
return str
class Honorarium(object):
URL = "http://www.burningman.com/installations/11_art_honor.html"
ROOT_URL = "http://www.burningman.com"
def _parse_artist(self, artist):
ret = {}
b = Pointer(artist.getnext())
ret["image_url"] = self.ROOT_URL + artist.xpath("//img")[0].get("src")
ret["title"] = b.p.text_content()
ret["artists"] = b.inct()
ret["artist_location"] = b.inct()
if not ret["artist_location"]:
b.inct()
b.inct(2)
ret["description"], ret["contact"], ret["url"] = '','',''
while b.t is not None and b.has(["Contact", "URL", "top"]) is None:
ret["description"] += b.t
data = b.inct(2)
while b.t is not None:
t = b.has(["Contact", "URL"])
if t is None:
break
ret[t.lower()] = b.incc()
if b.t is not None:
b.incc()
for key, value in ret.iteritems():
ret[key] = _clean_string(value)
return ret
def get_data(self):
root = lxml.html.parse(self.URL).getroot()
artists = root.xpath('//span[@class="imageright"]')
return [self._parse_artist(i) for i in artists]
class Camp(object):
PROXY_URL = "http://blog.burningman.com/ctrl/themecamps/"
ROOT_URL = "http://www.burningman.com"
def get_index(self):
return list("ABCDEFGHIJKLMNOPQRSTUVWXYZ#")
def _parse_camps(self, camp):
b = Pointer(camp.getnext().getchildren()[0])
ret = {}
ret["name"] = b.p.text_content()
b.inct()
ret["description"] = ""
while b.t is not None and b.has(["Hometown", "Contact", "Url"]) is None:
ret["description"] += b.t
data = b.inct()
if b.has(["Hometown"]) is not None:
ret["hometown"] = b.t[10:]
b.inct()
ret["contact"], ret["url"] = '', ''
while b.t is not None:
t = b.has(["Contact", "URL"])
if t is None:
break
ret[t.lower()] = b.incc()
if b.t is not None:
b.incc()
for key, value in ret.iteritems():
ret[key] = _clean_string(value)
return ret
def get_data(self):
opener = urllib2.build_opener()
results = []
for index in self.get_index():
url = self.PROXY_URL+"?job=getData&yy=2011&ci=%s" % index
req = urllib2.Request(url)
f = opener.open(req)
data = json.loads(f.read()[1:-1])[u"campData"]
data = data.replace("<br>", "<br/>")
root = lxml.html.fromstring(data)
camps = root.xpath('//div/div')
results.extend([self._parse_camps(i) for i in camps])
return results
if __name__ == "__main__":
if len(sys.argv) < 2 or sys.argv[1] not in ["camps", "honorarium"]:
print "Usage: scraper.py <camps|honorarium>"
sys.exit(0)
if sys.argv[1] == "camps":
Class = Camp
elif sys.argv[1] == "honorarium":
Class = Honorarium
h = Class()
data = h.get_data()
print json.dumps(data)