Created
June 9, 2021 00:30
-
-
Save marethyu/3045ce16a9ee52e4ef213f9af0b18886 to your computer and use it in GitHub Desktop.
Mangadex bookmarks fixer for Chrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Mangadex bookmarks fixer for Chrome | |
| # | |
| # How to use: | |
| # 1. Chrome -> Options (find the button with three dots) -> Bookmarks -> Bookmark manager -> Options -> Export bookmarks | |
| # 2. Save the html file to the directory where you downloaded this program. | |
| # 3. Change the value of 'BOOKMARKS_FILE' with the name of the html file. | |
| # 4. Run this program. You should find the output file 'Modified_ + BOOKMARKS_FILE' located in the same directory as this program after execution. | |
| # 5. Delete all your current bookmarks in Chrome's bookmark manager (CTRL-A to select all). | |
| # 6. Go to Options -> Import bookmarks to import the modified html file. | |
| # 7. Enjoy! | |
| import re | |
| import requests | |
| # EDIT HERE | |
| BOOKMARKS_FILE = '...' | |
| API_BASE_URL = 'https://api.mangadex.org/' | |
| BASE_URLS = [ | |
| 'https://mangadex.org/title/', | |
| 'https://mangadex.org/chapter/', | |
| 'https://mangadex.org/manga/' | |
| ] | |
| with open(BOOKMARKS_FILE, 'r', encoding='utf8') as f: | |
| htm = f.read() | |
| out = htm | |
| cnt = 0 | |
| def get_uuid(int_id, type): | |
| payload = { 'type': type, 'ids': [int_id] } | |
| r = requests.post(API_BASE_URL + 'legacy/mapping', json=payload) | |
| if r.status_code != 200: | |
| raise Exception(f'Bad status code: {r.status_code}') | |
| json = r.json() | |
| uuid = json[0]['data']['attributes']['newId'] | |
| return uuid | |
| def log_title(uuid, type): | |
| r = requests.get(API_BASE_URL + type + f'/{uuid}') | |
| if r.status_code != 200: | |
| raise Exception(f'Bad status code: {r.status_code}') | |
| json = r.json() | |
| title = json['data']['attributes']['title'] if type == 'chapter' else json['data']['attributes']['title']['en'] | |
| print(f'Manga: {title}') | |
| for link in re.findall(r'\"https://mangadex.org/[A-Za-z0-9-/]+\"', htm): | |
| link = link[1:-1] | |
| for base_url in BASE_URLS: | |
| if link.startswith(base_url): | |
| id = int(re.search(r'\d+', link).group()) | |
| type = base_url[21:-1] | |
| if type == 'title': | |
| type = 'manga' | |
| uuid = get_uuid(id, type) | |
| log_title(uuid, type) | |
| new_url = base_url + uuid | |
| cnt += 1 | |
| print(f'New url #{cnt}: {new_url}', end='\n\n') | |
| out = re.sub(link, new_url, out, 1) | |
| break | |
| with open('Modified_' + BOOKMARKS_FILE, 'w', encoding='utf8') as f: | |
| f.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment