Skip to content

Commit

Permalink
支持http和socks代理
Browse files Browse the repository at this point in the history
  • Loading branch information
0xHJK committed Jan 25, 2019
1 parent 5213971 commit 05c6198
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 6 deletions.
4 changes: 4 additions & 0 deletions core/extractors/baidu.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def baidu_search(keyword) -> list:
s = requests.Session()
s.headers.update(glovar.FAKE_HEADERS)
s.headers.update({'referer': 'http://music.baidu.com/'})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

music_list = []
r = s.get('http://musicapi.qianqian.com/v1/restserver/ting', params=params)
Expand All @@ -51,6 +53,8 @@ def baidu_search(keyword) -> list:
if mr.status_code != requests.codes.ok:
raise RequestError(mr.text)
mj = mr.json()
if not mj['data']['songList']:
continue
mj_music = mj['data']['songList'][0]
music['duration'] = str(datetime.timedelta(seconds=mj_music['time']))
size = mj_music['size'] or 0
Expand Down
9 changes: 7 additions & 2 deletions core/extractors/flac.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def flac_search(keyword) -> list:
}

music_list = []
r = requests.get('http://sug.music.baidu.com/info/suggestion', params=params)
r = requests.get('http://sug.music.baidu.com/info/suggestion', params=params,
proxies=glovar.get_option('proxies'))
if r.status_code != requests.codes.ok:
raise RequestError(r.text)
j = r.json()
Expand All @@ -39,12 +40,16 @@ def flac_search(keyword) -> list:
'source': 'flac'
}
m_params = {'songIds': music['id'], 'type': 'flac'}
mr = requests.get('http://music.baidu.com/data/music/fmlink', params=m_params)
# 不在同一个session能提高请求成功率
mr = requests.get('http://music.baidu.com/data/music/fmlink', params=m_params,
proxies=glovar.get_option('proxies'))
if mr.status_code != requests.codes.ok:
raise RequestError(mr.text)
mj = mr.json()
if mj['errorCode'] != 22000:
continue
if not mj['data']['songList']:
continue
mj_music = mj['data']['songList'][0]
music['duration'] = str(datetime.timedelta(seconds=mj_music['time']))
size = mj_music['size'] or 0
Expand Down
4 changes: 4 additions & 0 deletions core/extractors/kugou.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def kugou_search(keyword) -> list:
s = requests.Session()
s.headers.update(glovar.FAKE_HEADERS)
s.headers.update({'referer': 'http://www.kugou.com'})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

music_list = []
r = s.get('http://songsearch.kugou.com/song_search_v2', params=params)
Expand Down Expand Up @@ -74,6 +76,8 @@ def kugou_download(music):
'referer': 'http://m.kugou.com',
'User-Agent': glovar.IOS_USERAGENT
})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

r = s.get('http://m.kugou.com/app/i/getSongInfo.php', params=params)
if r.status_code != requests.codes.ok:
Expand Down
6 changes: 6 additions & 0 deletions core/extractors/netease.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ def netease_search(keyword) -> list:
s.headers.update({
'referer': 'http://music.163.com/',
})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

r = s.post('http://music.163.com/api/linux/forward', data=data)

if r.status_code != requests.codes.ok:
Expand Down Expand Up @@ -100,6 +103,9 @@ def netease_download(music):
s.headers.update({
'referer': 'http://music.163.com/',
})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

r = s.post('http://music.163.com/api/linux/forward', data=data)

if r.status_code != requests.codes.ok:
Expand Down
4 changes: 4 additions & 0 deletions core/extractors/qq.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def qq_search(keyword) -> list:
'referer': 'http://m.y.qq.com',
'User-Agent': glovar.IOS_USERAGENT
})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

music_list = []
r = s.get('http://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp', params=params)
Expand Down Expand Up @@ -82,6 +84,8 @@ def qq_download(music):
'referer': 'http://y.qq.com',
'User-Agent': glovar.IOS_USERAGENT
})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

r = s.get('http://base.music.qq.com/fcgi-bin/fcg_musicexpress.fcg', params=params)
if r.status_code != requests.codes.ok:
Expand Down
6 changes: 4 additions & 2 deletions core/extractors/xiami.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def xiami_search(keyword) -> list:
# 获取cookie
s.head('http://m.xiami.com')
s.headers.update({'referer': 'http://m.xiami.com/'})
if glovar.get_option('proxies'):
s.proxies.update(glovar.get_option('proxies'))

music_list = []
r = s.get('http://api.xiami.com/web', params=params)
Expand Down Expand Up @@ -82,6 +84,8 @@ def xiami_music_info(music, music_list, s):
if mr.status_code != requests.codes.ok:
raise RequestError(mr.text)
mj = mr.json()
if not mj['data']['trackList']:
return
mj_music = mj['data']['trackList'][0]
music['duration'] = str(datetime.timedelta(seconds=mj_music['length']))
music['rate'] = 128
Expand All @@ -103,5 +107,3 @@ def xiami_music_info(music, music_list, s):

search = xiami_search
download = xiami_download


7 changes: 5 additions & 2 deletions utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def set_opts(args):
:return:
'''
try:
opts, others = getopt.getopt(args, 'vhmk:s:c:o:',
opts, others = getopt.getopt(args, 'vhmk:s:c:o:x:',
['verbose', 'help', 'merge', 'nomerge',
'keyword=', 'source=', 'count=', 'outdir='])
'keyword=', 'source=', 'count=', 'outdir=', 'proxy='])
except getopt.GetoptError as e:
logger.error('命令解析失败')
logger.error(e)
Expand All @@ -48,6 +48,9 @@ def set_opts(args):
glovar.set_option('count', c)
elif o in ('-o', '--outdir'):
glovar.set_option('outdir', a)
elif o in ('-x', '--proxy'):
proxies = { 'http': a, 'https': a}
glovar.set_option('proxies', proxies)
elif o in ('-m', '--merge'):
glovar.set_option('merge', True)
elif o in ('--nomerge'):
Expand Down
1 change: 1 addition & 0 deletions utils/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def usage():
print('\t%-16s %s' % ('-s --source= ', '数据源目前支持qq netease kugou baidu xiami flac'))
print('\t%-16s %s' % ('-c --count= ', '数量限制'))
print('\t%-16s %s' % ('-o --outdir= ', '指定输出目录'))
print('\t%-16s %s' % ('-x --proxy= ', '指定代理(如http://127.0.0.1:1087)'))
print('example: python main.py -k "周杰伦" -s "qq netease kugou baidu xiami" -c 10 -o "/tmp"')


Expand Down

0 comments on commit 05c6198

Please sign in to comment.