Advertisement
Guest User

AnimePlanet to MAL exporter

a guest
Apr 24th, 2013
1,145
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/python
  2. #This script will take your anime-planet.com username and scrape a list of your watched anime in XML utf-8 format to anime-planet.xml
  3. #Will also create an anime_list.txt file for all the anime that needs to be added manually. Note that the code will break if there are
  4. #special characters invovled... So remove the anime from your Anime-Planet list first.
  5. #Additional info and packages:
  6. # Python 3.3 - http://python.org/download/
  7. # BeautifulSoup - http://www.crummy.com/software/BeautifulSoup/#Download
  8. # In order to successfully import the exported Anime-Planet list to MAL, first export a MAL list, and copy the <myinfo> block just after <myanimelist>
  9.  
  10. from bs4 import BeautifulSoup,NavigableString
  11. import urllib.request, urllib.error, urllib.parse,sys,re,codecs
  12. import json
  13.  
  14. print('This script will export your anime-planet.com anime list and saves it to anime-planet.xml')
  15. username = input("Enter your username: ")
  16. baseURL = 'http://www.anime-planet.com/users/'+username+'/anime'
  17. html = urllib.request.urlopen(baseURL).read()
  18. html = BeautifulSoup(html)
  19. pageNumber = int (html.find('li','next').findPrevious('li').next.contents[0])
  20. delimiter = '\t'
  21.  
  22. queryURL = 'http://mal-api.com/anime/search?q='
  23.  
  24. f = codecs.open('anime-planet.xml', 'w', 'utf-8')
  25. f.write ('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n')
  26. f.write ('<myanimelist>\n')
  27. g = codecs.open('anime_list.txt', 'w', 'utf-8')
  28. g.write ('You will have to add these anime manually \n \n')
  29.  
  30. print('Exporting rough variant of myanimelist format... \n')
  31. for i in range(1,pageNumber+1):
  32.         baseURL = 'http://www.anime-planet.com/users/'+username+'/anime?page='+str(i)
  33.         html = urllib.request.urlopen(baseURL).read()
  34.         html = BeautifulSoup(html)
  35.         for animeItem in html.findAll('tr')[1:]:
  36.             animeItem = BeautifulSoup(animeItem.renderContents())
  37.             animeName = '' + animeItem.a.text
  38.             queryName = re.sub('[^A-Za-z0-9]+', '%20', animeName)
  39.             queryTitle = urllib.request.urlopen(queryURL + queryName).read()  
  40.             print(queryTitle)
  41.             search=json.loads(queryTitle.decode('utf8'))
  42.             for x in search:
  43.                 print(animeName)
  44.                 try:
  45.                     if animeName.lower()==x["title"].lower():
  46.                         animeID=str(x["id"])
  47.                     elif animeName.lower()in [y.lower() for y in x["other_titles"]["english"]]:
  48.                         animeID=str(x["id"])
  49.                     elif animeName.lower() in [j.lower() for j in x["other_titles"]["synonyms"]]:
  50.                         animeID=str(x["id"])
  51.                 except KeyError as e:
  52.                     pass
  53.  
  54.             f.write ('\t<anime>\n');
  55.             f.write ('\t\t<series_animedb_id>'+ animeID +'</series_animedb_id>\n');
  56.             f.write ('\t\t<series_title><![CDATA['+ animeName +']]></series_title>\n');
  57.             f.write ('\t\t<series_type>' + animeItem.find('td','tableType').text + '</series_type>\n');
  58.             f.write ('\t\t<my_id>0</my_id>\n');
  59.             f.write ('\t\t<my_watched_episodes>'+ animeItem.find('td','tableEps').text.replace('&nbsp;','1').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "") +'</my_watched_episodes>\n');
  60.             f.write ('\t\t<my_start_date>0000-00-00</my_start_date>\n');
  61.             f.write ('\t\t<my_finish_date>0000-00-00</my_finish_date>\n');
  62.             f.write ('\t\t<my_fansub_group><![CDATA[]]></my_fansub_group>\n');
  63.             f.write ('\t\t<my_rated></my_rated>\n');
  64.             f.write ('\t\t<my_score>' + str(int(float(animeItem.img['name'])*2)).replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "") + '</my_score>\n');
  65.             f.write ('\t\t<my_dvd></my_dvd>\n');
  66.             f.write ('\t\t<my_storage></my_storage>\n');
  67.             f.write ('\t\t<my_status>' + animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "") +'</my_status>\n');
  68.             f.write ('\t\t<my_comments><![CDATA[]]></my_comments>\n');
  69.             f.write ('\t\t<my_times_watched>0</my_times_watched>\n');
  70.             f.write ('\t\t<my_rewatch_value></my_rewatch_value>\n');
  71.             f.write ('\t\t<my_downloaded_eps>0</my_downloaded_eps>\n');
  72.             f.write ('\t\t<my_tags><![CDATA[]]></my_tags>\n');
  73.             f.write ('\t\t<my_rewatching>0</my_rewatching>\n');
  74.             f.write ('\t\t<my_rewatching_ep>0</my_rewatching_ep>\n');
  75.             f.write ('\t\t<update_on_import>1</update_on_import>\n');
  76.             f.write ('\t</anime>\n\n');
  77.  
  78.  
  79.                
  80. f.write ('</myanimelist>\n')   
  81. print('Done, see anime-planet.xml and anime_list.txt')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement