From 12e207443c4be9e7abc7231fdb229b80e546cacc Mon Sep 17 00:00:00 2001 From: MK13 Date: Sat, 4 Apr 2020 23:10:15 +0200 Subject: [PATCH] A second commit --- .gitignore | 4 ++-- doir.py | 28 ++++++++++++++++------------ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index 47b5298..31870b7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,4 @@ cachefile.doir index.html -links/ -.idea/ +links +.idea diff --git a/doir.py b/doir.py index 862e4aa..2c0c77b 100755 --- a/doir.py +++ b/doir.py @@ -8,14 +8,15 @@ import socketserver parser = argparse.ArgumentParser() parser.add_argument("directory", help="directory which contains the files to resolve", type=str, default=".") -parser.add_argument("cachefile", help="path to the cache file containing already resolved DOIs", type=str, - default="cachefile.doir", nargs='?') -parser.add_argument("htmlfile", help="path to the html result file", type=str, default="index.html", nargs='?') +parser.add_argument("port", help="port on which the result will be served", type=int, default=8888, nargs='?') args = parser.parse_args() +if os.path.exists("links"): + os.remove("links") + cr = Crossref() results = {} -# Fifty days +# Fifty days in seconds - seconds * minutes * hours * days fifty_days = 60 * 60 * 24 * 50 @@ -86,20 +87,20 @@ def walk_files(dir, prefix): def write_cache(file): - with open(file, 'w') as cache_file: + with open(file, "w") as cache_file: cache_file.write(json.dumps(results)) def read_cache(file): if os.path.isfile(file): - with open(file, 'r') as cache_file: + with open(file, "r") as cache_file: return json.loads(cache_file.read()) else: return {} def write_html(file): - with open(file, 'w') as html_file: + with open(file, "w") as html_file: html_file.write("") for result in results: html_file.write("") @@ -119,13 +120,16 @@ def write_html(file): html_file.write("
TitleYearLink
") -results = read_cache(file=args.cachefile) +results = read_cache(file="cachefile.doir") walk_files(dir=args.directory, prefix=None) -write_cache(file=args.cachefile) -write_html(file=args.htmlfile) +write_cache(file="cachefile.doir") +write_html(file="index.html") -os.symlink(args.directory, 'links') +os.symlink(args.directory, "links") Handler = http.server.SimpleHTTPRequestHandler -httpd = socketserver.TCPServer(("", 8888), Handler) +httpd = socketserver.TCPServer(("", args.port), Handler) + +print("Start serving on localhost:" + args.port) + httpd.serve_forever()