import os
import torrent_parser as tp
import re
import logging
import traceback


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning("Failed to decode torrent")
        logging.warning(traceback.format_exc())
        return None


hre = re.compile(r'\[\w+\]\s?(?P<tname>[\s\S]+)')
def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname,ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None

    if tname[:2] == "._":
        tname = tname[2:]

    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')

    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


if __name__ == '__main__':
    with open('test.torrent','rb') as f:
        name = get_name(f.read())
    assert len(name) > 0
    print(name)

    tname = get_tname_byfname('hello.torrent')
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent', heads=['[TTG]',])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('._[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[HDR] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[OpenCD]hello.torrent.added', heads=None)
    assert tname == 'hello'

8dfe4eadac0f7cadb9ce181ea39a371c91447f52
<mask token>


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning('Failed to decode torrent')
        logging.warning(traceback.format_exc())
        return None


<mask token>


def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname, ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None
    if tname[:2] == '._':
        tname = tname[2:]
    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')
    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


<mask token>

<mask token>


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning('Failed to decode torrent')
        logging.warning(traceback.format_exc())
        return None


<mask token>


def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname, ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None
    if tname[:2] == '._':
        tname = tname[2:]
    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')
    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


if __name__ == '__main__':
    with open('test.torrent', 'rb') as f:
        name = get_name(f.read())
    assert len(name) > 0
    print(name)
    tname = get_tname_byfname('hello.torrent')
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('._[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[HDR] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[OpenCD]hello.torrent.added', heads=None)
    assert tname == 'hello'

<mask token>


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning('Failed to decode torrent')
        logging.warning(traceback.format_exc())
        return None


hre = re.compile('\\[\\w+\\]\\s?(?P<tname>[\\s\\S]+)')


def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname, ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None
    if tname[:2] == '._':
        tname = tname[2:]
    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')
    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


if __name__ == '__main__':
    with open('test.torrent', 'rb') as f:
        name = get_name(f.read())
    assert len(name) > 0
    print(name)
    tname = get_tname_byfname('hello.torrent')
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('._[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[HDR] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[OpenCD]hello.torrent.added', heads=None)
    assert tname == 'hello'

import os
import torrent_parser as tp
import re
import logging
import traceback


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning('Failed to decode torrent')
        logging.warning(traceback.format_exc())
        return None


hre = re.compile('\\[\\w+\\]\\s?(?P<tname>[\\s\\S]+)')


def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname, ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None
    if tname[:2] == '._':
        tname = tname[2:]
    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')
    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


if __name__ == '__main__':
    with open('test.torrent', 'rb') as f:
        name = get_name(f.read())
    assert len(name) > 0
    print(name)
    tname = get_tname_byfname('hello.torrent')
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('._[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[HDR] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[OpenCD]hello.torrent.added', heads=None)
    assert tname == 'hello'

import os
import torrent_parser as tp
import re
import logging
import traceback


def get_name(tdata):
    try:
        b = tp.decode(tdata)
        return b['info']['name']
    except Exception:
        logging.warning("Failed to decode torrent")
        logging.warning(traceback.format_exc())
        return None


hre = re.compile(r'\[\w+\]\s?(?P<tname>[\s\S]+)')
def get_tname_byfname(file_name, heads=[]):
    tname, ext = os.path.splitext(file_name)
    if ext == '.added':
        tname,ext = os.path.splitext(tname)
    if ext != '.torrent':
        return None

    if tname[:2] == "._":
        tname = tname[2:]

    hm = hre.match(tname)
    if hm:
        tname = hm.group('tname')

    for head in heads:
        if tname.startswith(head):
            tname = tname[len(head):].lstrip()
    return tname


if __name__ == '__main__':
    with open('test.torrent','rb') as f:
        name = get_name(f.read())
    assert len(name) > 0
    print(name)

    tname = get_tname_byfname('hello.torrent')
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent', heads=['[TTG]',])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('._[TTG] hello.torrent.added', heads=['[TTG]'])
    assert tname == 'hello'
    tname = get_tname_byfname('[TTG] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[HDR] hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('hello.torrent.added', heads=None)
    assert tname == 'hello'
    tname = get_tname_byfname('[OpenCD]hello.torrent.added', heads=None)
    assert tname == 'hello'
