Created
March 5, 2013 14:43
-
-
Save rafapolo/5090751 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
#-*- coding: UTF-8 -*- | |
# | |
# mini_info.py v0.1 | |
# | |
# author: Rafael Polo | |
# updated on 20/05/09. | |
# | |
# [email protected] | |
from bdecoder import decode | |
from bencode import bencode | |
from sha1 import sha | |
from urllib import quote | |
from urllib import urlopen | |
def decode_torrent_file(path): | |
path = path.strip() | |
try: | |
file = '' | |
if path.startswith('http'): | |
file = urlopen(path) | |
else: | |
file = open(path) | |
torrent_file = file.read() | |
torrent = decode(torrent_file) | |
except IOError: | |
return None | |
#print "Erro ao abrir arquivo." | |
except SyntaxError: | |
return None | |
#print "Erro ao decodificar torrent." | |
else: | |
file.close() | |
return torrent | |
# pega o hash do torrent | |
def get_hash(torrent): | |
info = bencode(torrent['info']) | |
sha1 = sha() | |
sha1.update(info) | |
return sha1.digest() | |
# descobre URL do scrape do torrent pela URL do announce | |
def get_scrape_url(torrent): | |
return torrent['announce'].replace('announce', 'scrape') | |
# requisita informações do scrape | |
def get_scrape_result(hash, scrape_url): | |
url_encoded_hash = quote(hash) | |
scrape = "%s/?info_hash=%s" % (scrape_url.strip(), url_encoded_hash) | |
try: | |
answer = decode(urlopen(scrape).read()) | |
hash_file = answer['files'].keys()[0] | |
except IndexError: | |
#print "<b>Tracker não tem esse arquivo.</b>" | |
return None | |
except KeyError: | |
#print "<b>Tracker não respondeu corretamente.</b>" | |
return None | |
except IOError: | |
#print "<b>Tracker não respondeu.</b>" | |
return None | |
except SyntaxError: | |
#print "<b>Tracker não respondeu como esperado.</b>" | |
return None | |
else: | |
return answer['files'][hash_file] | |
def get_mini_info(file): | |
torrent = decode_torrent_file(file) | |
if (torrent): | |
info = get_scrape_result(get_hash(torrent), get_scrape_url(torrent)) | |
if info: | |
return "Seeders: %s, Leechers: %s, Downloads: %s" % (info['incomplete'], info['complete'], info['downloaded']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment