forked from aloverso/loanbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_tools_from_tind.py
80 lines (58 loc) · 2.13 KB
/
create_tools_from_tind.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
import os
import xml.etree.ElementTree as ET
import requests
from pymongo import MongoClient
# TODO DRY w/ other references to os.environ.get('mongo_uri')
MONGO_URI = os.environ.get('mongo_uri', 'mongodb://localhost:27017/')
# ACCESS_TOKEN = os.environ['tind_access_token']
client = MongoClient(MONGO_URI)
db = client.olinloanbot
tools = db.tindtools
class Tool(object):
def __init__(self, name, collection, resource_link, olin_id, alternate_names, image_url):
self.name = name
self.current_user = None
self.current_due_date = None
self.collection = collection
self.resource_link = resource_link
self.olin_id = olin_id
self.alternate_names = alternate_names
self.image_url = image_url
def parse_xml():
r = requests.get("https://olin.tind.io/search?ln=en&p=&f=&c=Tools&c=Media+Equipment&of=xm")
root = ET.fromstring(r.text)
for record in root:
name = ''
collection = ''
resource_link = ''
olin_id = ''
alternate_names = []
image_url = ''
for field in record:
# get record id
if field.attrib['tag'] == '001':
olin_id = field.text
# get title
if field.attrib['tag'] == '245':
name = field[0].text.lower()
# get references
if field.attrib['tag'] == '500':
resource_link = field[0].text
# get image url
if field.attrib['tag'] == '856':
image_url = field[0].text
# get type
if field.attrib['tag'] == '980':
if field[0].text != 'BIB':
collection = field[0].text
# get alternate names
if field.attrib['tag'] == '246':
alternate_names.append(field[0].text.lower())
if image_url != '':
new_tool = Tool(name, collection, resource_link, olin_id, alternate_names, image_url)
tools.insert_one(new_tool.__dict__)
def create_tools(tools_list):
for tool in tools_list:
tools.insert_one(tool.__dict__)
parse_xml()
# create_tools(tools_list)