Import updated

* Added djvu support
* Nice mask for file renaming

TODO : Append to bibtex index + test if file already exists
This commit is contained in:
Phyks 2014-04-24 19:38:52 +02:00
parent 1420cf37a9
commit c304eb2578

43
main.py
View File

@ -35,11 +35,17 @@ def replaceAll(text, dic):
return text return text
def PDF2Doi(pdf): def findDOI(src):
pdftotext = subprocess.Popen(["pdftotext", pdf, "-"], if src.endswith(".pdf"):
stdout=subprocess.PIPE, totext = subprocess.Popen(["pdftotext", src, "-"],
stderr=subprocess.PIPE) stdout=subprocess.PIPE,
extractfull = pdftotext.communicate() stderr=subprocess.PIPE)
elif src.endswith(".djvu"):
totext = subprocess.Popen(["djvutxt", src],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
extractfull = totext.communicate()
if extractfull[1] is not "": if extractfull[1] is not "":
return False return False
@ -98,11 +104,9 @@ def addFile(src):
""" """
Add a file to the library Add a file to the library
""" """
# TODO : Handle books + djvu # TODO : Handle books (ISBN)
if src.endswith(".pdf"): # Check file already exist
doi = PDF2Doi(src) doi = findDOI(src)
elif src.endswith(".djvu"):
raise Exception("TODO")
if doi is False: if doi is False:
print("Could not determine the DOI for "+src+", switching to manual " + print("Could not determine the DOI for "+src+", switching to manual " +
@ -114,17 +118,30 @@ def addFile(src):
bibtex = doi2Bib(doi).strip().replace(',', ",\n") bibtex = doi2Bib(doi).strip().replace(',', ",\n")
bibtex = StringIO(bibtex) bibtex = StringIO(bibtex)
bibtex = BibTexParser(bibtex).get_entry_dict() bibtex = BibTexParser(bibtex).get_entry_dict()
bibtex_name = bibtex.keys()[0]
bibtex = bibtex[bibtex_name]
# TODO : Rename authors = re.split(' and ', bibtex['author'])
new_name = params.folder+"/"+doi
bibtex[bibtex.keys()[0]]['file'] = new_name new_name = params.format
new_name = new_name.replace("%f", authors[0].split(',')[0].strip())
new_name = new_name.replace("%l", authors[-1].split(',')[0].strip())
new_name = new_name.replace("%j", bibtex['journal'].replace('.', '')
.replace(' ', '_'))
new_name = new_name.replace("%Y", bibtex['year'])
new_name = new_name.replace("%t", bibtex['title'])
new_name = new_name.replace("%a", ', '.join([i.split(',')[0].strip()
for i in authors]))
new_name = params.folder+new_name+src[src.rfind('.'):]
bibtex['file'] = new_name
try: try:
shutil.copy2(src, new_name) shutil.copy2(src, new_name)
except IOError: except IOError:
sys.exit("Unable to move file to library dir " + params.folder+".") sys.exit("Unable to move file to library dir " + params.folder+".")
# TODO
bibtexAppend(bibtex) bibtexAppend(bibtex)
print("File " + src + " successfully imported.") print("File " + src + " successfully imported.")