Use tempfile when downloading a file URL

This commit is contained in:
Phyks 2014-04-26 18:27:01 +02:00
parent 6e18c16010
commit 6e6418fbad
2 changed files with 13 additions and 7 deletions

View File

@ -72,6 +72,7 @@ All your documents will be stored in the papers dir specified in `params.py`. Al
## License ## 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): * "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 ## Inspiration
Here are some sources of inspirations for this project : 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. 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 * Open
* Confirmation for deletion * Confirmation for deletion
* Rebuild * 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 * Webserver interface
* Various re.compile ? * Various re.compile ?
* check output of subprocesses before it ends * check output of subprocesses before it ends

12
main.py
View File

@ -6,6 +6,7 @@ from __future__ import print_function
import fetcher import fetcher
import sys import sys
import shutil import shutil
import tempfile
import requests import requests
import subprocess import subprocess
import re import re
@ -354,13 +355,12 @@ def downloadFile(url, filetype):
pdf = fetcher.download_url(url) pdf = fetcher.download_url(url)
if pdf is not False: 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) fh.write(pdf)
new_name = addFile(params.folder+'tmp.pdf', filetype) new_name = addFile(tmp.name, filetype)
try: tmp.close()
os.remove(params.folder+'tmp.pdf')
except:
warning('Unable to delete temp file '+params.folder+'tmp.pdf')
return new_name return new_name
else: else:
warning("Could not fetch "+url) warning("Could not fetch "+url)