From 6e6418fbad7a618d29f2b0eb4712ba2d4e64f82b Mon Sep 17 00:00:00 2001 From: Phyks Date: Sat, 26 Apr 2014 18:27:01 +0200 Subject: [PATCH] Use tempfile when downloading a file URL --- README.md | 8 +++++++- main.py | 12 ++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 795c5d0..22a54ff 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,7 @@ All your documents will be stored in the papers dir specified in `params.py`. Al ## License +All the source code I wrote is under a `no-alcoohol beer-ware license`. All functions that I didn't write myself are under the original license and their origin is specified in the function itself. ``` * -------------------------------------------------------------------------------- * "THE NO-ALCOHOL BEER-WARE LICENSE" (Revision 42): @@ -84,6 +85,8 @@ All your documents will be stored in the papers dir specified in `params.py`. Al * --------------------------------------------------------------------------------- ``` +I used the `tearpages.py` script from sciunto, which can be found [here](https://github.com/sciunto/tear-pages) and is released under a GNU GPLv3 license. + ## Inspiration Here are some sources of inspirations for this project : @@ -97,10 +100,13 @@ Here are some sources of inspirations for this project : A list of ideas and TODO. Don't hesitate to give feedback on the ones you really want or to propose your owns. +* if doi not found ? if no doi ? +* store url of articles +* test file field for bibtex * Open * Confirmation for deletion * Rebuild -* Remove the watermarks on pdf files : First page of IOP publishing articles +* Remove the watermarks on pdf files : First page of IOP publishing articles => tearpages * Webserver interface * Various re.compile ? * check output of subprocesses before it ends diff --git a/main.py b/main.py index 9debd10..4d5296e 100755 --- a/main.py +++ b/main.py @@ -6,6 +6,7 @@ from __future__ import print_function import fetcher import sys import shutil +import tempfile import requests import subprocess import re @@ -354,13 +355,12 @@ def downloadFile(url, filetype): pdf = fetcher.download_url(url) if pdf is not False: - with open(params.folder+'tmp.pdf', 'w+') as fh: + tmp = tempfile.NamedTemporaryFile() + + with open(tmp.name, 'w+') as fh: fh.write(pdf) - new_name = addFile(params.folder+'tmp.pdf', filetype) - try: - os.remove(params.folder+'tmp.pdf') - except: - warning('Unable to delete temp file '+params.folder+'tmp.pdf') + new_name = addFile(tmp.name, filetype) + tmp.close() return new_name else: warning("Could not fetch "+url)