Created
February 23, 2017 18:05
-
-
Save D-K-E/483cd79c55bec0e61c0f96ab78f9cce3 to your computer and use it in GitHub Desktop.
A Bulk Import Example for Zotero API using Bibtex as Data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Packages --------------------------------------- | |
import os # Needed for orienting in OS | |
import bibtexparser # Needed for parsing bibtex files | |
from bibtexparser.bparser import BibTexParser # Parses the bibtex | |
from bibtexparser.customization import convert_to_unicode # Converts latex | |
accents to unicode | |
import requests # Needed for uploading to zotero server | |
import pickle # For saving data locally | |
import json # Needed for converting data to zotero-server friendly format | |
os.chdir("PATH_TO_BIBTEX_IMPORT_FUNCTIONS/Zotero_Database") | |
from BibtexToZotero import * # Custom defined conversion functions | |
# Acquire Data ----------------------------- | |
os.chdir("PATH_TO_BIBTEX_DATABASE/Zotero_Database/BibTex") | |
with open("ref_bibs.bib", "r", encoding="utf-8") as refBib: | |
ref_bib = refBib.read() | |
B_parser = BibTexParser() | |
B_parser.customization = convert_to_unicode | |
bibs = bibtex_text_read(ref_bib) | |
# Parse Data -------------------------------------- | |
bib_list = [] | |
for bib in bibs: | |
bib_parse = bibtexparser.loads(bib.strip("\n"), parser=B_parser) | |
bib_entry = bib_parse.entries | |
bib_list.append(bib_entry) | |
for bibi in bib_list: | |
if len(bibi) == 0: | |
bib_list.remove(bibi) | |
# Filter Data -------------------------------- | |
bib_author_year_title_place = [] | |
for bibi in bib_list: | |
for bib in bibi: | |
if isinstance(bib, dict) and "address" in bib.keys() and ("author" in bib.keys() or "editor" in bib.keys()) and ("title" in bib.keys() or "booktitle" in bib.keys()) and ("year" in bib.keys() or "date" in bib.keys()): | |
bib_author_year_title_place.append(bib) | |
# Convert Data to Zotero Friendly Format ------------------- | |
zotero_bib = [bibtexTozotero(bibtex, zotero_dict={}) for bibtex in bib_author_year_title_place] | |
collection_zotero_bib = zotero_collection_map(zotero_bib, collection="IEIWC3UE") # Collection Key for Mesopotamia | |
zotero_upload_list = [] | |
for zotero_item in collection_zotero_bib: | |
if "notes" in zotero_item.keys(): | |
zotero_notes = bibtexNoteszotero(zotero_item) | |
zotero_item.pop("notes",None) | |
zotero_upload_list.append( | |
[zotero_item, zotero_notes] | |
) | |
else: | |
zotero_upload_list.append([zotero_item]) | |
# Upload Data to Zotero Server ------------------------------- | |
with open("PATH_TO_API_KEY/Zotero_Database/api/owner_api_key","r", encoding="utf-8") as apikey: | |
group_api_key= apikey.read() | |
base_url = "https://api.zotero.org" | |
library_id = "999927" # Library id of ANEEMM-G | |
library_type = "groups" | |
zotero_group_header = { | |
'Authorization':'Bearer ' + group_api_key, | |
"Zotero-API-Version": "3", | |
"Content-Type": "application/json", | |
"Zotero-Write-Token":zotero_write_token() | |
} | |
post_item_url = base_url + "/" + library_type + "/" + library_id + "/items" | |
req = requests.Session() | |
response_list = [] # We store the responses to check if there was an error afterwards | |
for zotero_bibtex in zotero_upload_list: | |
for zotero_bib in zotero_bibtex: | |
zotero_header = { | |
'Authorization':'Bearer ' + group_api_key, | |
"Zotero-API-Version": "3", | |
"Content-Type": "application/json", | |
"Zotero-Write-Token":zotero_write_token() | |
} | |
zotero_item = json.dumps([zotero_bib]) | |
response = req.post(post_item_url, data=zotero_item, headers=zotero_header) | |
response_list.append(response) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment