Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Get user ID from settings file
uid = data['user_id']
set_clipboard(scan_cites(zot_data, item_key, uid))
print prefs['format']
# If not ODT, then use `pyzotero`
else:
from pyzotero import zotero
from zq_utils import to_unicode
# Initiate the call to the Zotero API
zot = zotero.Zotero(data['user_id'], data['type'], data['api_key'])
# Return an HTML formatted citation in preferred style
ref = zot.item(item_key, content='bib', style=prefs['csl'])
uref = to_unicode(ref[0])
# Remove url, DOI, and "pp. ", if there
if prefs['csl'] != 'bibtex':
import re
uref = re.sub("(?:http|doi)(.*?)$|pp. ", "", uref)
# Export in chosen format
if prefs['format'] == 'Markdown':
import re
import html2md
from zq_utils import set_clipboard
def _get_library(self, library_id):
"""
If library id specified, fetch the group library from Zotero. Otherwise, use
the user's personal library.
"""
if library_id and library_id != 'personal':
if not self._library_client:
self._library_client = zotero.Zotero(str(library_id), 'group', self.account.oauth_key)
return self._library_client
else:
return self.client
def __init__(self, db, api_key):
self.db = db
self.api_key = api_key
self.client = zotero.Zotero(nlesc_library, library_type, self.api_key)
def get_mentions(since_version=None, keys=None):
by_version = since_version is not None
by_key = keys is not None
if by_version and by_key:
raise "Use either 'since_version' or 'keys', not both"
client = zotero.Zotero(os.environ.get('ZOTERO_LIBRARY'), 'group', os.environ.get('ZOTERO_API_KEY'))
if by_key:
items = client.everything(client.items(itemKey=keys))
logger.log(logging.INFO, 'Found %d items in Zotero library %s based on supplied key(s).' % (len(items),
os.environ.get('ZOTERO_LIBRARY')))
else:
their_last_version = client.last_modified_version()
our_last_version = get_last_version()
logger.log(logging.INFO, ('Database collection \'mention\' is currently at version %d; Zotero library %s is cur' +
'rently at version %d.') % (our_last_version, os.environ.get('ZOTERO_LIBRARY'), their_last_version))
if since_version is None:
since_version = our_last_version
items = (client.everything(client.items(since=since_version)))
logger.log(logging.INFO, 'Found %d new or updated items in Zotero library %s since version %d.' % (len(items),
os.environ.get('ZOTERO_LIBRARY'),
with open(wf.datafile(u"prefs.json"), 'r') as f:
prefs = json.load(f)
f.close()
# Create files, if necessary
if not os.path.exists(wf.cachefile(u"temp_bibliography.html")):
with open(wf.cachefile(u"temp_bibliography.html"), 'w') as f:
f.write('')
f.close()
if not os.path.exists(wf.cachefile(u"temp_bibliography.txt")):
with open(wf.cachefile(u"temp_bibliography.txt"), 'w') as f:
f.write('')
f.close()
# Initiate the call to the Zotero API
zot = zotero.Zotero(data['user_id'], data['type'], data['api_key'])
# Get the item key from the system input
item_key = wf.args[0]
# Return an HTML formatted citation in preferred style
ref = zot.item(item_key, content='bib', style=prefs['csl'])
uref = to_unicode(ref[0], encoding='utf-8')
html_ref = uref.encode('ascii', 'xmlcharrefreplace')
# Remove url, DOI, and "pp. ", if there
if prefs['csl'] != 'bibtex':
html_ref = re.sub("(?:http|doi)(.*?)$|pp. ", "", html_ref)
# Export in chosen format
if prefs['format'] == 'Markdown':
def refreshDB(self, libraryId, libraryrType, apiKey):
zotLib = zotero.Zotero(libraryId, libraryrType, apiKey)
self.refList = [i['data'] for i in zotLib.everything(zotLib.top())]
with open(libraryId + "-" + libraryrType + "-" + apiKey + ".pkl", 'wb') as f:
pickle.dump(self.refList, f)
# Get user ID from settings file
uid = data['user_id']
set_clipboard(scan_cites(zot_data, item_key, uid))
print prefs['format']
# If not ODT, then use `pyzotero`
else:
from pyzotero import zotero
from zq_utils import to_unicode
# Initiate the call to the Zotero API
zot = zotero.Zotero(data['user_id'], data['type'], data['api_key'])
# Return an HTML formatted citation in preferred style
ref = zot.item(item_key, content='citation', style=prefs['csl'])
# Remove the <span>...</span> tags and convert to Unicode
uref = to_unicode(ref[0][6:-7])
# Export in chosen format
if prefs['format'] == 'Markdown':
from dependencies import html2md
from zq_utils import set_clipboard
# Convert the HTML to Markdown
citation = html2md.html2text(uref)
if prefs['csl'] == 'bibtex':
citation = '[@' + citation.strip() + ']'
### FUNCTIONS ###
### MAIN ###
if __name__ == "__main__":
startTime = at.start_timer()
rootFolder = at.get_root_folder()
config = include.data['zotero']
at.setup_environment()
data = {}
# zotero API
library_type = 'user'
#library_type = 'group'
zot = zotero.Zotero(config['zoteroID'], library_type, config['apiKey'])
items = zot.top(limit=5)
for item in items:
print(item['data'])
at.stop_timer(startTime)
def __init__(self, apikey, userlibrary_id, usercollection_name, workmode):
self.__log = LogFile('ZoteroSync').log
self.__zot = zotero.Zotero( userlibrary_id, "user", apikey )
self.__collID = ZoteroLibs.findCollectionID( self.__zot, usercollection_name )
self.__modes = workmode
self.collateMaps()
def zotero_sync():
client = zotero.Zotero(os.environ.get('ZOTERO_LIBRARY'), 'group', os.environ.get('ZOTERO_API_KEY'))
items = (client.everything(client.items(since=get_last_version())))
logger.log(logging.INFO, str(len(items)) + ' new/updated zotero items')
items_to_save = []
project_keys = get_project_keys(client)
for item in items:
if 'title' not in item['data'] or not item['data']['title']:
continue
item_collection_keys = item['data'].get('collections', [])
if len(set.intersection(set(item_collection_keys), set(project_keys))) == 0:
logger.warning("%s is not part of a project" % item['key'])
continue