Skip to content

Commit e095589

Browse files
committed
Change cookies required to fetch userdata from IMDb
1 parent f89139e commit e095589

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ build/
55
dist/
66
*.spec
77

8-
*.txt
8+
*.json
99
*.zip

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,23 @@ Otherwise you can run it from the source code:
2525

2626
## How to run:
2727

28-
1. Copy your IMDb `id` cookie from your browser:
28+
1. Copy your IMDb `id` and `sid` cookies from your browser:
2929
1. Open any page on IMDb.
3030
2. Open Developer tools in your browser:
3131
- Firefox: ≡ → Web Developer → Storage Inspector<br><br>
3232
![Firefox](images/firefox.png)
3333
- Chrome: ⫶ → More tools → Developer tools → Application tab<br><br>
3434
![Chrome](images/chrome.png)
3535
3. Expand "Cookies" in the left panel and select `http://www.imdb.com`.
36-
3. Find a row named "id", double click on its cell in the "Value" column and copy it.
36+
4. Find rows named "id" and "sid", double click on their cells in the "Value" column and copy them.
3737

38-
2. Create a new file `imdb_cookie.txt` in the script directory and paste the cookie into it.
38+
2. Create a new file `imdb_cookie.json` in the script directory and paste the following data into it, replacing ellipses with your cookies from the previous step:
39+
```json
40+
{
41+
"id": "...",
42+
"sid": "..."
43+
}
44+
```
3945

4046
3. Use any of the following steps to run the tool (depending on which file you've downloaded):
4147
- Executable (`imdb_backup.exe`):

imdb_backup.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python3
2-
2+
import json
33
import os
44
import re
55
import sys
@@ -12,7 +12,7 @@
1212
import unidecode
1313
from bs4 import BeautifulSoup
1414

15-
COOKIE_FNAME = 'imdb_cookie.txt'
15+
COOKIE_FNAME = 'imdb_cookie.json'
1616
ZIP_FNAME = 'imdb_exported_lists.zip'
1717
README_REF = 'For more info check README.md.\n' \
1818
'[https://github.com/monk-time/imdb-backup-lists/blob/master/README.md]'
@@ -38,15 +38,18 @@ def load_imdb_cookies():
3838
cookie_path = script_path / COOKIE_FNAME
3939

4040
if cookie_path.exists():
41-
return {'id': cookie_path.read_text().strip()}
41+
cookies = json.loads(cookie_path.read_text())
42+
if not ('id' in cookies and 'sid' in cookies):
43+
raise ValueError(f'\n\n{COOKIE_FNAME} must contain both "id" and "sid" cookies.')
44+
return cookies
4245
else:
4346
raise FileNotFoundError(f'\n\nCreate a file "{COOKIE_FNAME}" in the script directory\n'
4447
f'and put your IMDb cookie inside.\n{README_REF}')
4548

4649

4750
def fetch_userid(cookies: dict) -> str:
4851
"""User ID is required for exporting any lists. Cookie validity will also be checked here."""
49-
r = requests.head('http://www.imdb.com/profile', cookies=cookies)
52+
r = requests.head('https://www.imdb.com/profile', cookies=cookies)
5053
r.raise_for_status()
5154
m = re.search(r'ur\d+', r.headers['Location'])
5255
if not m:
@@ -60,7 +63,7 @@ def get_fname(url: str, title: str) -> str:
6063
"""Turn an IMDb list into {LIST_OR_USER_ID}_{TITLE_SLUG}.csv."""
6164
match = re.search(r"..\d{6,}", url, re.MULTILINE)
6265
if not match:
63-
raise Exception(f'Can\'t extract list/user ID from {url} for the list "{title}"')
66+
raise Exception(f'\n\nCan\'t extract list/user ID from {url} for the list "{title}"')
6467
return match.group() + '_' + slugify(title) + '.csv'
6568

6669

0 commit comments

Comments
 (0)