Skip to content

Commit 0243f94

Browse files
committed
Add a CLI parameter for the cookie file (fix #1)
1 parent 6822007 commit 0243f94

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

imdb_backup.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,9 @@ def slugify(s: str) -> str:
3131
return s
3232

3333

34-
def load_imdb_cookies():
34+
def load_imdb_cookies(cookie_path):
3535
"""Read an IMDb 'id' cookie from the folder containing the script or executable."""
3636
# https://pyinstaller.readthedocs.io/en/stable/runtime-information.html#using-sys-executable-and-sys-argv-0
37-
script_path = Path(sys.argv[0]).resolve().parent
38-
cookie_path = script_path / COOKIE_FNAME
39-
4037
if cookie_path.exists():
4138
cookies = json.loads(cookie_path.read_text())
4239
if not ('id' in cookies and 'sid' in cookies):
@@ -122,8 +119,8 @@ def zip_all(mlists: Iterable[MList], zip_fname=ZIP_FNAME):
122119
zf.writestr('lists.txt', os.linesep.join(titles))
123120

124121

125-
def backup():
126-
cookies = load_imdb_cookies()
122+
def backup(cookie_path):
123+
cookies = load_imdb_cookies(cookie_path)
127124
userid = fetch_userid(cookies)
128125
print(f'Successfully logged in as user {userid}')
129126
mlists = fetch_lists_info(userid, cookies)
@@ -135,22 +132,29 @@ def pause_before_exit_unless_run_with_flag():
135132
136133
This will cause the script to show a standard "Press any key" prompt even if it crashes,
137134
keeping a console window visible when it wasn't launched in a terminal
138-
(e.g. by double-click the file on Windows).
135+
(e.g. by double-clicking the file on Windows).
139136
"""
140137

141138
def prompt():
142139
input('\nPress <ENTER> to exit ... ')
143140

144141
import argparse
145142
parser = argparse.ArgumentParser()
143+
# Optional positional argument for the input file with cookies
144+
145+
parser.add_argument('path', nargs='?', type=Path,
146+
default=Path(sys.argv[0]).resolve().parent / COOKIE_FNAME,
147+
help="path to the .json file with IMDb cookies")
146148
parser.add_argument('-n', '--nopause', action='store_true',
147149
help="don't pause the script before exiting")
148150

149-
if not parser.parse_args().nopause:
151+
args = parser.parse_args()
152+
if not args.nopause:
150153
import atexit
151154
atexit.register(prompt)
152155

156+
backup(cookie_path=args.path)
157+
153158

154159
if __name__ == '__main__':
155160
pause_before_exit_unless_run_with_flag()
156-
backup()

0 commit comments

Comments
 (0)