Example #1
0
def findepisode(showid,season,episode):
    """
    Return torrents which match the episode wanted
    """
    showname = tv.shows[showid]["name"].translate(dict((ord(c), u'') for c in u',.\'"<>'))
    
    url = str.format(urlformat,**{"show":showname,"season":season,"episode":episode})
    print url
    data = gzipdecode(urllib2.urlopen(url))
    data = BeautifulSoup(data)
    matches = data.findAll("item")
    
    validmatches = []
    
    for m in matches:
        torurl = m.enclosure["url"]
        if torurl is not None:
            try:
                tordata = gzipdecode(urllib2.urlopen(torurl))
                if tordata is not None:
                    tordatadecoded = bencode.bdecode(tordata)           
                    if isvalid(tordatadecoded):
                        validmatches.append(convert(m,tordatadecoded,tordata))
            except urllib2.HTTPError:
                pass
    
    validmatches.sort(key = lambda x: -1*(x["peers"] + x["verified"]*1000))
    return validmatches
Example #2
0
 def is_clean(self):
     '''
         Check if the file is clean from harmful metadatas
     '''
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             if decoded[key] != '':
                 return False
         except:
             pass
     return True
Example #3
0
File: misc.py Project: gutobenn/MAT
    def remove_all(self):
        """ Remove all comprimizing fields
        """
        with open(self.filename, 'r') as f:
            decoded = bencode.bdecode(f.read())

        cleaned = self.__remove_all_recursively(decoded)

        with open(self.output, 'w') as f:  # encode the decoded torrent
            f.write(bencode.bencode(cleaned))  # and write it in self.output

        self.do_backup()
        return True
Example #4
0
    def remove_all(self):
        """ Remove all comprimizing fields
        """
        with open(self.filename, 'r') as f:
            decoded = bencode.bdecode(f.read())

        cleaned = self.__remove_all_recursively(decoded)

        with open(self.output, 'w') as f:  # encode the decoded torrent
            f.write(bencode.bencode(cleaned))  # and write it in self.output

        self.do_backup()
        return True
Example #5
0
 def is_clean(self):
     """
         Check if the file is clean from harmful metadatas
     """
     with open(self.filename, "r") as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             if decoded[key]:
                 return False
         except KeyError:
             pass
     return True
Example #6
0
File: misc.py Project: Rafiot/MAT
    def remove_all(self):
        """ Remove all comprimizing fields
        """
        with open(self.filename, 'r') as f:
            decoded = bencode.bdecode(f.read())

        cleaned = {i: j for i, j in list(decoded.items()) if i in self.fields}

        with open(self.output, 'w') as f:  # encode the decoded torrent
            f.write(bencode.bencode(cleaned))  # and write it in self.output

        self.do_backup()
        return True
Example #7
0
 def get_meta(self):
     """
         Return a dict with all the meta of the file
     """
     metadata = {}
     with open(self.filename, "r") as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             if decoded[key]:
                 metadata[key] = decoded[key]
         except KeyError:
             pass
     return metadata
Example #8
0
 def remove_all(self):
     '''
         Remove all the files that are compromizing
     '''
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             decoded[key] = ''
         except:
             pass
     with open(self.output, 'w') as f:  # encode the decoded torrent
         f.write(bencode.bencode(decoded))  # and write it in self.output
     self.do_backup()
Example #9
0
 def get_meta(self):
     '''
         Return a dict with all the meta of the file
     '''
     metadata = {}
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             if decoded[key] != '':
                 metadata[key] = decoded[key]
         except:
             pass
     return metadata
Example #10
0
    def remove_all(self):
        """ Remove all comprimizing fields
        """
        decoded = ''
        with open(self.filename, 'r') as f:
            decoded = bencode.bdecode(f.read())

        cleaned = {i: j for i, j in list(decoded.items()) if i in self.fields}

        with open(self.output, 'w') as f:  # encode the decoded torrent
            f.write(bencode.bencode(cleaned))  # and write it in self.output

        self.do_backup()
        return True
Example #11
0
 def remove_all(self):
     """
         Remove all the files that are compromising
     """
     with open(self.filename, "r") as f:
         decoded = bencode.bdecode(f.read())
     for key in self.fields:
         try:
             decoded[key] = ""
         except KeyError:
             pass
     with open(self.output, "w") as f:  # encode the decoded torrent
         f.write(bencode.bencode(decoded))  # and write it in self.output
     self.do_backup()
     return True
Example #12
0
    def decode(self):
        '''
        Dekodovanie bencode torrentu.
        @return True v pridade uspesneho spracovania
        '''

        torrent, bdictionary = None, None

        try:
            torrent = open(self.path, 'rb')
        except:
            Log.error("Torrent: Nepodarilo sa otvorit torrent {}".format(self.path))
            return False

        try:
            bdictionary = bnc.bdecode(torrent.read())
        except:
            Log.error("Torrent: Nepodarilo sa dekodovat bencode v torrente {}".format(self.path))
            return False

        # Spracovanie info sekcie
        info               = bdictionary['info']
        self.info_hash     = hashlib.sha1(bnc.bencode(info)).hexdigest()
        self.info_hash_url = urllib.quote_plus(hashlib.sha1(bnc.bencode(info)).digest())
        self.name          = info['name']

        # Ziskanie trackerov
        try:
            announce_list = bdictionary['announce-list']
            for al in announce_list:
                if isinstance(al, list):
                    for a in al:
                        self.announce_list.append(a)
                else:
                    self.announce_list.append(a)
            self.announce_list = set(self.announce_list)
        except:
            self.announce_list.append(bdictionary['announce'])

        return True
Example #13
0
def decode(btfile):
    with open(btfile, 'rb') as fp:
        data = fp.read()
    torrent = bencode.bdecode(data)
    return torrent
Example #14
0
File: misc.py Project: gutobenn/MAT
 def get_meta(self):
     """ Return a dict with all the meta of the file
     """
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     return self.__get_meta_recursively(decoded)
Example #15
0
File: misc.py Project: gutobenn/MAT
 def is_clean(self):
     """ Check if the file is clean from harmful metadata
     """
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     return self.fields.issuperset(self.__get_key_recursively(decoded))
Example #16
0
import os
from types import StringType
# get bencode package from http://github.com/fishy/scripts/downloads
from bencode.bencode import bencode, bdecode, BTFailure

try :
	torrent = sys.argv[1]
except IndexError :
	print "Usage: \"%s <torrent_file> [tracker_url]\" to show torrent info (without tracker_url), or to add tracker(s)" % sys.argv[0]
	sys.exit()

size = os.stat(torrent).st_size
file = open(torrent, "rb")
data = file.read(size)
file.close()
info = bdecode(data)

if len(sys.argv) == 2 :
	print info
	sys.exit()

if 'announce-list' not in info :
	list = [info['announce']]
	for i in range(len(sys.argv)-2) :
		tracker = sys.argv[i+2]
		if tracker not in list :
			list.append(tracker)
	print list
	info['announce-list'] = [list]
else :
	list = info['announce-list'][0]
Example #17
0
 def get_meta(self):
     """ Return a dict with all the meta of the file
     """
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     return self.__get_meta_recursively(decoded)
Example #18
0
 def is_clean(self):
     """ Check if the file is clean from harmful metadata
     """
     with open(self.filename, 'r') as f:
         decoded = bencode.bdecode(f.read())
     return self.fields.issuperset(self.__get_key_recursively(decoded))
Example #19
0
    def get_peerlist(self):
        '''
        Ziskanie peerlistu z trackeru.
        '''

        if self.socket:
            data = ""
            cdata = []
            tries = TRIES
            query_string = self.query

            if not query_string:
                query_string = "?info_hash={}&peer_id=AAAAABBBBBCCCCCDDDDD&downloaded=0&uploaded=0&left=1&port=6881&compact=1".format(
                    self.info_hash)

            Log.debug(
                "Tracker: Odosielana poziadavka: {}".format(query_string))
            request_string = \
                "GET " + self.path + query_string +" HTTP/1.1\r\n" + \
                "Connection: keep-alive\r\n" + \
                "Accept-Encoding: gzip\r\n" + \
                "\r\n"

            while tries > 0:
                try_nr = TRIES - tries + 1
                try:
                    self.socket.sendall(request_string)
                except:
                    Log.error(
                        "Tracker: Pokus {}/{}: Odoslanie poziadavku na tracker zlyhalo"
                        .format(try_nr, TRIES))
                    tries -= 1
                    if tries > 0: time.sleep(SLEEP_TIME)
                    continue

                # http://www.binarytides.com/receive-full-data-with-the-recv-socket-function-in-python/
                begin = time.time()
                errflg = False
                while True:
                    if cdata and time.time() - begin > RECV_TIMEOUT:
                        break
                    elif time.time() - begin > RECV_TIMEOUT * 2:
                        break

                    try:
                        data = (self.socket.recv(RECV_SIZE))
                        if data:
                            cdata.append(data)
                            begin = time.time()
                        else:
                            time.sleep(0.1)
                    except socket.timeout:
                        Log.error(
                            "Tracker: Pokus {}/{}: Bol dosiahnuty timeout (socket.timeout)"
                            .format(try_nr, TRIES))
                        errflg = True
                        break
                    except socket.error:
                        Log.error(
                            "Tracker: Pokus {}/{}: Chyba socketu (socket.error)"
                            .format(try_nr, TRIES))
                        errflg = True
                        break
                    except:
                        Log.error(
                            "Tracker: Pokus {}/{}: Neznama chyba pri komunikacii s trackerom"
                            .format(try_nr, TRIES))
                        errflg = True
                        break

                if errflg:
                    tries -= 1
                    if tries > 0: time.sleep(SLEEP_TIME)
                    continue

                data = ''.join(cdata)

                # Zisti navratovy status kod
                status = re.search(r'HTTP\/1\.\d\s(.*?)\r\n', data)
                if not status:
                    Log.error(
                        "Tracker: Pokus {}/{}: Neplatna/nezmyslena odpoved serveru"
                        .format(try_nr, TRIES))
                    tries -= 1
                    if tries > 0: time.sleep(SLEEP_TIME)
                    continue

                if status.group(1) is not None and status.group(1) == "200 OK":
                    bstring = re.search(r'\r\n\r\n(.*)', data, re.DOTALL)
                    if bstring:
                        bstring = bstring.group(1)
                    else:
                        Log.error(
                            "Tracker: Pokus {}/{}: Nepodarilo sa ziskat peerlist z odpovede serveru"
                            .format(try_nr, TRIES))
                        tries -= 1
                        if tries > 0: time.sleep(SLEEP_TIME)
                        continue

                    # Obcas moze nastat chyba pri bdecode
                    bdecoded = None
                    try:
                        bdecoded = bnc.bdecode(bstring)
                    except:
                        Log.error(
                            "Tracker: Pokus {}/{}: Nepodarilo sa dekodovat bencode z odpovede trackeru \"{}\""
                            .format(try_nr, TRIES, bstring))
                        tries -= 1
                        if tries > 0: time.sleep(SLEEP_TIME)
                        continue

                    bpeers = None
                    try:
                        bpeers = bdecoded['peers']
                    except:
                        Log.error(
                            "Tracker: Pokus {}/{}: V odpovedi trackeru nie je kluc peers "
                            .format(try_nr, TRIES))
                        tries -= 1
                        if tries > 0: time.sleep(SLEEP_TIME)
                        continue

                    offset = 0
                    peers = []

                    # Dekoduj peerlist (!i = int, !H = ushort)
                    try:
                        while offset < len(bpeers):
                            ip = struct.unpack_from("!i", bpeers, offset)[0]
                            ip = socket.inet_ntoa(struct.pack("!i", ip))
                            offset += 4
                            port = struct.unpack_from("!H", bpeers, offset)[0]
                            offset += 2
                            peers.append("{}:{}".format(ip, port))
                    except:
                        Log.error(
                            "Tracker: Pokus {}/{}: Chyba pri dekodovani peerlistu"
                            .format(try_nr, TRIES))
                        tries -= 1
                        if tries > 0: time.sleep(SLEEP_TIME)
                        continue

                    return peers
                else:
                    Log.error(
                        "Tracker: Pokus {}/{}: Vrateny stavovy kod \'{}\'".
                        format(try_nr, TRIES, status.group(1)))
                    tries -= 1
                    if tries > 0: time.sleep(SLEEP_TIME)
                    continue

            Log.error(
                "Tracker: Dosiahnuty maximalny pocet pokusov - Neboli ziskane ziadne data z trackeru"
            )
            return None