ren811

NyaaAnimeFinder

Oct 4th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib3
  2. import os
  3. import time
  4. from sys import exit
  5.  
  6. # diretório atual
  7. curdir = os.getcwd()
  8.  
  9. #lista de animes
  10. animes = [['Coppelion',
  11.            'Kill la Kill',
  12.            'Golden Time',
  13.            'Yuushibu'],
  14.           ['coppelion.jpg',
  15.            'killlakill.jpg'
  16.            'goldentime.jpg']]
  17.  
  18. #lista de fansubs
  19. fansubs = ['HorribleSubs',
  20.            'Commie',
  21.            'UTW',
  22.            'WhyNot',
  23.            'Vivid',
  24.            'FFF',
  25.            'GotWoot',
  26.            'EveTaku',
  27.            'Coalgirls']
  28.  
  29. # conexão com a página
  30.  
  31. url = 'http://www.nyaa.se/?page=rss'
  32. http = urllib3.PoolManager()
  33. r = http.request('GET', url)
  34.  
  35. # começo e fim da página de HTML
  36. startTable = "<html><body><FONT FACE='arial'><table border=\"2\">"
  37. endTable = "</table></FONT></body></html>"
  38.  
  39. # retorna o status obtido na conexão
  40. print("Status obtido: " + str(r.status))
  41. if (r.status != 200):
  42.     print("O script não será mais executado.")
  43.     os.system("pause")
  44.     exit(0)
  45.  
  46. print("Iniciando conversão para UTF-8...")
  47.  
  48. # tenta decodificar para UTF8
  49. try:
  50.     t = r.data.decode("utf8")
  51. except DECODEERROR as E:
  52.     print("Ocorreu um erro: " + str(E))
  53.     print("O script será parado agora.")
  54.     exit(0)
  55. print("Conversão completa.")
  56.  
  57. #passamos a info para uma variável
  58. HTML = str(r.data)
  59.  
  60. #lista para armazenar os torrents
  61. itemList = []
  62.  
  63. class Item:
  64.     def __init__(self, start, end):
  65.         self.start = start
  66.         self.end = end
  67.         self.data = HTML[start+6:end]
  68.         self.title = self.getTitle()
  69.         self.category = self.getCat()
  70.         self.DL = self.getDownloadLink()
  71.         self.VL = self.getViewLink()
  72.         self.description = self.getDescription()
  73.         self.date = self.getDate()
  74.     def getTitle(self):
  75.         a = self.data.find("<title>")
  76.         b = self.data.find("</title>")
  77.         return str(self.data[a+7:b])
  78.     def getCat(self):
  79.         me = self.data.find("<category>")
  80.         ow = self.data.find("</category>")
  81.         return str(self.data[me+10:ow])
  82.     def getDownloadLink(self):
  83.         a = self.data.find("<link>")
  84.         b = self.data.find("</link>")
  85.         return str(self.data[a+6:b])
  86.     def getViewLink(self):
  87.         a = self.data.find("<guid>")
  88.         b = self.data.find("</guid>")
  89.         return str(self.data[a+6:b])
  90.     def getDescription(self):
  91.         a = self.data.find("<description>")
  92.         b = self.data.find("</description>")
  93.         return str(self.data[a+22:b-3])
  94.     def getDate(self):
  95.         a = self.data.find("<pubDate>")
  96.         b = self.data.find("</pubDate>")
  97.         return str(self.data[a+9:b])
  98.  
  99. # separa os itens em objetos e coloca no array itemList
  100.  
  101. def encontraItem(n):
  102.     comeco = HTML.find("<item>", n)
  103.     fim = HTML.find("</item>", comeco)
  104.     if comeco == -1 or fim == -1:
  105.         return [-1, -1]
  106.     else:
  107.         thisItem = Item(comeco,fim)
  108.         itemList.append(thisItem)
  109.         print("thisItem: " + str(thisItem.end))
  110.         return [comeco, fim]
  111.  
  112. ultimoitem = 0
  113. contador = 0
  114.  
  115. # percorrer a info que pegamos direto do site
  116.  
  117. while(1):
  118.     umitem = encontraItem(ultimoitem)
  119.     if umitem[0] > 0 and umitem[1] > 0:
  120.         ultimoitem = umitem[0]+1
  121.         print("Começo: " + str(umitem[0]))
  122.         print("Fim: " + str(umitem[1]))
  123.         contador += 1
  124.     if umitem[0] == -1 or umitem[1] == -1:
  125.         print("Itens encontrados: " + str(contador))
  126.         break
  127.  
  128. # separar os animes que queremos
  129.  
  130. animeList = []
  131. contador = 0
  132. for i in range(len(itemList)):
  133.     for x in range(len(animes[0])):
  134.         if itemList[i].title.find(animes[0][x]) != -1 and itemList[i].category.find("English-translated") != -1:
  135.             print("Title: " + itemList[i].title)
  136.             print("Anime: " + animes[0][x])
  137.             animeList.append(itemList[i])
  138.             contador += 1
  139.  
  140. print("Torrens provaveis: " + str(contador))
  141.  
  142. # abrimos um arquivo para escrever o HTML
  143. file = open(curdir + "\page.html","w",encoding="utf8")
  144. HTML = startTable
  145.  
  146. def NewLine(string):
  147.     return "<tr>" + string + "</tr>"
  148.  
  149. def NewRow(string):
  150.     return "<td>" + string + "</td>"
  151.  
  152. for i in range(len(animeList)):
  153.     HTML += NewLine(NewRow(animeList[i].title) + NewRow(animeList[i].description) + NewRow(animeList[i].DL + NewRow(animeList[i].date)))
  154.  
  155. HTML += endTable
  156. file.write(HTML)
  157. file.close()
Advertisement
Add Comment
Please, Sign In to add comment