mirror of
https://github.com/vikstrous/pirate-get
synced 2025-01-10 10:04:21 +01:00
test torrent command parser
This commit is contained in:
parent
0b8a27e64e
commit
383e5a101d
@ -66,6 +66,43 @@ def parse_cmd(cmd, url):
|
||||
return ret_no_quotes
|
||||
|
||||
|
||||
def parse_torrent_command(l):
|
||||
# Very permissive handling
|
||||
# Check for any occurances or d, f, p, t, m, or q
|
||||
cmd_code_match = re.search(r'([hdfpmtq])', l,
|
||||
flags=re.IGNORECASE)
|
||||
if cmd_code_match:
|
||||
code = cmd_code_match.group(0).lower()
|
||||
else:
|
||||
code = None
|
||||
|
||||
# Clean up command codes
|
||||
# Substitute multiple consecutive spaces/commas for single
|
||||
# comma remove anything that isn't an integer or comma.
|
||||
# Turn into list
|
||||
l = re.sub(r'^[hdfp, ]*|[hdfp, ]*$', '', l)
|
||||
l = re.sub('[ ,]+', ',', l)
|
||||
l = re.sub('[^0-9,-]', '', l)
|
||||
parsed_input = l.split(',')
|
||||
|
||||
# expand ranges
|
||||
choices = []
|
||||
# loop will generate a list of lists
|
||||
for elem in parsed_input:
|
||||
left, sep, right = elem.partition('-')
|
||||
if right:
|
||||
choices.append(list(range(int(left), int(right) + 1)))
|
||||
elif left != '':
|
||||
choices.append([int(left)])
|
||||
|
||||
# flatten list
|
||||
choices = sum(choices, [])
|
||||
# the current code stores the choices as strings
|
||||
# instead of ints. not sure if necessary
|
||||
choices = [elem for elem in choices]
|
||||
return code, choices
|
||||
|
||||
|
||||
def main():
|
||||
config = load_config()
|
||||
|
||||
@ -235,40 +272,7 @@ def main():
|
||||
return
|
||||
|
||||
try:
|
||||
# Very permissive handling
|
||||
# Check for any occurances or d, f, p, t, m, or q
|
||||
cmd_code_match = re.search(r'([hdfpmtq])', l,
|
||||
flags=re.IGNORECASE)
|
||||
if cmd_code_match:
|
||||
code = cmd_code_match.group(0).lower()
|
||||
else:
|
||||
code = None
|
||||
|
||||
# Clean up command codes
|
||||
# Substitute multiple consecutive spaces/commas for single
|
||||
# comma remove anything that isn't an integer or comma.
|
||||
# Turn into list
|
||||
l = re.sub(r'^[hdfp, ]*|[hdfp, ]*$', '', l)
|
||||
l = re.sub('[ ,]+', ',', l)
|
||||
l = re.sub('[^0-9,-]', '', l)
|
||||
parsed_input = l.split(',')
|
||||
|
||||
# expand ranges
|
||||
choices = []
|
||||
# loop will generate a list of lists
|
||||
for elem in parsed_input:
|
||||
left, sep, right = elem.partition('-')
|
||||
if right:
|
||||
choices.append(list(range(int(left), int(right) + 1)))
|
||||
elif left != '':
|
||||
choices.append([int(left)])
|
||||
|
||||
# flatten list
|
||||
choices = sum(choices, [])
|
||||
# the current code stores the choices as strings
|
||||
# instead of ints. not sure if necessary
|
||||
choices = [str(elem) for elem in choices]
|
||||
|
||||
code, choices = parse_torrent_command(l)
|
||||
# Act on option, if supplied
|
||||
print('')
|
||||
if code == 'h':
|
||||
|
@ -83,7 +83,6 @@ def search_results(results, local=None):
|
||||
|
||||
def descriptions(chosen_links, results, site):
|
||||
for link in chosen_links:
|
||||
link = int(link)
|
||||
path = '/torrent/%s/' % results[link]['id']
|
||||
req = request.Request(site + path, headers=pirate.data.default_headers)
|
||||
req.add_header('Accept-encoding', 'gzip')
|
||||
@ -108,7 +107,6 @@ def descriptions(chosen_links, results, site):
|
||||
|
||||
def file_lists(chosen_links, results, site):
|
||||
for link in chosen_links:
|
||||
link = int(link)
|
||||
path = '/ajax_details_filelist.php'
|
||||
query = '?id=' + results[link]['id']
|
||||
req = request.Request(site + path + query,
|
||||
|
@ -163,7 +163,6 @@ def get_torrent(info_hash):
|
||||
|
||||
def save_torrents(chosen_links, results, folder):
|
||||
for link in chosen_links:
|
||||
link = int(link)
|
||||
magnet = results[link]['magnet']
|
||||
name = re.search(r'dn=([^\&]*)', magnet)
|
||||
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
||||
@ -181,7 +180,6 @@ def save_torrents(chosen_links, results, folder):
|
||||
|
||||
def save_magnets(chosen_links, mags, folder):
|
||||
for link in chosen_links:
|
||||
link = int(link)
|
||||
magnet = results[link]['magnet']
|
||||
name = re.search(r'dn=([^\&]*)', magnet)
|
||||
torrent_name = parse.unquote(name.group(1)).replace('+', ' ')
|
||||
|
@ -17,5 +17,24 @@ class TestPirate(unittest.TestCase):
|
||||
for test in tests:
|
||||
self.assertEqual(pirate.pirate.parse_cmd(*test[0]), test[1])
|
||||
|
||||
def test_parse_torrent_command(self):
|
||||
tests = [
|
||||
[['h'], ('h', [])],
|
||||
[['q'], ('q', [])],
|
||||
[['d1'], ('d', [1])],
|
||||
[['f1'], ('f', [1])],
|
||||
[['p1'], ('p', [1])],
|
||||
[['t1'], ('t', [1])],
|
||||
[['m1'], ('m', [1])],
|
||||
[['d 23'], ('d', [23])],
|
||||
[['d 23,1'], ('d', [23, 1])],
|
||||
[['d 23, 1'], ('d', [23, 1])],
|
||||
[['1d'], ('d', [1])],
|
||||
[['1 ... d'], ('d', [1])],
|
||||
[['1-3 d'], ('d', [1,2,3])],
|
||||
]
|
||||
for test in tests:
|
||||
self.assertEqual(pirate.pirate.parse_torrent_command(*test[0]), test[1])
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
Loading…
Reference in New Issue
Block a user