Advertisement
Guest User

Untitled

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