def get_user_items(userID, public=True, startIndex=0, numItems=99): if public: zlib = zotero.Library("user", userID, '', '') else: zlib = zotero.Library("user", userID, '', get_oauth_access_token()) items = zlib.fetchItems({'limit': numItems, 'start': startIndex}) metadataTypes = [ "date", "title", "creatorSummary", "keywords", "abstractNote", "extra", "tags" ] results = {m: [] for m in metadataTypes} while len(items) == numItems: for item in items: for metadataType in metadataTypes: metadata = item.get(metadataType) if not metadata: continue try: metadata = metadata.encode("ascii", "ignore") if metadataType == "date": metadata = parse(metadata) except ValueError: print "Error encoding or parsing date with {0}".format( metadata) results[metadataType].append(metadata) startIndex += numItems items = zlib.fetchItems({'limit': numItems, 'start': startIndex}) return results
def get_user_items(request, userID, userKey, startIndex = 0, numItems = 50): zlib = zotero.Library("user", userID, '', userKey) items = zlib.fetchItems({'limit': numItems, 'start': startIndex}) metadataTypes = ["title", "creatorSummary", "keywords", "abstractSummary"] results = {m : [] for m in metadataTypes} for item in items: for metadataType in metadataTypes: metadata = item.get(metadataType) if not metadata: continue try: metadata = metadata.encode("ascii", "ignore") except (ValueError, AttributeError): print "{0} at {1}".format("error", metadata) results[metadataType].append(metadata) return results
#!/usr/bin/python import sys sys.path.append('../lib/py') import json from libZotero import zotero creds = json.loads( open('./user_writing_config.json').read()) # library credentials print(creds) #create the zotero library object which will be our interface for interacting with the Zotero API zlib = zotero.Library(creds['libraryType'], creds['libraryID'], creds['librarySlug'], creds['apiKey']) #create a new item of type book #getting a template item causes the appropriate fields to be present in the item #without doing this the item doesn't know what fields are valid for this item type #even if we do newItem = zlib.getTemplateItem('book') #set values for a couple of the valid fields newItem.set('title', 'Book 1') newItem.set('abstractNote', 'Created using a zotero python library and the write api') # make the request to the API to create the item # a Zotero Item object will be returned # if the creation went okay it will have a writeFailure property set to False createdItem = zlib.createItem(newItem) if createdItem.writeFailure != False: print(createdItem.writeFailure['code']) print(createdItem.writeFailure['message']) sys.exit(1)
""" from libZotero import zotero from urllib import urlopen import datetime from handler import Handler import os from wikiparser import WikiParser import hashlib #https://api.zotero.org/users/3949286/items?page=3&key=yrQKEJNQsAKekW9GOgGVzCBG # handler = Handler() wikiparser = WikiParser() zlib = zotero.Library('user', '3949286', '<null>', 'yrQKEJNQsAKekW9GOgGVzCBG') print zlib # retrieve the first five top-level items. #items = zlib.fetchItemsTop({'limit': 6, 'content': 'json,bib,coins'}) #for item in items: # print 'Item Type: %s | Key: %s | Title: %s ' % (item.itemType,item.itemKey, item.title) term = "Blinded_experiment" url = "https://en.wikipedia.org/wiki/" + term req = urlopen(url) #.readlines() filename = os.getcwd() + "/urlcontent.html" file = open(filename, "w") file.write(req.read()) item_type = 'encyclopediaArticle'
zotauth = OAuth1Service( name='zotero', consumer_key='<App Consumer Key>', consumer_secret='<App Consumer Secret>', request_token_url='https://www.zotero.org/oauth/request', access_token_url='https://www.zotero.org/oauth/access', authorize_url='https://www.zotero.org/oauth/authorize', base_url='https://api.zotero.org') request_token, request_token_secret = zotauth.get_request_token( method='GET', params={'oauth_callback': 'oob'}) authorize_url = zotauth.get_authorize_url(request_token) print 'Visit this URL in your browser: ' + authorize_url pin = raw_input('Enter PIN from browser: ') accessTokenResponse = zotauth.get_raw_access_token( request_token, request_token_secret, method='POST', data={'oauth_verifier': pin}) if accessTokenResponse.status_code != 200: raise Exception("Error in Zotero OAuth token exchange") accessVars = urlparse.parse_qs(accessTokenResponse.text) apiKey = accessVars['oauth_token'][0] userID = accessVars['userID'][0] username = accessVars['username'][0] zlib = zotero.Library('user', userID, username, apiKey)
try: cur.execute(sql) except MySQLdb.IntegrityError: print "*!*!*! " + wikiid + " duplicate !*!*!*" return True, most_recent except MySQLdb.ProgrammingError: print "ERROR: " + sql exit() print(item.itemKey + " => " + wikiid) return True, most_recent #Setup zoteroLib zlib = zotero.Library(zotero_creds['libraryType'], zotero_creds['libraryID'], zotero_creds['librarySlug'], zotero_creds['apiKey']) collectionKey = None hasMoreItems = True start = 0 most_recent = zotero_date if (len(sys.argv) > 1): if (sys.argv[1] == "add"): hasMoreItems = False if (len(sys.argv) > 2): zotero_item = zlib.fetchItem(sys.argv[2]) if (not zotero_item): print "Failed to retrieve zotero item for ID " + sys.argv[2]
('ó', 'ó'), ('ô', 'ô'), ('õ', 'õ'), ('ö', 'ö'), ('ø', 'ø'), ('ù', 'ù'), ('ú', 'ú'), ('û', 'û'), ('ü', 'ü'), ('ý', 'ý'), ('þ', 'þ'), ('ÿ', 'ÿ'), ('–', '–'), ('—', '—'), ('…', '…'), ('“', '“'), ('”', '”')] publicati_path = os.path.join(os.sep, os.path.dirname(os.getcwd()), '_data', 'publications') csljson_path = os.path.join(os.sep, os.getcwd(), 'csljson') # credential with open('.credential.json') as credential: c = json.loads(credential.read()) # libZotero/zotero config library = zotero.Library(c['libraryType'], c['libraryID'], c['librarySlug'], c['apiKey']) # fetch some items items = library.fetchItems({ 'collectionKey': c['collectionID'], 'content': 'json', 'sort': 'date' }) for item in items: yamlname = os.path.join(os.sep, publicati_path, item.get('date').replace('-', '')) jsonname = os.path.join(os.sep, csljson_path, item.get('date').replace('-', '')) # csljson must be fetched individually it is parsed as xml
'../lib/py') # unnecessary if libZotero is installed separately import json import time import argparse from libZotero import zotero parser = argparse.ArgumentParser( description='Add starred items from google reader to your Zotero library.') parser.add_argument('--libraryID', required=True) parser.add_argument('--libraryType', required=True) parser.add_argument('--apiKey', required=True) parser.add_argument('--starredFile', required=True) args = parser.parse_args() print args zlib = zotero.Library(args.libraryType, args.libraryID, '', args.apiKey) #create a collection for the items starredCollection = zlib.createCollection('Google Reader Starred') if starredCollection.writeFailure != False: print "Error creating collection" print starredCollection.writeFailure[ 'code'], ": ", starredCollection.writeFailure['message'] sys.exit(1) print "New Zotero collection created for google reader starred items with collectionKey ", starredCollection.get( 'collectionKey') #read the starred items from the json file and create a Zotero item for each one starredObject = json.loads(open(args.starredFile).read()) starredReaderItems = starredObject['items']