Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urllib3
- import os
- from bs4 import BeautifulSoup
- os.system("mode con: cols=50 lines=25")
- url = 'http://nyaa.se'
- http = urllib3.PoolManager()
- r = http.request('GET', url)
- def printalogo():
- logo = ["|\ | \ / /\ /\\", "| \ | \ / / \ / \ ___", "| \ | | /____\ /____\ / _ \ | | ", "| \ | | | | | | _ | __/ | |", "| \| | | | | | |_| \___| \__/"]
- for item in logo:
- print(item)
- print("_____________________________")
- print("Python Anime Observer!\n\nV:0.2")
- printalogo()
- print ("Status obtido: ")
- print (r.status)
- if (r.status == 200):
- print("Iniciando conversão de bytes para UTF-8...")
- t = r.data.decode("utf8")
- print("Conversão completa.")
- HTML = BeautifulSoup(t)
- data = str(HTML.find_all('tr',attrs={'class':'tlistrow'}))
- os.system("cls")
- printalogo()
- else:
- os.system("cls")
- printalogo()
- print ("Status retornado é desconhecido (" + str(r.status) + ")")
- os.system("pause")
- data = t
- animes = ['Shingeki no Kyojin',
- 'Gatchaman Crowds',
- 'Inu to Hasami wa Tsukaiyou',
- 'The World God Only Knows'
- 'Gin no Saji'] #TODO: tornar customizável
- tags = ("<tr", "</tr>", "<td", "</td>")
- fansubs = ['HorribleSubs',
- 'Commie',
- 'UTW',
- 'WhyNot']
- ctr = []
- ftr = []
- # ------------------------------------ #
- # Funções que pegam posições das tags #
- # ------------------------------------ #
- def encontraTD(n):
- comeco = data.find(tags[2],n)
- fim = data.find(tags[3],comeco)
- return [comeco,fim]
- def encontraTR(n):
- comeco = data.find(tags[0],n)
- fim = data.find(tags[1],comeco)
- ctr.append(comeco)
- ftr.append(fim)
- return [comeco,fim]
- # ------------------------------------------ #
- # Percorre o arquivo inteiro. #
- # Capturando primeiro os pontos de TR, #
- # depois os TDs entre o começo e fim do TR #
- # ------------------------------------------ #
- contador = 1
- lastTR = 0
- while(1):
- gotTR = encontraTR(lastTR)
- if gotTR[0] > 0 and gotTR[1] > 0:
- lastTD = gotTR[0]+1
- ctd = []
- ftd = []
- while (1):
- gotTD = encontraTD(lastTD)
- ctd.append(gotTD[0])
- ftd.append(gotTD[1])
- lastTD = gotTD[1]+1
- #print("TD " + str(contador) + ": " + str(gotTD))
- if lastTD+14 >= gotTR[1]:
- #print("\nTDs: " + str([ctd,ftd]))
- break
- contador+=1
- lastTR = gotTR[1]+1
- #print("TR: " + str(gotTR))
- if gotTR[0] == -1 or gotTR[1] == -1:
- break
- #print("\nctr: " + str(ctr))
- #print("ftr: " + str(ftr))
- #print("CTRs: " + str(len(ctr)))
- #os.system("pause")
- # -------------------------------- #
- # inicia busca dentro de torrents #
- # -------------------------------- #################################################
- # Torrents selecionados tem seus TRs nos arrays 'foundC' (começo) e 'foundF' (fim) #
- # -------------------------------------------------------------------------------- #
- foundC = []
- foundF = []
- contador = 0
- encontrar = "English-translated"
- #print("Procurando caracteres entre " + str(ctr[0]) + " (<tr>) e " + str(ftr[len(ftr)-1]) + " (</tr>)...")
- for i in range(len(ctr)):
- busca = data.find(str(" " + encontrar),ctr[i],ftr[i])
- if busca == -1:
- #print("Nada")
- i+=1
- else:
- #print(busca)
- foundC.append(ctr[i])
- foundF.append(ftr[i])
- i+=1
- contador+=1
- if i == len(ctr):
- print("\nEncontrados " + str(contador) + " torrents com \"" + encontrar + "\"")
- break
- #print(foundC)
- #print(len(foundC))
- #print(foundF)
- # ----------------------- #
- # Refina busca por fansub #
- # ----------------------- #
- torrentsC = []
- torrentsF = []
- contador = 0
- print("Procurando torrents de fansubs...")
- for i in range(len(foundC)):
- for x in range(len(fansubs)):
- busca = data.find(fansubs[x], foundC[i], foundF[i])
- if busca != -1:
- contador+=1
- torrentsC.append(foundC[i])
- torrentsF.append(foundF[i])
- print("Encontrados " + str(contador) + " torrents de fansubs conhecidos.")
- #print(torrentsC)
- #print(torrentsF)
- # ------------------------------------------------------------------------ #
- # Começar a tratar os torrents individualmente, separando-os pelas tags tr #
- # ------------------------------------------------------------------------ #
- print("Separando torrents...")
- PossiveisTorrents = []
- for i in range(len(torrentsC)):
- #print("alo")
- PossiveisTorrents.append(data[torrentsC[i]:torrentsF[i]+len(tags[3])])
- print("Possíveis torrents: " + str(len(PossiveisTorrents)))
- # ------------------------------------------------ #
- # Procurar pelos animes especificados inicialmente #
- # ------------------------------------------------ #
- contador = 0
- escolhidos = []
- for i in range(len(PossiveisTorrents)):
- for a in range(len(animes)):
- busca = PossiveisTorrents[i].find(animes[a])
- if busca != -1:
- escolhidos.append(PossiveisTorrents[i])
- contador+=1
- if contador == 0:
- print("> Encontrados " + str(contador) + " torrents de animes procurados.\n\n")
- else:
- print("> Encontrados " + str(contador) + " torrents de animes procurados.\n\n")
- #print(escolhidos)
- # ---------------------------------
- # Obter agora os IDs dos torrents.
- # ---------------------------------
- IDs = []
- TorrentName = []
- for i in range(len(escolhidos)):
- buscaIDC = escolhidos[i].find("tid")
- buscaIDF = escolhidos[i].find(">",buscaIDC+1)
- IDs.append(escolhidos[i][buscaIDC+4:buscaIDF-1])
- buscaNome = escolhidos[i].find("</a>",buscaIDF-1)
- TorrentName.append(escolhidos[i][buscaIDF+1:buscaNome])
- print("\nTítulo do torrent: " + str(TorrentName[i]))
- print("Link: http://www.nyaa.se/?page=view&tid=" + str(IDs[i]))
- os.system("pause")
Advertisement
Add Comment
Please, Sign In to add comment