This repository has been archived by the owner on Jan 7, 2021. It is now read-only.
/
documentcloud.py
655 lines (546 loc) · 20.2 KB
/
documentcloud.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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
"""
Python library for interacting with the DocumentCloud API.
DocumentCloud's API can search, upload, edit and organize documents hosted
in its system. Public documents are available without an API key, but
authorization is required to interact with private documents.
Further documentation:
https://www.documentcloud.org/help/api
"""
import base64
import urllib, urllib2
from datetime import datetime
from dateutil.parser import parse as dateparser
try:
import json
except ImportError:
import simplejson as json
#
# API objects
#
class BaseAPIObject(object):
"""
An abstract version of the objects returned by the API.
"""
def __init__(self, d):
self.__dict__ = d
def __repr__(self):
return '<%s: %s>' % (self.__class__.__name__, self.__str__())
def __str__(self):
return self.__unicode__().encode("utf-8")
def __unicode__(self):
return unicode(self.title)
class Document(BaseAPIObject):
"""
A document returned by the API.
"""
def __init__(self, d):
self.__dict__ = d
self.resources = Resource(d.get("resources"))
self.created_at = dateparser(d.get("created_at"))
self.updated_at = dateparser(d.get("updated_at"))
#
# Updates
#
def put(self):
"""
Save changes made to the object to DocumentCloud.
According to DocumentCloud's docs, edits are allowed for the following
fields:
* title
* source
* description
* related_article
* access
* published_url
Returns nothing.
"""
params = dict(
title=self.title,
source=self.source,
description=self.description,
related_article=self.resources.related_article,
published_url=self.resources.published_url,
access=self.access,
)
self._connection.put('documents/%s.json' % self.id, params)
#
# Lazy loaded attributes
#
def get_contributor(self):
"""
Fetch the contributor field if it does not exist.
"""
try:
return self.__dict__[u'contributor']
except KeyError:
obj = self._connection.documents.get(id=self.id)
self.__dict__[u'contributor'] = obj.contributor
return obj.contributor
contributor = property(get_contributor)
def get_contributor_organization(self):
"""
Fetch the contributor_organization field if it does not exist.
"""
try:
return self.__dict__[u'contributor_organization']
except KeyError:
obj = self._connection.documents.get(id=self.id)
self.__dict__[u'contributor_organization'] = obj.contributor_organization
return obj.contributor_organization
contributor_organization = property(get_contributor_organization)
def get_annotations(self):
"""
Fetch the annotations field if it does not exist.
"""
try:
obj_list = self.__dict__[u'annotations']
return [Annotation(i) for i in obj_list]
except KeyError:
obj = self._connection.documents.get(id=self.id)
obj_list = [Annotation(i) for i in obj.__dict__['annotations']]
self.__dict__[u'annotations'] =obj.__dict__['annotations']
return obj_list
annotations = property(get_annotations)
def get_sections(self):
"""
Fetch the sections field if it does not exist.
"""
try:
obj_list = self.__dict__[u'sections']
return [Section(i) for i in obj_list]
except KeyError:
obj = self._connection.documents.get(id=self.id)
obj_list = [Section(i) for i in obj.__dict__['sections']]
self.__dict__[u'sections'] = obj.__dict__['sections']
return obj_list
sections = property(get_sections)
def get_entities(self):
"""
Fetch the entities extracted from this document by OpenCalais.
"""
try:
return self.__dict__[u'entities']
except KeyError:
data = self._connection.fetch("documents/%s/entities.json" % self.id).get("entities")
obj_list = []
for type, entity_list in data.items():
for entity in entity_list:
entity['type'] = entity
obj = Entity(entity)
obj_list.append(obj)
self.__dict__[u'entities'] = obj_list
return obj_list
entities = property(get_entities)
#
# Text
#
def get_full_text_url(self):
"""
Returns the URL that contains the full text of the document.
"""
return self.resources.text
full_text_url = property(get_full_text_url)
def get_full_text(self):
"""
Downloads and returns the full text of the document.
"""
req = urllib2.Request(self.full_text_url)
response = urllib2.urlopen(req)
return response.read()
full_text = property(get_full_text)
def get_page_text_url(self, page):
"""
Returns the URL for the full text of a particular page in the document.
"""
template = self.resources.page.get('text')
url = template.replace("{page}", str(page))
return url
def get_page_text(self, page):
"""
Downloads and returns the full text of a particular page in the document.
"""
url = self.get_page_text_url(page)
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return response.read()
#
# Images
#
def get_pdf_url(self):
"""
Returns the URL that contains the full PDF of the document.
"""
return self.resources.pdf
pdf_url = property(get_pdf_url)
def get_pdf(self):
"""
Downloads and returns the full PDF of the document.
"""
req = urllib2.Request(self.pdf_url)
response = urllib2.urlopen(req)
return response.read()
pdf = property(get_pdf)
def get_small_image_url(self, page=1):
"""
Returns the URL for the small sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
template = self.resources.page.get('image')
url = template.replace("{page}", str(page)).replace("{size}", "small")
return url
small_image_url = property(get_small_image_url)
def get_thumbnail_image_url(self, page=1):
"""
Returns the URL for the thumbnail sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
template = self.resources.page.get('image')
url = template.replace("{page}", str(page)).replace("{size}", "thumbnail")
return url
thumbnail_image_url = property(get_thumbnail_image_url)
def get_large_image_url(self, page=1):
"""
Returns the URL for the large sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
template = self.resources.page.get('image')
url = template.replace("{page}", str(page)).replace("{size}", "large")
return url
large_image_url = property(get_large_image_url)
def get_small_image_url_list(self):
"""
Returns a list of the URLs for the small sized image of every page.
"""
return [self.get_small_image_url(i) for i in range(1, self.pages +1)]
small_image_url_list = property(get_small_image_url_list)
def get_thumbnail_image_url_list(self):
"""
Returns a list of the URLs for the thumbnail sized image of every page.
"""
return [self.get_thumbnail_image_url(i) for i in range(1, self.pages +1)]
thumbnail_image_url_list = property(get_thumbnail_image_url_list)
def get_large_image_url_list(self):
"""
Returns a list of the URLs for the large sized image of every page.
"""
return [self.get_large_image_url(i) for i in range(1, self.pages +1)]
large_image_url_list = property(get_large_image_url_list)
def get_small_image(self, page=1):
"""
Downloads and returns the small sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
url = self.get_small_image_url(page=page)
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return response.read()
small_image = property(get_small_image)
def get_thumbnail_image(self, page=1):
"""
Downloads and returns the thumbnail sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
url = self.get_thumbnail_image_url(page=page)
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return response.read()
thumbnail_image = property(get_thumbnail_image)
def get_large_image(self, page=1):
"""
Downloads and returns the large sized image of a single page.
The page kwarg specifies which page to return. One is the default.
"""
url = self.get_large_image_url(page=page)
req = urllib2.Request(url)
response = urllib2.urlopen(req)
return response.read()
large_image = property(get_large_image)
class Project(BaseAPIObject):
"""
A project returned by the API.
"""
def get_document_list(self):
"""
Retrieves all documents included in this project.
"""
try:
return self.__dict__[u'document_list']
except KeyError:
obj_list = [self._connection.documents.get(i) for i in self.document_ids]
self.__dict__[u'document_list'] = obj_list
return obj_list
document_list = property(get_document_list)
def get_document(self, id):
"""
Retrieves a particular document from this project.
"""
obj_list = self.document_list
matches = [i for i in obj_list if str(i.id) == str(id)]
if not matches:
raise DoesNotExistError("The resource you've requested does not exist or is unavailable without the proper credentials.")
return matches[0]
class Section(BaseAPIObject):
"""
A section earmarked inside of a Document
"""
pass
class Entity(BaseAPIObject):
"""
Keywords and such extracted from the document by OpenCalais.
"""
def __unicode__(self):
return unicode(self.value)
class Annotation(BaseAPIObject):
"""
An annotation earmarked inside of a Document.
"""
def __init__(self, d):
self.__dict__ = d
def __repr__(self):
return '<%s>' % self.__class__.__name__
def __str__(self):
return self.__unicode__().encode("utf-8")
def __unicode__(self):
return u''
def get_location(self):
"""
Return the location as a good
"""
image_string = self.__dict__['location']['image']
image_ints = map(int, image_string.split(","))
return Location(*image_ints)
location = property(get_location)
class Location(object):
"""
The location of a
"""
def __repr__(self):
return '<%s>' % self.__class__.__name__
def __str__(self):
return self.__unicode__().encode("utf-8")
def __unicode__(self):
return u''
def __init__(self, top, right, bottom, left):
self.top = top
self.right = right
self.bottom = bottom
self.left = left
class Resource(BaseAPIObject):
"""
The resources associated with a Document. Hyperlinks and such.
"""
def __repr__(self):
return '<%ss>' % self.__class__.__name__
def __str__(self):
return self.__unicode__().encode("utf-8")
def __unicode__(self):
return u''
def __getattr__(self, name):
# When these attrs don't exist in the DocumentCloud db,
# they aren't included in the JSON. But we need to them
# to come out as empty strings if someone tries to call
# them here in Python.
if name in ['related_article', 'published_url']:
return ''
else:
raise AttributeError
#
# Exceptions
#
class CredentialsMissingError(Exception):
"""
Raised if an API call is attempted without the required login credentials
"""
pass
class CredentialsFailedError(Exception):
"""
Raised if an API call fails because the login credentials are no good.
"""
pass
class DoesNotExistError(Exception):
"""
Raised when the user asks the API for something it cannot find.
"""
pass
#
# API connection clients
#
class BaseDocumentCloudClient(object):
"""
Patterns common to all of the different API methods.
"""
BASE_URI = u'https://www.documentcloud.org/api/'
def __init__(self, username, password):
self.username = username
self.password = password
def put(self, method, params):
"""
Post changes back to DocumentCloud
"""
if not self.username and not self.password:
raise CredentialsMissingError("This is a private method. You must provide a username and password when you initialize the DocumentCloud client to attempt this type of request.")
# Assemble the URL
url = self.BASE_URI + method
# Prepare the params
params['_method'] = 'put'
params = urllib.urlencode(params)
# Create the request object
request = urllib2.Request(url, params)
credentials = '%s:%s' % (self.username, self.password)
encoded_credentials = base64.encodestring(credentials).replace("\n", "")
header = 'Basic %s' % encoded_credentials
request.add_header('Authorization', header)
# Make the request
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
if e.code == 404:
raise DoesNotExistError("The resource you've requested does not exist or is unavailable without the proper credentials.")
elif e.code == 401:
raise CredentialsFailedError("The resource you've requested requires proper credentials.")
else:
raise e
def fetch(self, method, params=None):
"""
Fetch an url.
"""
# Assemble the URL
url = self.BASE_URI + method
# Prepare any query string parameters
if params:
params = urllib.urlencode(params)
# Create the request object
args = [i for i in [url, params] if i]
request = urllib2.Request(*args)
# If the client has credentials, include them as a header
if self.username and self.password:
credentials = '%s:%s' % (self.username, self.password)
encoded_credentials = base64.encodestring(credentials).replace("\n", "")
header = 'Basic %s' % encoded_credentials
request.add_header('Authorization', header)
# Make the request
try:
response = urllib2.urlopen(request)
except urllib2.HTTPError, e:
if e.code == 404:
raise DoesNotExistError("The resource you've requested does not exist or is unavailable without the proper credentials.")
elif e.code == 401:
raise CredentialsFailedError("The resource you've requested requires proper credentials.")
else:
raise e
# Read the response
data = response.read()
# Convert its JSON to a Python dictionary and return
return json.loads(data)
class DocumentClient(BaseDocumentCloudClient):
"""
Methods for collecting documents
"""
def __init__(self, username, password, connection):
self.username = username
self.password = password
# We want to have the connection around on all Document objects
# this client creates in case the instance needs to hit the API
# later. Storing it will preserve the credentials.
self._connection = connection
def _get_search_page(self, query, page, per_page):
"""
Retrieve one page of search results from the DocumentCloud API.
"""
params = {
'q': query,
'page': page,
'per_page': per_page,
}
data = self.fetch(u'search.json', params)
return data.get("documents")
def search(self, query):
"""
Retrieve all objects that make a search query.
Example usage:
>> documentcloud.documents.search('salazar')
"""
page = 1
document_list = []
# Loop through all the search pages and fetch everything
while True:
results = self._get_search_page(query, page=page, per_page=1000)
if results:
document_list += results
page += 1
else:
break
obj_list = []
for doc in document_list:
doc['_connection'] = self._connection
obj = Document(doc)
obj_list.append(obj)
return obj_list
def get(self, id):
"""
Retrieve a particular document using it's unique identifier.
Example usage:
>> documentcloud.documents.get(u'71072-oir-final-report')
"""
data = self.fetch('documents/%s.json' % id).get("document")
data['_connection'] = self._connection
return Document(data)
class ProjectClient(BaseDocumentCloudClient):
"""
Methods for collecting projects
"""
def __init__(self, username, password, connection):
self.username = username
self.password = password
# We want to have the connection around on all Document objects
# this client creates in case the instance needs to hit the API
# later. Storing it will preserve the credentials.
self._connection = connection
def all(self):
"""
Retrieve all your projects. Requires authentication.
Example usage:
>> documentcloud.projects.all()
"""
if not self.username and not self.password:
raise CredentialsMissingError("This is a private method. You must provide a username and password when you initialize the DocumentCloud client to attempt this type of request.")
project_list = self.fetch('projects.json').get("projects")
obj_list = []
for proj in project_list:
proj['_connection'] = self._connection
proj = Project(proj)
obj_list.append(proj)
return obj_list
def get(self, id):
"""
Retrieve a particular project using its unique identifier.
Example usage:
>> documentcloud.projects.get(u'arizona-shootings')
"""
try:
return [i for i in self.all() if str(i.id) == str(id)][0]
except IndexError:
raise DoesNotExistError("The resource you've requested does not exist or is unavailable without the proper credentials.")
class DocumentCloud(BaseDocumentCloudClient):
"""
The public interface for the DocumentCloud API
"""
def __init__(self, username=None, password=None):
super(DocumentCloud, self).__init__(username, password)
self.documents = DocumentClient(self.username, self.password, self)
self.projects = ProjectClient(self.username, self.password, self)
if __name__ == '__main__':
"""
Ignore all this. Ad hoc testing ground as I build the API piece by piece.
"""
from pprint import pprint
from private_settings import *
public = DocumentCloud()
private = DocumentCloud(DOCUMENTCLOUD_USERNAME, DOCUMENTCLOUD_PASSWORD)
bad = DocumentCloud("Bad", "Login")
obj = private.documents.get(u'15144-mitchrpt')
print obj.resources.related_article
#print obj.title
#obj.title = 'The Mitchell Report (w00t!)'
#print obj.title
#obj.put()