ren811

NyaAnimeFinder

Sep 1st, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.29 KB | None | 0 0
  1. import urllib3
  2. import os
  3. from bs4 import BeautifulSoup
  4.  
  5. os.system("mode con: cols=50 lines=25")
  6.  
  7. url = 'http://nyaa.se'
  8. http = urllib3.PoolManager()
  9. r = http.request('GET', url)
  10.  
  11.  
  12. def printalogo():
  13.     logo = ["|\   | \  /   /\      /\\", "| \  |  \ /   /  \    /  \        ___", "|  \ |   |   /____\  /____\      / _ \ |  | ", "|   \ |   |  |      | |      |  _  |  __/ |  |", "|    \|   |  |      | |      | |_|  \___| \__/"]
  14.     for item in logo:
  15.         print(item)
  16.     print("_____________________________")
  17.     print("Python Anime Observer!\n\nV:0.2")
  18.  
  19. printalogo()
  20. print ("Status obtido: ")
  21. print (r.status)
  22. if (r.status == 200):
  23.     print("Iniciando conversão de bytes para UTF-8...")
  24.     t = r.data.decode("utf8")
  25.     print("Conversão completa.")
  26.     HTML = BeautifulSoup(t)
  27.     data = str(HTML.find_all('tr',attrs={'class':'tlistrow'}))
  28.     os.system("cls")
  29.     printalogo()
  30. else:
  31.     os.system("cls")
  32.     printalogo()
  33.     print ("Status retornado é desconhecido (" + str(r.status) + ")")
  34.     os.system("pause")
  35.  
  36. data = t
  37. animes = ['Shingeki no Kyojin',
  38.           'Gatchaman Crowds',
  39.           'Inu to Hasami wa Tsukaiyou',
  40.           'The World God Only Knows'
  41.           'Gin no Saji'] #TODO: tornar customizável
  42.  
  43. tags = ("<tr", "</tr>", "<td", "</td>")
  44.  
  45. fansubs = ['HorribleSubs',
  46.            'Commie',
  47.            'UTW',
  48.            'WhyNot']
  49.  
  50. ctr = []
  51. ftr = []
  52.  
  53. # ------------------------------------ #
  54. # Funções que pegam posições das tags  #
  55. # ------------------------------------ #
  56.  
  57. def encontraTD(n):
  58.         comeco = data.find(tags[2],n)
  59.         fim = data.find(tags[3],comeco)
  60.         return [comeco,fim]
  61.  
  62. def encontraTR(n):
  63.     comeco = data.find(tags[0],n)
  64.     fim = data.find(tags[1],comeco)
  65.     ctr.append(comeco)
  66.     ftr.append(fim)
  67.     return [comeco,fim]
  68.  
  69. # ------------------------------------------ #
  70. # Percorre o arquivo inteiro.                #
  71. # Capturando primeiro os pontos de TR,       #
  72. # depois os TDs entre o começo e fim do TR   #
  73. # ------------------------------------------ #
  74.  
  75. contador = 1
  76. lastTR = 0
  77. while(1):
  78.         gotTR = encontraTR(lastTR)
  79.         if gotTR[0] > 0 and gotTR[1] > 0:
  80.                 lastTD = gotTR[0]+1
  81.                 ctd = []
  82.                 ftd = []
  83.                 while (1):
  84.                         gotTD = encontraTD(lastTD)
  85.                         ctd.append(gotTD[0])
  86.                         ftd.append(gotTD[1])
  87.                         lastTD = gotTD[1]+1
  88.                         #print("TD " + str(contador) + ": " + str(gotTD))
  89.                         if lastTD+14 >= gotTR[1]:
  90.                                 #print("\nTDs: " + str([ctd,ftd]))
  91.                                 break
  92.                 contador+=1
  93.         lastTR = gotTR[1]+1
  94.         #print("TR: " + str(gotTR))
  95.         if gotTR[0] == -1 or gotTR[1] == -1:
  96.                 break
  97.  
  98. #print("\nctr: " + str(ctr))
  99. #print("ftr: " + str(ftr))
  100. #print("CTRs: " + str(len(ctr)))
  101. #os.system("pause")
  102.  
  103. # -------------------------------- #
  104. # inicia busca dentro de torrents  #
  105. # -------------------------------- #################################################
  106. # Torrents selecionados tem seus TRs nos arrays 'foundC' (começo) e 'foundF' (fim) #
  107. # -------------------------------------------------------------------------------- #
  108.  
  109. foundC = []
  110. foundF = []
  111. contador = 0
  112. encontrar = "English-translated"
  113. #print("Procurando caracteres entre " + str(ctr[0]) + " (<tr>) e " + str(ftr[len(ftr)-1]) + " (</tr>)...")
  114. for i in range(len(ctr)):
  115.         busca = data.find(str(" " + encontrar),ctr[i],ftr[i])
  116.         if busca == -1:
  117.                 #print("Nada")
  118.                 i+=1
  119.         else:
  120.                 #print(busca)
  121.                 foundC.append(ctr[i])
  122.                 foundF.append(ftr[i])
  123.                 i+=1
  124.                 contador+=1
  125.         if i == len(ctr):
  126.                 print("\nEncontrados " + str(contador) + " torrents com \"" + encontrar + "\"")
  127.                 break
  128. #print(foundC)
  129. #print(len(foundC))
  130. #print(foundF)
  131.  
  132. # ----------------------- #
  133. # Refina busca por fansub #
  134. # ----------------------- #
  135.  
  136. torrentsC = []
  137. torrentsF = []
  138. contador = 0
  139. print("Procurando torrents de fansubs...")
  140. for i in range(len(foundC)):
  141.         for x in range(len(fansubs)):
  142.                 busca = data.find(fansubs[x], foundC[i], foundF[i])
  143.                 if busca != -1:
  144.                         contador+=1
  145.                         torrentsC.append(foundC[i])
  146.                         torrentsF.append(foundF[i])
  147.  
  148. print("Encontrados " + str(contador) + " torrents de fansubs conhecidos.")
  149.  
  150. #print(torrentsC)
  151. #print(torrentsF)
  152.  
  153. # ------------------------------------------------------------------------ #
  154. # Começar a tratar os torrents individualmente, separando-os pelas tags tr #
  155. # ------------------------------------------------------------------------ #
  156.  
  157. print("Separando torrents...")
  158. PossiveisTorrents = []
  159. for i in range(len(torrentsC)):
  160.         #print("alo")
  161.         PossiveisTorrents.append(data[torrentsC[i]:torrentsF[i]+len(tags[3])])
  162.  
  163. print("Possíveis torrents: " + str(len(PossiveisTorrents)))
  164.  
  165. # ------------------------------------------------ #
  166. # Procurar pelos animes especificados inicialmente #
  167. # ------------------------------------------------ #
  168.  
  169. contador = 0
  170. escolhidos = []
  171. for i in range(len(PossiveisTorrents)):
  172.         for a in range(len(animes)):
  173.                 busca = PossiveisTorrents[i].find(animes[a])
  174.                 if busca != -1:
  175.                         escolhidos.append(PossiveisTorrents[i])
  176.                         contador+=1
  177.  
  178. if contador == 0:
  179.     print("> Encontrados " + str(contador) + " torrents de animes procurados.\n\n")
  180. else:
  181.     print("> Encontrados " + str(contador) + " torrents de animes procurados.\n\n")
  182.  
  183. #print(escolhidos)
  184.  
  185. # ---------------------------------
  186. # Obter agora os IDs dos torrents.
  187. # ---------------------------------
  188.  
  189. IDs = []
  190. TorrentName = []
  191.  
  192. for i in range(len(escolhidos)):
  193.         buscaIDC = escolhidos[i].find("tid")
  194.         buscaIDF = escolhidos[i].find(">",buscaIDC+1)
  195.         IDs.append(escolhidos[i][buscaIDC+4:buscaIDF-1])
  196.         buscaNome = escolhidos[i].find("</a>",buscaIDF-1)
  197.         TorrentName.append(escolhidos[i][buscaIDF+1:buscaNome])
  198.         print("\nTítulo do torrent: " + str(TorrentName[i]))
  199.         print("Link: http://www.nyaa.se/?page=view&tid=" + str(IDs[i]))
  200.  
  201. os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment