Created
February 1, 2019 04:41
-
-
Save GhostofGoes/e3d84e6e54e331893b059dcbddab8375 to your computer and use it in GitHub Desktop.
Simple export for Anime-Planet
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
import logging | |
import re | |
import browsercookie | |
import requests | |
from bs4 import BeautifulSoup | |
from requests.cookies import RequestsCookieJar | |
class AnimePlanet: | |
EXPORT_URL = 'https://www.anime-planet.com/api/export' | |
def __init__(self): | |
self.log = logging.getLogger(self.__class__.__name__) | |
self.cookies = RequestsCookieJar() | |
self.update_cookies() | |
self.uid = self.get_export_uid() | |
def update_cookies(self): | |
chrome_cookies = browsercookie.chrome() | |
self.cookies.update(chrome_cookies) | |
def get_export_uid(self) -> str: | |
page = self._get('https://www.anime-planet.com/users/export_list.php') | |
if page: | |
return self._parse_export_page_uid(page.text) | |
else: | |
self.log.error(f"Couldn't get the export page") | |
return '' | |
@staticmethod | |
def _parse_export_page_uid(text: str) -> str: | |
soup = BeautifulSoup(text, 'html.parser') | |
tag = soup.find('form', action=re.compile('/api/export/anime')) | |
uid = tag['action'].rpartition('/')[2] | |
return uid | |
def _get(self, url: str) -> requests.Response: | |
response = requests.get(url, cookies=self.cookies) | |
if response.status_code != 200: | |
self.log.error(f"Failed to get URL '{url}': status " | |
f"code {response.status_code}") | |
return None | |
return response | |
def export_anime(self): | |
return self._get_list('anime') | |
def export_manga(self): | |
return self._get_list('manga') | |
def _get_list(self, list_name: str) -> dict: | |
response = self._get(f'{self.EXPORT_URL}/{list_name}/{self.uid}') | |
if response: | |
return response.json() | |
else: | |
self.log.error(f"Could not get the {list_name} list") | |
return {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment