Code cleanup, now uses temporary directories for safety
This commit is contained in:
parent
e83d954a6c
commit
d4c0779dec
@ -1,5 +1,4 @@
|
|||||||
from fanficfare import geturls
|
from fanficfare import geturls
|
||||||
import os
|
|
||||||
from os import listdir, remove, rename, utime
|
from os import listdir, remove, rename, utime
|
||||||
from os.path import isfile, join
|
from os.path import isfile, join
|
||||||
from subprocess import check_output, STDOUT
|
from subprocess import check_output, STDOUT
|
||||||
@ -7,6 +6,8 @@ import logging
|
|||||||
from optparse import OptionParser
|
from optparse import OptionParser
|
||||||
import re
|
import re
|
||||||
from ConfigParser import ConfigParser
|
from ConfigParser import ConfigParser
|
||||||
|
from tempfile import mkdtemp
|
||||||
|
from shutil import rmtree
|
||||||
|
|
||||||
logging.getLogger("fanficfare").setLevel(logging.ERROR)
|
logging.getLogger("fanficfare").setLevel(logging.ERROR)
|
||||||
|
|
||||||
@ -25,6 +26,17 @@ def parse_url(url):
|
|||||||
url = neutral.search(url).group(1)
|
url = neutral.search(url).group(1)
|
||||||
return url
|
return url
|
||||||
|
|
||||||
|
def get_files(mypath, filetype=None, fullpath=False):
|
||||||
|
ans = []
|
||||||
|
if filetype:
|
||||||
|
ans = [f for f in listdir(mypath) if isfile(join(mypath, f)) and f.endswith(filetype)]
|
||||||
|
else:
|
||||||
|
ans = [f for f in listdir(mypath) if isfile(join(mypath, f))]
|
||||||
|
if fullpath:
|
||||||
|
return [join(mypath, f) for f in ans]
|
||||||
|
else:
|
||||||
|
return ans
|
||||||
|
|
||||||
def main(user, password, server, label, inout_file, path="", ):
|
def main(user, password, server, label, inout_file, path="", ):
|
||||||
|
|
||||||
if path != "":
|
if path != "":
|
||||||
@ -43,68 +55,56 @@ def main(user, password, server, label, inout_file, path="", ):
|
|||||||
|
|
||||||
if len(urls) != 0: print "URLs to parse: {}".format(", ".join(urls))
|
if len(urls) != 0: print "URLs to parse: {}".format(", ".join(urls))
|
||||||
|
|
||||||
files = lambda x: [f for f in listdir(x) if isfile(join(x, f))]
|
loc = mkdtemp()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
for url in urls:
|
for url in urls:
|
||||||
print "Working with url {}".format(url)
|
print "Working with url {}".format(url)
|
||||||
|
storyId = None
|
||||||
try:
|
try:
|
||||||
res = check_output('calibredb search "Identifiers:{}" {}'.format(url, path), shell=True,stderr=STDOUT)
|
res = check_output('calibredb search "Identifiers:{}" {}'.format(url, path), shell=True,stderr=STDOUT)
|
||||||
storyId = res
|
storyId = res
|
||||||
print "\tStory is in calibre with id {}".format(storyId)
|
print "\tStory is in calibre with id {}".format(storyId)
|
||||||
try:
|
print "\tExporting file"
|
||||||
print "\tExporting file"
|
res = check_output('calibredb export {} --dont-save-cover --dont-write-opf --single-dir --to-dir "{}" {}'.format(storyId, loc, path), shell=True)
|
||||||
res = check_output('calibredb export {} --dont-save-cover --dont-write-opf --single-dir {}'.format(storyId, path), shell=True)
|
cur = get_files(loc, ".epub", True)[0]
|
||||||
onlyfiles = files(".")
|
print '\tDownloading with fanficfare, updating file "{}"'.format(cur)
|
||||||
for cur in onlyfiles:
|
moving=""
|
||||||
if not cur.endswith(".epub"): continue
|
|
||||||
print '\tDownloading with fanficfare, updating file "{}"'.format(cur)
|
|
||||||
res = check_output('fanficfare -u "{}" --update-cover'.format(cur), shell=True,stderr=STDOUT)
|
|
||||||
#print res
|
|
||||||
if "already contains" in res:
|
|
||||||
print "\tIssue with story, FF.net site is broken."
|
|
||||||
fp.write("{}\n".format(url))
|
|
||||||
remove(cur)
|
|
||||||
continue
|
|
||||||
elif "Story does not exist" in res:
|
|
||||||
print "\tInvalid URL"
|
|
||||||
continue
|
|
||||||
elif "more recently than Story" in res:
|
|
||||||
print "\tForcing download update\n"
|
|
||||||
res = check_output('fanficfare -u "{}" --force --update-cover'.format(cur), shell=True,stderr=STDOUT)
|
|
||||||
|
|
||||||
print "\tRemoving {} from library".format(storyId)
|
|
||||||
res = check_output('calibredb remove {} {}'.format(storyId, path), shell=True,stderr=STDOUT)
|
|
||||||
#print res
|
|
||||||
print "\tAdding {} to library".format(cur)
|
|
||||||
res = check_output('calibredb add "{}" {}'.format(cur, path), shell=True,stderr=STDOUT)
|
|
||||||
res = check_output('calibredb search "Identifiers:{}" {}'.format(url, path), shell=True, stderr=STDOUT)
|
|
||||||
print "\tAdded {} to library with id {}".format(cur, res)
|
|
||||||
#print res
|
|
||||||
remove(cur)
|
|
||||||
except Exception as e:
|
|
||||||
print "\tSomething fucked up: {}".format(e)
|
|
||||||
remove(cur)
|
|
||||||
fp.write("{}\n".format(url))
|
|
||||||
except:
|
except:
|
||||||
print "\tStory is not in calibre"
|
#story is not in calibre
|
||||||
try:
|
cur = url
|
||||||
res = check_output('fanficfare -u "{}" --update-cover'.format(url), shell=True)
|
moving = 'cd "{}" && '.format(loc)
|
||||||
#print res
|
try:
|
||||||
onlyfiles = files(".")
|
res = check_output('{}fanficfare -u "{}" --update-cover'.format(moving, cur), shell=True,stderr=STDOUT)
|
||||||
for cur in onlyfiles:
|
|
||||||
if not cur.endswith(".epub"): continue
|
if "already contains" in res:
|
||||||
try:
|
raise ValueError("Issue with story, site is broken. Story likely hasn't updated on site yet.")
|
||||||
print "\tAdding {} to library".format(cur)
|
elif "Story does not exist" in res:
|
||||||
res = check_output('calibredb add "{}" {}'.format(cur, path), shell=True, stderr=STDOUT)
|
raise ValueError("Invalid URL")
|
||||||
#print res
|
elif "more recently than Story" in res:
|
||||||
remove(cur)
|
print "\tForcing download update\n"
|
||||||
except Exception:
|
res = check_output('{}fanficfare -u "{}" --force --update-cover'.format(moving, cur), shell=True,stderr=STDOUT)
|
||||||
remove(cur)
|
cur = get_files(loc, '.epub', True)[0]
|
||||||
raise
|
except Exception as e:
|
||||||
except Exception as e:
|
print "Exception: {}".format(e)
|
||||||
print "\tSomething fucked up: {}".format(e)
|
rmtree(loc)
|
||||||
fp.write("{}\n".format(url))
|
loc = mkdtemp()
|
||||||
|
fp.write("{}\n".format(url))
|
||||||
|
continue
|
||||||
|
|
||||||
|
if storyId:
|
||||||
|
print "\tRemoving {} from library".format(storyId)
|
||||||
|
res = check_output('calibredb remove {} {}'.format(storyId, path), shell=True,stderr=STDOUT)
|
||||||
|
|
||||||
|
print "\tAdding {} to library".format(cur)
|
||||||
|
res = check_output('calibredb add "{}" {}'.format(cur, path), shell=True,stderr=STDOUT)
|
||||||
|
res = check_output('calibredb search "Identifiers:{}" {}'.format(url, path), shell=True, stderr=STDOUT)
|
||||||
|
print "\tAdded {} to library with id {}".format(cur, res)
|
||||||
|
|
||||||
|
remove(cur)
|
||||||
|
|
||||||
|
rmtree(loc)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user