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
|
###############################################################################
#
# This file is part of the Utopia Documents application.
# Copyright (c) 2008-2014 Lost Island Labs
# <info@utopiadocs.com>
#
# Utopia Documents is free software: you can redistribute it and/or modify
# it under the terms of the GNU GENERAL PUBLIC LICENSE VERSION 3 as
# published by the Free Software Foundation.
#
# Utopia Documents is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# In addition, as a special exception, the copyright holders give
# permission to link the code of portions of this program with the OpenSSL
# library under certain conditions as described in each individual source
# file, and distribute linked combinations including the two.
#
# You must obey the GNU General Public License in all respects for all of
# the code used other than OpenSSL. If you modify file(s) with this
# exception, you may extend this exception to your version of the file(s),
# but you are not obligated to do so. If you do not wish to do so, delete
# this exception statement from your version.
#
# You should have received a copy of the GNU General Public License
# along with Utopia Documents. If not, see <http://www.gnu.org/licenses/>
#
###############################################################################
#? name: FigShare
#? www: http://figshare.com/
#? urls: http://api.figshare.com/ http://figshare.com/
import common.utils
import json
import spineapi
import urllib
import utopia.document
import urllib2 #, oauth_auth, oauth
from lxml import etree
def flatten(html):
return etree.tostring(etree.HTML('<html>' + html + '</html>'), method='text', encoding='unicode')
class FigShareAnnotator(utopia.document.Annotator):
'''Connect to FigShare service.'''
def on_ready_event(self, document):
doi = common.utils.metadata(document, 'doi')
if doi is not None:
page = 1
items_retrieved = 0
api_search_url = 'http://api.figshare.com/v1/articles/search?'
query = {'search_for': doi, 'has_link': doi}
htmls = []
while True:
query['page'] = page
#handler = oauth_auth.HTTPOauthAuthHandler()
#consumer = oauth.KeySecret('yeiB61W0PYaUGPhhi8pBhA', 'LTOVWR94y8YZwscJhrFg0w')
#token = oauth.KeySecret('jsWfvZBLPgNMRMjFaQOMbgAst4Rh5LzWmTMDD4HkHOpAjsWfvZXLPgNMRMjFaQOMbg', 'm8iqNc7AQH9Yrqa6e0H5AA')
#handler.add_password(None, 'api.figshare.com', consumer, token)
#opener = urllib2.build_opener(handler)
#print api_search_url + urllib.urlencode(query)
#response = opener.open(api_search_url + urllib.urlencode(query)).read()
response = urllib2.urlopen(api_search_url + urllib.urlencode(query), timeout=8).read()
data = json.loads(response)
items = data.get('items', [])
items_found = int(data.get('items_found', 0))
# Bail if no items found
if len(items) == 0 or items_found <= 0:
break
# Bail after ten pages of stuff
if page > 10:
break
items_retrieved += len(items)
for item in items:
title = flatten(item.get('title'))
description = flatten(item.get('description'))
links = item.get('links', [])
url = item.get('url')
item_doi = item.get('DOI')
article_id = item.get('article_id')
authors = item.get('authors', [])
published_date = item.get('published_date')
type = item.get('type')
html = u'''
<div id="{article_id}" class="box">
<p>
<span class="title">{title}</span>
<span class="authors">{authors}</span>
<a href="{url}" title="Explore FigShare">[Link]</a>
</p>
<p class="readmore">
{description}
</p>
</div>
'''.format(**{
'article_id': article_id,
'title': title,
'url': item_doi,
'description': description,
'authors': u', '.join((author['author_name'] for author in authors))
})
htmls.append(html)
# Stop if we've retrieved the number of items expected
if items_retrieved >= items_found:
break
page += 1
if len(htmls) > 0:
annotation = spineapi.Annotation()
annotation['concept'] = 'FigShareReference'
annotation['property:html'] = ''.join(htmls)
annotation['property:name'] = 'FigShare'
annotation['property:description'] = 'Publicly available supplementary material'
annotation['property:sourceDatabase'] = 'figshare'
annotation['property:sourceDescription'] = '<p><a href="http://figshare.com/">fig<strong>share</strong></a> allows researchers to publish all of their research outputs in seconds in an easily citable, sharable and discoverable manner.</p>'
document.addAnnotation(annotation)
class FigShareVisualiser(utopia.document.Visualiser):
'''Visualise FigShare references.'''
def visualisable(self, annotation):
return annotation.get('concept') == 'FigShareReference'
def visualise(self, annotation):
return ('<style>#figshare .authors { font-style: italic }</style>',
'<div id="figshare">' + annotation['property:html'] + '</div>')
|