/
minifigs.py
97 lines (76 loc) · 2.06 KB
/
minifigs.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
95
96
97
import pymongo
from pymongo import Connection
import urllib
import re
from bs4 import BeautifulSoup
conn = Connection("localhost")
db = conn.test
coll = db.Minifigs
def getURL(url):
sock = urllib.urlopen(url)
data = sock.read()
sock.close
return data
def catParser(page):
plinks = []
try:
d = getURL(page)
except:
return plinks
soup = BeautifulSoup(d)
all_links = soup.find_all('a')
for link in all_links:
pdoc = {}
if link["href"].find("catalogItem.asp?S") >= 0:
set = str(link["href"]).split("=")[1]
link = "http://www.bricklink.com/"+str(link["href"])
pdoc = {"id":set,"link":link}
plinks.append(pdoc)
return plinks
def comParser(page):
plinks = []
try:
d = getURL(page)
except:
return plinks
soup = BeautifulSoup(d)
all_links = soup.find_all('a')
for link in all_links:
pdoc = {}
if link["href"].find("catalogItem.asp?P") >= 0:
set = str(link["href"]).split("=")[1]
link = "http://www.bricklink.com/"+str(link["href"])
pdoc = {"id":set,"link":link}
plinks.append(pdoc)
return plinks
def ParseInsertData():
# file is tab delimited in this form:
# Category ID Category Name Number Name Weight (in Grams)
f = open('/Users/kennygorman/Downloads/Minifigs.txt', 'r')
for line in f:
doc = {}
line = line.rstrip()
a = line.split('\t')
doc["categoryID"] = a[0]
doc["categoryName"] = a[1]
doc["itemNumber"] = a[2]
doc["itemName"] = a[3]
doc["imageLargeURL"] = "http://www.bricklink.com/ML/%s.jpg" % (a[2])
doc["imageThumbURL"] = "http://img.bricklink.com/M/%s.gif" % (a[2])
doc["tags"] = a[1].split('/')
try:
float(a[4])
doc["itemWeightGrams"] = float(a[4])
except:
doc["itemWeightGrams"] = None
lookupPage = "http://www.bricklink.com/catalogItemIn.asp?M=%s&v=0&in=S&srt=0&srtAsc=A&ov=Y" % (a[2])
out = catParser(lookupPage)
if out:
doc["sets"] = out
lookupPage = "http://www.bricklink.com/catalogItemInv.asp?M=%s" % (a[2])
out = comParser(lookupPage)
if out:
doc["components"] = out
coll.insert(doc)
if __name__ == "__main__":
ParseInsertData()