Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2013
355
0
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. #Additional info and packages:
  4. # Python 3.3 - http://python.org/download/
  5. # BeautifulSoup - http://www.crummy.com/software/BeautifulSoup/#Download
  6. # 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>
  7.  
  8. from bs4 import BeautifulSoup,NavigableString
  9. import urllib2,sys,re,codecs
  10. import json
  11.  
  12. print('This script will export your anime-planet.com anime list and saves it to anime-planet.xml')
  13. username = raw_input("Enter your username: ")
  14. headers = { 'User-Agent' : 'Mozilla/5.0' }
  15. baseURL = 'http://www.anime-planet.com/users/'+username+'/anime'
  16. req = urllib2.Request(baseURL, None, headers)
  17. html = urllib2.urlopen(req).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-planet2.xml', 'w', 'utf-8')
  25. f.write ('<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n')
  26. f.write ('<myanimelist>\n')
  27.  
  28. print('Exporting rough variant of myanimelist format... \n')
  29. for i in range(1,pageNumber+1):
  30.                 baseURL = 'http://www.anime-planet.com/users/'+username+'/anime?page='+str(i)
  31.                 req = urllib2.Request(baseURL, None, headers)
  32.                 html = urllib2.urlopen(req).read()
  33.                 html = BeautifulSoup(html)
  34.                 for animeItem in html.findAll('tr')[1:]:
  35.                         animeItem = BeautifulSoup(animeItem.renderContents())
  36.                         animeName = '' + animeItem.a.text
  37.                         queryName = re.sub('[^A-Za-z0-9]+', '%20', animeName)
  38.                         queryTitle = urllib2.urlopen(queryURL + queryName).read()  
  39.                         print(animeName)
  40.                         search=json.loads(queryTitle.decode('utf8'))
  41.                         for x in search:
  42.                                 #print(animeName)
  43.                                 try:
  44.                                         if animeName.lower()==x["title"].lower():
  45.                                                 animeID=str(x["id"])
  46.                                         elif animeName.lower()in [y.lower() for y in x["other_titles"]["english"]]:
  47.                                                 animeID=str(x["id"])
  48.                                         elif animeName.lower() in [j.lower() for j in x["other_titles"]["synonyms"]]:
  49.                                                 animeID=str(x["id"])
  50.                                 except KeyError as e:
  51.                                         pass
  52.                         if animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "")=="Watched":
  53.                                 status="Completed"
  54.                         elif animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "")=="Stalled":
  55.                                 status="On-Hold"
  56.                         elif animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "")=="WanttoWatch":
  57.                                 status="Plan to Watch"
  58.                         elif animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "")=="Won'tWatch":
  59.                                 status="Dropped"
  60.                                 continue
  61.                         else:
  62.                                 status=animeItem.find('td','tableStatus').text.replace('status box','').replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "")
  63.                         f.write ('\t<anime>\n');
  64.                         f.write ('\t\t<series_animedb_id>'+ animeID +'</series_animedb_id>\n');
  65.                         f.write ('\t\t<series_title><![CDATA['+ animeName +']]></series_title>\n');
  66.                         f.write ('\t\t<series_type>' + animeItem.find('td','tableType').text + '</series_type>\n');
  67.                         f.write ('\t\t<my_id>0</my_id>\n');
  68.                         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');
  69.                         f.write ('\t\t<my_start_date>0000-00-00</my_start_date>\n');
  70.                         f.write ('\t\t<my_finish_date>0000-00-00</my_finish_date>\n');
  71.                         f.write ('\t\t<my_fansub_group><![CDATA[]]></my_fansub_group>\n');
  72.                         f.write ('\t\t<my_rated></my_rated>\n');
  73.                         f.write ('\t\t<my_score>' + str(int(float(animeItem.img['name'])*2)).replace("\t", "").replace("\n", "").replace("\r", "").replace(" ", "") + '</my_score>\n');
  74.                         f.write ('\t\t<my_dvd></my_dvd>\n');
  75.                         f.write ('\t\t<my_storage></my_storage>\n');
  76.                         f.write ('\t\t<my_status>' + status +'</my_status>\n');
  77.                         f.write ('\t\t<my_comments><![CDATA[]]></my_comments>\n');
  78.                         f.write ('\t\t<my_times_watched>0</my_times_watched>\n');
  79.                         f.write ('\t\t<my_rewatch_value></my_rewatch_value>\n');
  80.                         f.write ('\t\t<my_downloaded_eps>0</my_downloaded_eps>\n');
  81.                         f.write ('\t\t<my_tags><![CDATA[]]></my_tags>\n');
  82.                         f.write ('\t\t<my_rewatching>0</my_rewatching>\n');
  83.                         f.write ('\t\t<my_rewatching_ep>0</my_rewatching_ep>\n');
  84.                         f.write ('\t\t<update_on_import>1</update_on_import>\n');
  85.                         f.write ('\t</anime>\n\n');
  86.  
  87.  
  88.                                
  89. f.write ('</myanimelist>\n')  
  90. print('Done, see anime-planet.xml and anime_list.txt')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement