Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
import requests
from bs4 import BeautifulSoup
url = 'http://www.tntvillage.scambioetico.org/src/releaselist.php'
categorie = [{"id":"0","nome":"Tutte le categorie"},
{"id":"4","nome":"Film"},
{"id":"2","nome":"Musica"},
{"id":"29","nome":"Serie TV"},
{"id":"14","nome":"Documentari"},
{"id":"13","nome":"Students Releases"},
{"id":"3","nome":"E Books"},
{"id":"6","nome":"Linux"},
{"id":"7","nome":"Anime"},
{"id":"8","nome":"Cartoni"},
{"id":"1","nome":"Programmi e Film TV"},
{"id":"9","nome":"Macintosh"},
{"id":"10","nome":"Windows Software"},
{"id":"11","nome":"Pc Game"},
{"id":"12","nome":"Playstation 2"},
{"id":"21","nome":"Video Musicali"},
{"id":"22","nome":"Sport"},
{"id":"23","nome":"Teatro"},
{"id":"24","nome":"Wrestling"},
{"id":"25","nome":"Varie"},
{"id":"26","nome":"Xbox"},
{"id":"27","nome":"Immagini sfondi"},
{"id":"28","nome":"Altri Giochi"},
{"id":"30","nome":"Fumetteria"},
{"id":"31","nome":"Trash"},
{"id":"32","nome":"PlayStation 1"},
{"id":"33","nome":"PSP Portable"},
{"id":"34","nome":"Audiolibri"},
{"id":"35","nome":"Podcast"},
{"id":"36","nome":"Edicola"},
{"id":"37","nome":"Mobile"},
{"id":"38","nome":"Free Culture"}]
def parse_table(table):
if table is None: raise ValueError("table can't be none")
# Riga:
# 0: Torrent link
# 1: Magnet
# 2: Icona Categoria
# 3: Leechers
# 4: Seeders
# 5: Download Completati
# 6: Titolo
res = []
for i in range(1, len(table)):
if len(table[i]) != 7: raise ValueError("invalid row: {}".format(r))
tmp = {}
tmp['torrent'] = table[i][0].a['href'] if table[i][0].a['href'] != None else 'error'
tmp['magnet'] = table[i][1].a['href'] if table[i][0].a['href'] != None else 'error'
tmp['cat'] = table[i][2].img['src']
tmp['l'] = int(table[i][3].string)
tmp['s'] = int(table[i][4].string)
tmp['c'] = int(table[i][5].string)
titolo = table[i][6].contents
if len(titolo) != 2: return []
tmp['titolo'] = titolo[0].string.strip()
tmp['didascalia'] = titolo[1].string.strip()
res.append(tmp)
return res
def ricerca(query, cat=None, page=None):
params = {
'cat': cat,
'page': page,
'srcrel': query
}
r = requests.post(url, data = params)
if not r.ok: raise ConnectionError()
soup = BeautifulSoup(r.text, 'html.parser')
t = []
for r in soup.find_all('tr'):
t.append(r.find_all('td'))
return parse_table(t)
def main():
print("== Inserisci testo da cercare:")
query = input()
finished = False
while not finished:
print("== In quale categoria vuoi cercare? (Premi invio per visualizzare l'elenco.)")
cat = input()
if cat == "":
for i,c in enumerate(categorie):
print("{}={}, ".format(c['id'], c['nome']))
else: finished = True
try: cat = int(cat)
except ValueError:
print("== Categoria non valida.")
exit(-1)
n = 1
print("\n== Risultati per: {}".format(query))
results = ricerca(query, cat=cat)
if len(results) == 0:
print("== Nessun risultato.")
exit(0)
for i,r in enumerate(results):
print("{}) {}\n {}{}".format(i+1, r['titolo'], "" if i < 9 else " ", r['didascalia']))
print("== Digita il numero del torrent da selezionare:")
t = int(input()) - 1
print("== Dettagli del torrent")
print("\n{}\n{}\n\nTorrent:\n{}".format(results[t]['titolo'], results[t]['didascalia'], results[t]['torrent']))
return 0
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.