Commit 2a646304 authored by Tiago Peixoto's avatar Tiago Peixoto
Browse files

Add bibtex generation to do_all.py

parent 3e44019b
......@@ -29,6 +29,7 @@ from process_entry import *
from analyze import *
from update_props import *
from draw import *
from get_bibtex import *
from locks import acquire_lock
......@@ -69,9 +70,13 @@ for entry in entries:
force=["vertex_properties",
"edge_properties"])
print("drawing...")
print("\tdrawing...")
for alt, g in entry.parse(cache_only=True, lazy=True):
print(f"\t{alt}...")
print(f"\t {alt}...")
draw_entry(entry, alt, thumb=True)
print("\tconverting...")
convert_entry(entry)
print("\tgenerating bibtex...")
bibtex_entry(entry)
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2020 Tiago de Paula Peixoto <tiago@skewed.de>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program 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 Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
import os
from functools import wraps
import requests
import json
import arxiv2bib
import process_entry
from locks import acquire_lock, acquire_lock_file
from util import *
cr_url = "https://api.crossref.org"
def get_url(url):
r = requests.get(url)
if r.status_code != 200:
raise ValueError(r.status_code)
return str(r.content, "utf-8")
def get_json(url):
return json.loads(get_url(url))
def get_bib(doi):
url = f"{cr_url}/works/{doi}/transform/application/x-bibtex"
return get_url(url)
def get_doi(entry, query):
query = query.replace("&", "")
qtitle = query.split('"')[1]
url = f"{cr_url}/works?query.bibliographic={query}"
ret = get_json(url)
item = ret["message"]["items"][0]
try:
title = item["title"][0]
doi = item["DOI"]
except KeyError:
return None
print("\t query:", qtitle)
print("\t title:", title)
with acquire_lock_file("title_comparison.lock", block=True) as rlock:
with open("title_comparison", "a") as f:
print(f"{entry.name}\t {qtitle}\t {title}", file=f)
return doi
def cache_bibtex(func):
@wraps(func)
def wrap(entry, cache_only=False):
base = f"{process_entry.root}/cache/bibtex/{entry.name}"
if not os.path.exists(base):
os.makedirs(base, exist_ok=True)
bibs = []
for i, cit in enumerate(entry.citation):
fname = f"{base}/{i}.bib"
if os.path.exists(fname):
with open(fname, "r") as f:
bibs.append(f.read())
else:
bibs = []
break
if len(bibs) == 0 and not cache_only:
bibs = func(entry)
for i, bib in enumerate(bibs):
fname = f"{base}/{i}.bib"
with open(fname, "w") as f:
f.write(bib)
return bibs
return wrap
def get_dummy_bib(entry, title, url):
return f"""@article{{{entry.name}}},
title = {{{title}}},
url = {{{url}}}
}}
"""
dummy_list = []
if os.path.exists("dummy_bibtex_list"):
dummy_list = open("dummy_bibtex_list", "r").read().split()
@cache_bibtex
def bibtex_entry(entry):
bibs = []
for i, cit in enumerate(entry.citation):
url = cit[1]
if entry.name in dummy_list:
bib = get_dummy_bib(entry, cit[0], cit[1])
elif url.startswith("https://doi.org"):
doi = url.replace("https://doi.org/","")
bib = get_bib(doi)
elif url.startswith("http://konect.cc"):
doi = "10.1145/2487788.2488173"
print(f"old url: {url}")
print(f"new url: https://doi.org/{doi}")
with acquire_lock_file("url_replace.lock", block=True) as rlock:
with open("url_replace", "a") as f:
print(f"{entry.name}\t{url}\thttps://doi.org/{doi}", file=f)
bib = get_bib(doi)
elif url.startswith("https://arxiv.org") or url.startswith("http://arxiv.org"):
aid = url.replace("https://arxiv.org/abs/", "")
aid = aid.replace("http://arxiv.org/abs/", "")
bib = arxiv2bib.arxiv2bib([aid])[0].bibtex() + "\n"
else:
doi = get_doi(entry, cit[0])
if doi is None:
print("DOI not found, using dummy entry")
bib = get_dummy_bib(entry, cit[0], cit[1])
else:
print(f"old url: {url}")
print(f"new url: https://doi.org/{doi}")
with acquire_lock_file("url_replace.lock", block=True) as rlock:
with open("url_replace", "a") as f:
print(f"{entry.name}\t{url}\thttps://doi.org/{doi}", file=f)
bib = get_bib(doi)
bibs.append(bib)
return bibs
if __name__ == "__main__":
if len(sys.argv) > 1:
names = sys.argv[1:]
else:
names = None
entries = process_entry.get_entries(names)
for entry in entries.values():
with acquire_lock(entry, block=False) as lock:
if lock is None:
continue
print("entry:", entry.name)
bibtex_entry(entry)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment