Download working

This commit is contained in:
Phyks 2014-05-09 23:37:17 +02:00
parent c757b1cb64
commit 681283a0db
4 changed files with 27 additions and 17 deletions

View File

@ -116,10 +116,13 @@ A list of ideas and TODO. Don't hesitate to give feedback on the ones you really
80. Search engine 80. Search engine
100. UTF-8 ? 100. UTF-8 ?
200. Webserver interface ? GUI ? (not likely for now…) 200. Webserver interface ? GUI ? (not likely for now…)
Keep multiple versions of papers
Export of bibtex
Tree à la docear ?
## Issues ? ## Issues ?
* Multiplication of {{}} * Multiplication of {{}} => solved in bibtexparser
## Thanks ## Thanks

View File

@ -8,6 +8,7 @@ import fetcher
import params import params
from bibtexparser.bparser import BibTexParser from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding from bibtexparser.customization import homogeneize_latex_encoding
from codecs import open
def getNewName(src, bibtex, tag=''): def getNewName(src, bibtex, tag=''):
@ -69,7 +70,7 @@ def bibtexAppend(data):
data is a dict for one entry in bibtex, as the one from bibtexparser output data is a dict for one entry in bibtex, as the one from bibtexparser output
""" """
try: try:
with open(params.folder+'index.bib', 'a') as fh: with open(params.folder+'index.bib', 'a', encoding='utf-8') as fh:
fh.write(parsed2Bibtex(data)+"\n") fh.write(parsed2Bibtex(data)+"\n")
except: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")
@ -80,7 +81,7 @@ def bibtexEdit(ident, modifs):
"""Update ident key in bibtex file, modifications are in modifs dict""" """Update ident key in bibtex file, modifications are in modifs dict"""
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
@ -102,7 +103,7 @@ def bibtexRewrite(data):
for entry in data.keys(): for entry in data.keys():
bibtex += parsed2Bibtex(data[entry])+"\n" bibtex += parsed2Bibtex(data[entry])+"\n"
try: try:
with open(params.folder+'index.bib', 'w') as fh: with open(params.folder+'index.bib', 'w', encoding='utf-8') as fh:
fh.write(bibtex) fh.write(bibtex)
except: except:
tools.warning("Unable to open index file.") tools.warning("Unable to open index file.")
@ -112,7 +113,7 @@ def bibtexRewrite(data):
def deleteId(ident): def deleteId(ident):
"""Delete a file based on its id in the bibtex file""" """Delete a file based on its id in the bibtex file"""
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
@ -148,7 +149,7 @@ def deleteId(ident):
def deleteFile(filename): def deleteFile(filename):
"""Delete a file based on its filename""" """Delete a file based on its filename"""
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
@ -192,7 +193,7 @@ def diffFilesIndex():
""" """
files = tools.listDir(params.folder) files = tools.listDir(params.folder)
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
index = BibTexParser(fh.read(), index = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
index_diff = index.get_entry_dict() index_diff = index.get_entry_dict()
@ -219,7 +220,7 @@ def getBibtex(entry, file_id='both'):
file_id is file or id or both to search for a file / id / both file_id is file or id or both to search for a file / id / both
""" """
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
@ -244,7 +245,7 @@ def getBibtex(entry, file_id='both'):
def getEntries(): def getEntries():
"""Returns the list of all entries in the bibtex index""" """Returns the list of all entries in the bibtex index"""
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read(), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()

View File

@ -35,7 +35,7 @@ def download(url):
return r.content, contenttype return r.content, contenttype
except requests.exceptions.RequestException: except requests.exceptions.RequestException:
tools.warning("Unable to get "+url+" using roxy "+proxy+". It " + tools.warning("Unable to get "+url+" using proxy "+proxy+". It " +
"may not be available.") "may not be available.")
continue continue
return False return False

20
main.py
View File

@ -14,6 +14,7 @@ import tools
import params import params
from bibtexparser.bparser import BibTexParser from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding from bibtexparser.customization import homogeneize_latex_encoding
from codecs import open
EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim' EDITOR = os.environ.get('EDITOR') if os.environ.get('EDITOR') else 'vim'
@ -159,8 +160,11 @@ def addFile(src, filetype, manual):
sys.exit("Unable to move file to library dir " + params.folder+".") sys.exit("Unable to move file to library dir " + params.folder+".")
# Remove first page of IOP papers # Remove first page of IOP papers
if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article': try:
tearpages.tearpage(new_name) if 'IOP' in bibtex['publisher'] and bibtex['type'] == 'article':
tearpages.tearpage(new_name)
except:
pass
backend.bibtexAppend(bibtex) backend.bibtexAppend(bibtex)
return new_name return new_name
@ -210,8 +214,8 @@ def editEntry(entry, file_id='both'):
os.path.dirname(bibtex['file'])) os.path.dirname(bibtex['file']))
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
index = BibTexParser(fh.read().encode('utf-8'), index = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
index = index.get_entry_dict() index = index.get_entry_dict()
except: except:
@ -224,13 +228,15 @@ def editEntry(entry, file_id='both'):
def downloadFile(url, filetype, manual): def downloadFile(url, filetype, manual):
print('Downloading '+url)
dl, contenttype = fetcher.download(url) dl, contenttype = fetcher.download(url)
if dl is not False: if dl is not False:
print('Download finished')
tmp = tempfile.NamedTemporaryFile(suffix='.'+contenttype) tmp = tempfile.NamedTemporaryFile(suffix='.'+contenttype)
with open(tmp.name, 'w+') as fh: with open(tmp.name, 'w+') as fh:
fh.write(dl.encode('utf-8')) fh.write(dl)
new_name = addFile(tmp.name, filetype, manual) new_name = addFile(tmp.name, filetype, manual)
tmp.close() tmp.close()
return new_name return new_name
@ -241,8 +247,8 @@ def downloadFile(url, filetype, manual):
def openFile(ident): def openFile(ident):
try: try:
with open(params.folder+'index.bib', 'r') as fh: with open(params.folder+'index.bib', 'r', encoding='utf-8') as fh:
bibtex = BibTexParser(fh.read().encode('utf-8'), bibtex = BibTexParser(fh.read(),
customization=homogeneize_latex_encoding) customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict() bibtex = bibtex.get_entry_dict()
except: except: