-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdocument_api.py
More file actions
176 lines (136 loc) · 5.4 KB
/
document_api.py
File metadata and controls
176 lines (136 loc) · 5.4 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import shutil
import requests
import xmltodict
from bs4 import BeautifulSoup
from xml.etree import ElementTree as ET
# Full documentation for this API can be found on Gallica's site: http://api.bnf.fr/api-document-de-gallica
class Document(object):
@staticmethod
def issues(id):
ISSUES_BASEURL = 'https://gallica.bnf.fr/services/Issues?ark=ark:/'
url = "".join([ISSUES_BASEURL, id, '/date'])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
print(soup)
file = open('issues.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('issues.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def issues_date(id, date):
ISSUESDATE_BASEURL = 'https://gallica.bnf.fr/services/Issues?ark=ark:/'
url = "".join([ISSUESDATE_BASEURL, id, '/date', '&date=', date])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
print(soup)
file = open('issues_date.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('issues_date.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def OAI(id):
OAI_BASEURL = 'https://gallica.bnf.fr/services/OAIRecord?ark=ark:/'
url = "".join([OAI_BASEURL, id])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('oai.xml', 'wb')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('oai.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def pagination(id):
PAGINATION_BASEURL = 'https://gallica.bnf.fr/services/Pagination?ark=ark:/'
url = "".join([PAGINATION_BASEURL, id])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('pagination.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('pagination.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def simple_images(id, res):
IMAGES_BASEURL = 'https://gallica.bnf.fr/ark:/'
url = "".join([IMAGES_BASEURL, id, '/', res])
print(url)
response = requests.get(url, stream=True)
with open('simple_image.jpg', 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file)
del response
@staticmethod
def content(id, query):
CONTENT_BASEURL = 'https://gallica.bnf.fr/services/ContentSearch?ark='
url = "".join([CONTENT_BASEURL, id, '&query=', query])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('content.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('content.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def content_page(id, query, page):
CONTENTPAGE_BASEURL = 'https://gallica.bnf.fr/services/ContentSearch?ark='
url = "".join([CONTENTPAGE_BASEURL, id, '&query=', query, '&page=', page])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('content_page.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('content_page.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def toc(id):
TOC_BASEURL = 'https://gallica.bnf.fr/services/Toc?ark='
url = "".join([TOC_BASEURL, id])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('toc.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('toc.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def texte_brut(id):
TEXTEBRUT_BASEURL = 'https://gallica.bnf.fr/ark:/'
url = "".join([TEXTEBRUT_BASEURL, id, '.texteBrut'])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
file = open('textebrut.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('textebrut.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc
@staticmethod
def ocr(id, page):
OCR_BASEURL = 'https://gallica.bnf.fr/RequestDigitalElement?O='
url = "".join([OCR_BASEURL, id, '&E=ALTO&Deb=', page])
print(url)
s = requests.get(url, stream=True)
soup = BeautifulSoup(s.content,"lxml-xml")
print(soup)
file = open('ocr.xml', 'w')
file.write(soup.prettify().encode('UTF-8'))
file.close()
with open('ocr.xml') as xml:
doc = xmltodict.parse(xml.read())
return doc