Last active
December 17, 2015 06:48
-
-
Save zhasm/5567544 to your computer and use it in GitHub Desktop.
迅雷任务添加。 * 自动添加字符串中的可下载链接,例如 ed2k, magnet, thunder。
* 自动解析verycd 链接;
* 支持递归解析(不常用) 需要 安装 requests 库。(sudo pip install requests; 或 sudo easy_install requests) 默认的 离线迅雷代码库 在XL_PATH='python $HOME/git/lx/lixian_cli.py' 。如果跟你的路径不一样,请修改该变量。
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 | |
# -*- encoding: utf-8 -*- | |
# | |
# Author: Rex Zhang | |
# Create Time: 2013-01-29 11:50 | |
# File name: xladd.py | |
""" | |
usage: | |
xladd "string that contains downloadable resources, like ed2k, magnet, thunder." | |
xladd http://www.verycd.com/topics/2953431/ | |
""" | |
import re | |
import os | |
import requests | |
from pprint import pprint | |
XL_PATH='python $HOME/git/lx/lixian_cli.py' | |
def getArgs(): | |
"""show argpase snippets""" | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('links', nargs='+', | |
help='') | |
parser.add_argument('-d', '--deep', action='store_false', | |
help='parse http to get links') | |
return parser.parse_args() | |
def _deep_link(link): | |
if link and 'http' in link.lower(): | |
deeper = False | |
if 'verycd.com' in link.lower(): | |
deeper = True | |
link = link.replace('verycd.com', 'verycd.gdajie.com') | |
content = requests.get(link).content | |
links = flatten_list(_parse_link(content, deep=False)) | |
if deeper : | |
links = [_deep_link(i) for i in links if 'detail.htm' in i] | |
links = flatten_list(links) | |
links = [i for i in links if not i.lower().startswith('http')] | |
return links | |
else: | |
return link | |
def _parse_link(link, deep=True): | |
ret = [] | |
link_types = ['ed2k', | |
'magnet', | |
'thunder', | |
'http'] | |
types = '|'.join(link_types) | |
regex_str = r'''(?i)(?:%s):[^"']{3,}''' % types | |
regex = re.compile(regex_str) | |
ret += regex.findall(link) | |
if deep: | |
ret = [_deep_link(r) for r in ret] | |
return ret | |
def flatten_list(l): | |
ret = [] | |
if not l: | |
return [] | |
if not isinstance(l, list): | |
return [l] | |
for i in l: | |
if not isinstance(i, list): | |
ret.append(i) | |
else: | |
ret += flatten_list(i) | |
return list(set(ret)) | |
def get_links(args): | |
ret = [] | |
for link in args.links: | |
if '\n' in link: | |
links = [i.strip() for i in link.splitlines()] | |
for link in links: | |
ret += _parse_link(link, args.deep) | |
else: | |
ret += _parse_link(link, args.deep) | |
return flatten_list(ret) | |
def do(cmd): | |
for i in os.popen(cmd).readlines(): | |
i = i.strip() | |
if i: | |
print i | |
def add_task(links): | |
for link in links: | |
cmd = '''%(xl)s add "%(link)s"''' % { | |
'xl': XL_PATH, | |
'link': link, | |
} | |
do(cmd) | |
if __name__ == '__main__': | |
args = getArgs() | |
links = get_links(args) | |
add_task(links) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment