Skip to content

Instantly share code, notes, and snippets.

@GhostofGoes
Created February 1, 2019 04:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GhostofGoes/e3d84e6e54e331893b059dcbddab8375 to your computer and use it in GitHub Desktop.
Save GhostofGoes/e3d84e6e54e331893b059dcbddab8375 to your computer and use it in GitHub Desktop.
Simple export for Anime-Planet
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