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
100. UTF-8 ?
200. Webserver interface ? GUI ? (not likely for now…)
Keep multiple versions of papers
Export of bibtex
Tree à la docear ?
## Issues ?
* Multiplication of {{}}
* Multiplication of {{}} => solved in bibtexparser
## Thanks

View File

@ -8,6 +8,7 @@ import fetcher
import params
from bibtexparser.bparser import BibTexParser
from bibtexparser.customization import homogeneize_latex_encoding
from codecs import open
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
"""
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")
except:
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"""
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(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()
@ -102,7 +103,7 @@ def bibtexRewrite(data):
for entry in data.keys():
bibtex += parsed2Bibtex(data[entry])+"\n"
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)
except:
tools.warning("Unable to open index file.")
@ -112,7 +113,7 @@ def bibtexRewrite(data):
def deleteId(ident):
"""Delete a file based on its id in the bibtex file"""
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(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()
@ -148,7 +149,7 @@ def deleteId(ident):
def deleteFile(filename):
"""Delete a file based on its filename"""
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(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()
@ -192,7 +193,7 @@ def diffFilesIndex():
"""
files = tools.listDir(params.folder)
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(),
customization=homogeneize_latex_encoding)
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
"""
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(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()
@ -244,7 +245,7 @@ def getBibtex(entry, file_id='both'):
def getEntries():
"""Returns the list of all entries in the bibtex index"""
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(),
customization=homogeneize_latex_encoding)
bibtex = bibtex.get_entry_dict()

View File

@ -35,7 +35,7 @@ def download(url):
return r.content, contenttype
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.")
continue
return False

20
main.py
View File

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