Source

citeulike_api / src / citeulike_api / citeulike_api.py

Full commit
  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
# encoding: utf-8
"""
fetch a supplied user's bookmarks somewhere, optionally writing them out into a file
"""
from string import Template
import re
import codecs
from string import capwords
from copy import deepcopy
import importlib
try:
    import simplejson as json
except ImportError:
    import json
from calendar import month_name
from util import get_code_revision
import time
import mechanize
import os.path
import hashlib
import openmeta
import glob

def sha1_digest(userfiles):
    """return a hex digest of some bytes"""
    return hashlib.sha1(userfiles).hexdigest()
    
#from the CUL webform
# <option value="BOOK" >Book</option> 
# <option value="CHAP" >Book chapter/section</option> 
# <option value="PAMP" >Booklet</option> 
# <option value="INCOL" >Book part (with own title)</option> 
# <option value="INCONF" >Conference proceedings (article)</option> 
# <option value="CONF" >Conference proceedings (whole)</option> 
# <option value="DATA" >Data file</option> 
# <option value="ELEC" >Electronic citation</option> 
# <option value="INPR" >In the press</option> 
# <option value="JOUR" selected="selected">Journal article</option> 
# <option value="MANUAL" >Manual (technical documentation)</option> 
# <option value="GEN" >Miscellaneous</option> 
# <option value="REP" >Technical report</option> 
# <option value="STD" >Technical Standard</option> 
# <option value="MTHES" >Thesis (Master's)</option> 
# <option value="THES" >Thesis (PhD)</option> 
# <option value="UNPB" >Unpublished work</option> 

CUL_FIELDS = {
  'JOUR': 'article', 
  'BOOK': 'book', 
  'PAMP': 'booklet', 
  'CHAP': 'inbook', 
  'INCOL': 'incollection', 
  'INCONF': 'inproceedings', 
  'MANUAL': 'manual', 
  'MTHES': 'mastersthesis', 
  'THES': 'phdthesis', 
  'CONF': 'proceedings', 
  'REP': 'techreport', 
  'STD': 'techreport', 
  'UNPB': 'unpublished', 
  # '?patent': 'patent',
  # '?collection': 'collection',
  'ELEC': 'electronic',
  'GEN': 'misc',
  'DATA': 'misc',
  'INPR': 'misc',
}

FIELDS_THAT_GET_SPURIOUS_BRACES = {
  "journal": "journal",
  "authors": "authors",
  "abstract": "abstract",
  "title": "title",
  "location": "location",
  "address": "address",
  "booktitle": "title_secondary"
}

#anglocentric stopwords
STOP_WORDS = set([
  'In', 'To', 'For', 'At', 'With', 'A', 'An', 'And', 'The', 'On', 'By', 'Of'
])

_edit_priority_url = Template(
  "http://www.citeulike.org/editpriority.json?"
  "user_article_id=579067&article_id=1137810&"
  "to_read=4&callback=jsonp1282733494417"
)
_all_bibtex_t = Template(
  "http://www.citeulike.org/bibtex/user/${username}"
  "?key_type=4&clean_urls=0"
)
_all_json_t = Template(
  "http://www.citeulike.org/json/user/${username}"
)
_article_edit_t = Template(
  "http://www.citeulike.org/edit_article_details?"
  "username=${username}&article_id=${article_id}"
)
_article_view_t = Template(
  "http://www.citeulike.org/user/${username}/article/${article_id}"
)
_attachment_t= Template(
  "http://www.citeulike.org${path}"
)

_base_url = 'http://www.citeulike.org/'

_cul = None

class CulError(Exception):
    pass

# handy filters
def BRACE_IN_TITLE(rec):
    return rec['title'].find('{')>=0

def BRACE_IN_QUESTIONABLE_LOCATION(rec):
    field_vals = []
    for field in FIELDS_THAT_GET_SPURIOUS_BRACES:
        field_val = rec.get(field)
        if field_val is None: continue
        if isinstance(field_val, basestring):
            field_vals.append(field_val)
        else:
            for sub_val in field_val:
                field_vals.append(sub_val)
    return any([
      field_val.find('{')>=0
      for field_val in field_vals
    ])

def HAS_SHOUT_CAPS(rec):
    return rec['title']==rec['title'].upper()

def get_browser(debug=False):
    """create a browser, optionally with debugging enabled"""
    import socket
    socket.setdefaulttimeout(20.0)
    import mechanize
    
    browser = mechanize.Browser()
    
    if debug:
        import logging
        import sys
        # Log information about HTTP redirects and Refreshes.
        browser.set_debug_redirects(True)
        # Log HTTP response bodies (ie. the HTML, most of the time).
        browser.set_debug_responses(True)
        # Print HTTP headers.
        browser.set_debug_http(True)

        # To make sure you're seeing all debug output:
        logger = logging.getLogger("mechanize")
        logger.addHandler(logging.StreamHandler(sys.stdout))
        logger.setLevel(logging.INFO)
    return browser

def fetch_all_bibtex(username="livingthingdan", password=None, filepath=None):
    """fetch a whole library in bibtex format.
    
    we should probably do this authenticated using the below CUL client class"""
    
    cul = get_cul(username, password)
    return cul.download_bibtex(dumpfile=filepath)

def fetch_all_json(username="livingthingdan", password=None, filepath=None):
    """fetch a whole library in JSON format."""
    cul = get_cul(username, password)
    return cul.download_json_index(json_cache=filepath)

def tokenise(f):
    return [t for t in re.split(r'[-\s]+', f) if t]
    
def topwords_iter(f):
    """yield non-stopword tokens """
    for t in tokenise(f):
        t = stripped_word(t)
        if t not in STOP_WORDS:
            yield t

def topwords(f):
    """return non-stopword tokens"""
    return list(topwords_iter(f))

def get_sirname(creators):
    return stripped_word(tokenise(creators[0])[-1])
    
def stripped_word(w):
    """remove punctuation and return a word initial-capped"""
    return capwords(re.sub(r'[^\w]*', '', w))

def strip_html_tags(value):
    """Returns the given HTML with all tags stripped."""
    return re.sub(r'<[^>]*?>', '', unicode(value))

def create_key(obj):
    """create a citeulike-like AuthorYearTitle key from a record.
    probably not the same as theirs, which handles name prefixes better."""
    key = []
    creators = obj.get('authors', obj.get('editors', ['Anonymous']))
    key.append(get_sirname(creators))
    if obj.get('published', False):
        key.append(obj['published'][0])
    key.append(topwords_iter(obj['title']).next())
    return ''.join(key)
    
def strip_wrapping_braces(field):
    #delete leading space and braces
    field = re.sub(r'^[{\s]*', '', field)
    #delete trailing space and braces
    return re.sub(r'[}\s]*$', '', field)

def strip_braces(field):
    return field.replace('{', '').replace('}', '')

def strip_tex_nonsense(ustring):
    ustring = ustring.replace('\\', '')
    ustring = ustring.replace(r'{', '')
    ustring = ustring.replace(r'}', '')
    ustring = ustring.replace(r'"', '')
    return ustring
    
def open_article_for_edit(article_id, username, password):
    # from subprocess import popen
    import webbrowser
    import urlparse
    cul = get_cul(username, password)
    edit_path = cul.get_edit_url(article_id)
    edit_url = urlparse.urljoin(_base_url, edit_path)
    webbrowser.open_new_tab(edit_url)

def get_cul(username, *args, **kwargs):
    """return the last browser used, or create a new one with the supplied
    pasword"""
    global _cul
    if not _cul: 
        _cul = CiteULike(username, *args, **kwargs)
    return _cul
    
def auto_edit_article(article_id, username, password):
    cul = get_cul(username, password)
    cul.kill_cite_key(article_id)

def clean_article(obj):
    """returns a record dict with certain CUL fuckups corrected."""
    obj = deepcopy(obj)
    for creator_field in ('authors', 'editors'):
        creator_list = obj.get(creator_field, None)
        if not creator_list: continue
        obj[creator_field] = [strip_braces(n) for n in creator_list]
    return obj

class CiteULike(object):
    
    MIN_API_WAIT = 5
    
    def __init__(self, username,
          password=None,
          json_cache=None,
          attachment_path=None,
          debug=False,):
        """set up an object to access CUL
        If you omit username but suply the path to a acached copy of the JSON output, then you can still do useful things in an "offline mode"
        """
        # first set up the socket handling to a value reflective of CiteULike's
        # somewhat frequent, er, network difficulties
        
        #set up state for anonymous use
        self.username = username
        self.logged_in = False
        self.debug = debug
        
        browser = get_browser(debug=debug)
        browser.set_handle_robots(False)
        user_agent_string = "CiteULikeApi %s" % get_code_revision()
        browser.addheaders = [
          ("User-agent", user_agent_string),
        ]
        self.browser = browser
        
        self.last_api_access = time.time() - self.MIN_API_WAIT
        
        #now, if password is provided, log in
        if password is not None:
            self.login(username, password)
        
        # set up record cache
        self.records = []
        self.json_cache = json_cache
        
        # if we ARE set up to use a cache file, try to load its contents now
        if json_cache:
            self.load_json_from_cache(json_cache)
        
        self.attachment_path = attachment_path
        if attachment_path is not None:
            self.attachment_path = os.path.realpath(attachment_path)
        
        if openmeta.is_openmeta_working():
            self.tagger = openmeta.set_tags
        else:
            def _dummy_tagger(*args, **kwargs):
                pass
            self.tagger = _dummy_tagger
    
    def wait_for_api_limit(self, min_wait=0):
        min_wait = max(min_wait, self.MIN_API_WAIT)
        now = time.time()
        elapsed_time = now - self.last_api_access
        if elapsed_time<min_wait:
            time.sleep(min_wait-elapsed_time)
        self.last_api_access = time.time()
    
    def login(self, username, password):
        browser = self.browser
        
        browser.open('http://www.citeulike.org/login?from=/')
        self.wait_for_api_limit()
        
        browser.select_form(name='frm')
        browser['password'] = password
        browser['username'] = username
        
        self.wait_for_api_limit()
        
        try:
            #handle redirects manually to avoid connection flakiness
            browser.set_handle_redirect(False)
            resp = browser.submit()
        except mechanize.HTTPError, e:
            if e.getcode()!=302 : raise e
            next_page = e.info().getheader('Location')
            if next_page == 'http://www.citeulike.org/' :
                #success
                self.logged_in = True
            elif next_page.find('status=login-failed')>=0:
                raise CulError('Login Failed')
            else:
                err = CulError('Unknown login response')
                err.data = e
                raise err
        finally:
            browser.set_handle_redirect(True)
        
    def get_edit_url(self, article_id):
        return _article_edit_t.substitute(
          username = self.username,
          article_id=article_id)
        
    def cache_records(self, force_update=False):
        """update myself with all relevant records for rapid searching"""
        if not self.records or force_update:
            self.records = self.download_json_index()
        self.article_id_index()
        return self.records
    
    def render(self, renderer_module='bibtex', ids=None,
            attachment_path=None, *args, **kwargs):
        self._check_index()
        if attachment_path is None: attachment_path=self.attachment_path
        renderer_module = importlib.import_module(
          'citeulike.output.' + renderer_module)
        renderer = renderer_module.Renderer(
            attachment_path=attachment_path,
            *args, **kwargs)
        if ids is None:
            records = self.records
        else:
            records = tuple(self.with_ids(ids))
        return renderer.render(records)
        
    def with_ids(self, ids):
        ids = set([unicode(i) for i in ids])
        for id_ in ids:
            yield self.article_lookup[id_]
                
    def download_json_index(self, json_cache=None):
        """load the index JSON, optionally caching it locally"""
        browser = self.browser
        self.wait_for_api_limit()
        content = browser.open(
          _all_json_t.substitute(username = self.username)
          ).read()
        # Apparently we get utf-8 bytes back and have to decode to avoid ascii
        # roundtripping
        # it would be nice to use a stream decoder for this, but codecs doesn't
        # like mechanize
        content = codecs.decode(content, 'utf-8')
        
        json_cache = json_cache or self.json_cache
        if json_cache:
            with codecs.open(json_cache, 'w', encoding='utf-8') as f:
                f.write(content)
        return json.loads(content)
        
    def load_json_from_cache(self, json_cache):
        self.json_cache = json_cache
        with codecs.open(json_cache, 'r', encoding='utf-8') as f:
            self.records = json.load(f)
        self.article_id_index()
    
    def article_id_index(self):
        article_lookup = {}
        for i in xrange(len(self.records)):
            article_lookup[self.records[i]['article_id']] = self.records[i]
        self.article_lookup = article_lookup
        
    def download_bibtex(self, username = None, dumpfile=None):
        """Download the CUL bibtex.
        This is highly likely to be malformed. You'd be better off using the
        bibtex_output methods"""
        browser = self.browser
        username = username or self.username
        content = browser.open(
          _all_bibtex_t.substitute(username = username)
        )
        if dumpfile:
            with codecs.open(dumpfile, 'w', encoding='utf-8') as f:
                f.write(content)
        return content
        
    def _check_index(self):
        """do we have records laoded?"""
        if not self.records:
            raise CulError("Empty record index. Have you run "
              "load_json_from_cache or cache_records?")
         
    def bulk_apply(self, filt=None, method_names=None, method_args=None):
        self._check_index()
        
        #what to do
        if not method_names:
            method_names=['kill_cite_key']
        methods = [getattr(self, mn) for mn in method_names]
        
        if not method_args:
            method_args = {}
        
        #what to do it to
        if filt is None:
            filt = lambda x: True
        
        for rec in self.records:
            if not filt(rec): continue
            print "Batching record %s, '%s'" % (
              rec['article_id'], rec['title'])
                  
            for fn, fn_name in zip(methods, method_names):
                print "applying %s" % fn_name
                for attempt in range(4):
                    try:
                        fn(int(rec['article_id']), **method_args)
                        break
                    except IOError, e:
                        print "network problem", unicode(e)
                        if hasattr(e, 'getcode'):
                            print "HTTP code", e.getcode()
                        wait = 5*(2**attempt-1)
                        print "retrying in %d seconds" % wait
                        self.wait_for_api_limit(wait)
                        
        
    def go_to_article_form(self, article_id, fields=None):
        """point our invisible browser at the artcle change form"""
        browser = self.browser
        self.wait_for_api_limit()
        browser.open(self.get_edit_url(article_id))

        browser.select_form(
          predicate=lambda form: form.attrs.get('id')=='article'
        )
        return browser
    
    def submit_article_form(self):
        self.wait_for_api_limit()
        resp = self.browser.submit()
        if not resp.geturl().startswith('http://www.citeulike.org/user/'):
            raise CulError("some kind of form failure")
    
    #### Particular handy things i need to do often
    def kill_cite_key(self, article_id):
        """blank out the bibtex key in case you imported or copied the article
        from someone else who uses a convention you don't like.
        Less important now that CUL export multiple cite keys."""
        browser = self.go_to_article_form(article_id)
        browser.form['bibtex_import_cite'] = ""
        self.wait_for_api_limit()
        self.submit_article_form()
    
    def kill_braces(self, article_id, fields=None):
        """purge the braces that CUL wraps fields with sometimes"""
        if fields is None:
            fields = FIELDS_THAT_GET_SPURIOUS_BRACES
        print 'checking %s for braces' % article_id
        browser = self.go_to_article_form(article_id)
        for json_name, form_name in fields.iteritems():
            questionable_content = browser.form[form_name]
            better_content = '\r\n'.join([
               strip_wrapping_braces(line) for line in questionable_content.splitlines()
            ])
            browser.form[form_name] = better_content
            print '  updating %s' % json_name 
        self.wait_for_api_limit()
        self.submit_article_form()
    
    def fix_title_case(self, article_id):
        from titlecase import titlecase
        """PURGE SHOUT CASE TITLE!!!1!"""
        print 'updating %s for silly title caps' % article_id
        browser = self.go_to_article_form(article_id)
        questionable_content = browser.form['title']
        browser.form['title'] = titlecase(questionable_content)
        print '  updating "%s"' % questionable_content 
        self.wait_for_api_limit()
        self.submit_article_form()

    def download_attachments(self, article_id,
            attachment_path=None,
            force=False,
            sync_tags=True):
        """sync my pdfs etc to disk from a given article"""
        browser = self.browser
        article_id = unicode(article_id)
        if attachment_path:
            attachment_path = os.path.realpath(attachment_path)
        else:
            attachment_path = self.attachment_path
        article_meta = self.article_lookup[article_id]
        attachments = article_meta.get('userfiles', [])
        for attachment in attachments:
            local_path = os.path.join(attachment_path, attachment["name"])
            print "inspecting local path %s" % os.path.abspath(local_path)
            remote_url = _attachment_t.substitute(path=attachment["path"])
            #if the force flag is on, let's DO it
            do_download = force
            # elsewise, check if we need to
            if not force:
                if not os.path.exists(local_path):
                    print "local path %s does not exist. downloading" % \
                      os.path.abspath(local_path)
                    do_download = True
                else: #Is this sane? overwrites local annotations
                    local_sha1 = sha1_digest(open(local_path, 'rb').read())
                    if attachment['sha1'] != local_sha1:
                        do_download = True
                        print "local path SHA %s does not equal remote SHA %s" \
                          " Downloading." % \
                          (local_sha1, attachment['sha1'])
                          
            if do_download:
                remote_path = _attachment_t.substitute(path=attachment['path'])
                self.wait_for_api_limit()
                content = browser.open(remote_path).read()
                with open(local_path, 'wb') as f:
                    f.write(content)
            else:
                print "...but no need to download this one"
            
            if sync_tags:
                self.tagger(local_path, article_meta.get('tags', []))
                
    def delete_unknown_attachments(self, attachment_path=None):
        """for now, this only handles orphan PDFs"""
        if attachment_path:
            attachment_path = os.path.realpath(attachment_path)
        else:
            attachment_path = self.attachment_path
        known_attachments = set()
        for record in self.records:
            for att in record.get('userfiles', []):
                known_attachments.add(
                  os.path.abspath(
                    os.path.join(
                      attachment_path, 
                      att['name']
                    )
                  )
                )
        all_attachments = set(glob.glob(
          os.path.join(attachment_path, '*.pdf')
        ))
        
        for unknown_attachment in (all_attachments-known_attachments):
            print "unknown attachment '%s'" % unknown_attachment
            os.remove(os.path.abspath(unknown_attachment))