0

I have a big Bibtex, which has been created by Mendely. However, Mendeley has a habit to strangely format the journal name. As soon as I have updated one references by DOI lookup in Mendely, just the first character of the journal name has been capitalized. Any solutions as provided by Mendely for a workaround would not help here (e.g. re-writing cite style).

I am looking for a way to iterate over all my bibtex items and, if a DOI field is available for the related bibtex item, overwrite the journal name with the one from crossref.org/dx.doi.org fetching result.

Does anyone know such kind of tool/script, which is able to handle this? Is maybe someone aware of a suitable Jabref plugin?

1 Answer 1

4

Meanwhile, I haven written a small pyhton program, which exactly does what I need:

It crawls through the whole bibtexml file, retrieves the journal title from a doi source and replaces the item with the obsolete one. If the item cannot be automatically retrieved, the small script tries to perform this task by chance:

from xml.dom import minidom
import subprocess
import urllib.request
import urllib.error
import json
import codecs

xmldoc = minidom.parse('library.xml')
itemlist = xmldoc.getElementsByTagName('entry') 

print('Number of items: ' + str(itemlist.length))
print("")

for s in itemlist :
    print("Current item: " + str(s.attributes['id'].value))

    # Convert from PMID to DOI
    if len(s.getElementsByTagName('doi')) != 1 and len(s.getElementsByTagName('pmid')) >= 1:
        print("DOI not available, but PMID found...")
        pmid_s = s.getElementsByTagName('pmid')[0].firstChild.data
        req = urllib.request.Request("http://www.pmid2doi.org/rest/json/doi/" + pmid_s, method='GET', headers={'Accept': 'application/json','Content-Type': 'application/json; charset=UTF-8'})
        try:
            json_s = urllib.request.urlopen(req).read().decode("utf-8")
        except urllib.error.HTTPError as e:
            if e.code == 404:
                print("No web resources available :(")

                if len(s.getElementsByTagName('journal')) >= 1:
                    journal_node = s.getElementsByTagName('journal')
                    journal_node_s = journal_node[0].firstChild.data

                    journal_title = journal_node_s.title()
                    journal_title = journal_title.replace(" Of ", " of ")
                    journal_title = journal_title.replace(" The ", " the ")
                    journal_title = journal_title.replace(" And ", " and ")
                    journal_title = journal_title.replace(" In ", " in ")

                    journal_node[0].firstChild.data = journal_title

                    print("Fix it manually: " + journal_node_s + " --> " + journal_title)

                print("")
                continue
            else:
                raise
        json_l = json.loads(json_s)
        doi_s = json_l['doi']
        print("Convert: " + pmid_s + " --> " + doi_s)
        doi = xmldoc.createElement("doi")
        doi_text = xmldoc.createTextNode(doi_s)
        doi.appendChild(doi_text)
        s.childNodes[1].appendChild(doi)

    # Crawl data by DOI
    if len(s.getElementsByTagName('doi')) >= 1:
        doi = s.getElementsByTagName('doi')
        doi_s = doi[0].firstChild.data
        print("DOI: " + doi_s)

        req = urllib.request.Request("http://dx.doi.org/" + doi_s, headers={"Accept" : "application/vnd.crossref.unixref+xml"})
        try:
            contdoc_s = urllib.request.urlopen(req)
        except urllib.error.HTTPError as e:
            if e.code == 404:
                print("No web resources available :(")
                if len(s.getElementsByTagName('journal')) >= 1:
                    journal_node = s.getElementsByTagName('journal')
                    journal_node_s = journal_node[0].firstChild.data

                    journal_title = journal_node_s.title()
                    journal_title = journal_title.replace(" Of ", " of ")
                    journal_title = journal_title.replace(" The ", " the ")
                    journal_title = journal_title.replace(" And ", " and ")
                    journal_title = journal_title.replace(" In ", " in ")

                    journal_node[0].firstChild.data = journal_title

                    print("Fix it manually: " + journal_node_s + " --> " + journal_title)

                print("")
                continue
            else:
                raise

        contdoc = minidom.parse(contdoc_s)

        # Parse journal_metadata node from crawled data
        if len(contdoc.getElementsByTagName('journal_metadata')) >= 1:
            journal_metadata = contdoc.getElementsByTagName('journal_metadata')[0]
            journal_title = journal_metadata.getElementsByTagName('full_title')[0].firstChild.data
            print("Fetched journal title: " + journal_title)

            # Link parsed data to existing base data
            if len(s.getElementsByTagName('journal')) >= 1:
                journal_node = s.getElementsByTagName('journal')
                journal_node_s = journal_node[0].firstChild.data

                print("Update: " + journal_node_s + " --> " + journal_title)
                journal_node[0].firstChild.data = journal_title

    else:
        print("Item has neither DOI nor PMID. Skip it...")

    print("")

xml = xmldoc.toxml(encoding="utf-8")
f = open("library-out.xml", "wb")
#xmldoc.writexml(f)
f.write(xml)
f.close()

This python script is written for Python 3.x

Conversion from bibtex file to bibtexml file can be done with this tool: http://sourceforge.net/projects/bibtexml/files/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .