Advertisement
7h3d4rkw0lf

w0lfy's Animu Downloader in Python

Sep 11th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.65 KB | None | 0 0
  1. #By Remi_Scarlet aka 7h3d4rkw0lf
  2.  
  3.  
  4. from HTMLParser import HTMLParser
  5. from htmlentitydefs import name2codepoint
  6. import urllib2
  7. import re
  8. import string
  9. import os.path
  10. import subprocess
  11. from time import strftime, gmtime
  12. import time
  13.  
  14. #insert first couple of words of group+show you want
  15. listOfAnimes = ["", "", "", "", "", "", ""]
  16. listOfAnimes[0] = "[FTW]_Watashi_ga_Motenai"
  17. listOfAnimes[1] = "[Vivid] Kamisama no Inai"
  18. listOfAnimes[2] = "[UTW]_Fate_Kaleid_Liner"
  19. listOfAnimes[3] = "[Vivid] Stella Jogakuin"
  20. listOfAnimes[4] = "[Commie] Love Lab"
  21. listOfAnimes[5] = ""
  22. listOfAnimes[6] = ""
  23.  
  24. #insert search terms for each show.
  25. url = ["","","","","","",""]
  26. url[0] = "ftw watashi ga motenai"
  27. url[1] = "vivid kamisama no inai"
  28. url[2] = "utw fate kaleid liner"
  29. url[3] = "vivid stella jogakuin"
  30. url[4] = "commie love lab"
  31. url[5] = "Blank!"
  32. url[6] = "Blank!"
  33.  
  34.  
  35.  
  36.  
  37. for i in range(len(listOfAnimes)):
  38.     listOfAnimes[i] = string.replace(listOfAnimes[i],"[","\[")
  39.     listOfAnimes[i] = string.replace(listOfAnimes[i],"]","\]") 
  40.     print listOfAnimes[i]
  41.  
  42. dataArray = ["", "", ""]
  43.  
  44. for i in range(len(url)):
  45.     if url[i] != "Blank!":
  46.         temp = url[i].split(" ")
  47.         url[i] = "http://www.nyaa.se/?page=search&cats=0_0&filter=0&term="
  48.         for x in range(len(temp)):
  49.             url[i] = url[i]+temp[x]
  50.             if x != len(temp)-1:
  51.                 url[i] = url[i]+"+"
  52.     print url[i]
  53.  
  54. def animeDirectory(nameOfShow, data):
  55.     if nameOfShow == listOfAnimes[0]:  
  56.         torrentLoc = "D:\\Anime Torrents\\Watamote\\torrents\\"+data+".torrent"
  57.     if nameOfShow == listOfAnimes[1]:  
  58.         torrentLoc = "D:\\Anime Torrents\\Kamisama no Inai\\torrents\\"+data+".torrent"
  59.     if nameOfShow == listOfAnimes[2]:  
  60.         torrentLoc = "D:\\Anime Torrents\\Fate Kaleid\\torrents\\"+data+".torrent"
  61.     if nameOfShow == listOfAnimes[3]:  
  62.         torrentLoc = "D:\\Anime Torrents\\Stella Jogakuin\\torrents\\"+data+".torrent"
  63.     if nameOfShow == listOfAnimes[4]:  
  64.         torrentLoc = ""
  65.     if nameOfShow == listOfAnimes[5]:  
  66.         torrentLoc = ""
  67.     if nameOfShow == listOfAnimes[6]:  
  68.         torrentLoc = ""
  69.     return torrentLoc
  70.  
  71.    
  72. def doesExist(torrentLoc):
  73.     if os.path.isfile(torrentLoc) == True:
  74.         return True
  75.     if os.path.isfile(torrentLoc) == False:
  76.         return False
  77.  
  78. def openTorrent(torrentLoc, animeLoc, data):
  79.     bittorrentPath = r'G:\\BitTorrent\\BitTorrent.exe'
  80.     subprocess.call([bittorrentPath, "/directory", animeLoc, torrentLoc])
  81.     print "@@@@@@@@@@@@@@"
  82.     print "@@@Success!@@@"
  83.     print "@@@@@@@@@@@@@@"
  84.     print "Downloading", data, "to", animeLoc+data
  85.    
  86. class NyaaParser(HTMLParser):
  87.     def handle_starttag(self, tag, attrs):
  88.         #print "Start tag: ", tag
  89.         for attr in attrs:
  90.             #print "--attr:", attr
  91.             if tag == 'a' and re.match("\('href',", str(attr)) != None:
  92.                 attribute = str(attr)
  93.                 #print "link for anime desu --------------", attribute[11:-2]
  94.                 dataArray[0] = attribute[11:-2]
  95.                 dataArray[0] = string.replace(dataArray[0],"view","download")
  96.     def handle_data(self, data):
  97.         for i in listOfAnimes:
  98.             if i == "":
  99.                 break
  100.             torrentLoc = animeDirectory(i, data)
  101.             if re.match(i, str(data)) != None and re.search(".mkv", str(data)) != None:
  102.                 print "Torrent Name: ", data
  103.                 print "Match found!"   
  104.                 if doesExist(torrentLoc) == False:
  105.                     print "!!!!!!Downloading file!!!!!!"
  106.                     print "Downloaded to: "+torrentLoc
  107.                     urlFile = urllib2.urlopen(dataArray[0]+"&txt=1")
  108.                     print "Torrent downloaded from: "+dataArray[0]+"&txt=1"
  109.                     localFile = open(torrentLoc, 'wb')
  110.                     localFile.write(urlFile.read())
  111.                     localFile.close()
  112.                     animeLoc = string.replace(torrentLoc,"\\torrents\\"+data+".torrent","")
  113.                     if doesExist(animeLoc+data) == False:
  114.                         openTorrent(torrentLoc, animeLoc, data)
  115.                     else:
  116.                         print "Weird. Seems like you didn't have the torrent file but DID have the mkv file already! Bittorrent not opened."
  117.                     print "------------------------------------"
  118.                     print "------------------------------------"               
  119.                 else:
  120.                     print "Oops! Already exists on local drive. Not downloading", data + ".torrent"
  121.                     print "------------------------------------"
  122.                     print "------------------------------------"
  123.        
  124.  
  125. lastTime = ""
  126. while 1:
  127.     if strftime("%H:%M:%S", gmtime()) == "03:59:59":
  128.         for i in url:
  129.             if i != "Blank!":
  130.                 print i
  131.                 data = urllib2.urlopen(i)
  132.                 source = data.read()
  133.                 parser = NyaaParser()
  134.                 parser.feed(source)
  135.                 print "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"
  136.             else:
  137.                 print "No more animes to search! We're done!"
  138.                 break
  139.     else:
  140.         if lastTime != strftime("%H:%M:%S", gmtime()):
  141.             print "Will check for new episodes at 03:59:59PM UTC. Current time is: "+strftime("%H:%M:%S", gmtime()), "UTC"
  142.         lastTime = strftime("%H:%M:%S", gmtime())
  143.         time.sleep(0.1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement