Skip to content
This repository was archived by the owner on Mar 8, 2023. It is now read-only.

Commit 4fddc72

Browse files
authored
Merge pull request #4 from iawia002/weibo
fix m.weibo.cn
2 parents 34874ec + 984199c commit 4fddc72

5 files changed

Lines changed: 73 additions & 30 deletions

File tree

lulu/config.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,11 @@
113113
'Accept-Language': 'en-US,en;q=0.8',
114114
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:51.0) Gecko/20100101 Firefox/51.0', # noqa
115115
}
116+
117+
FAKE_HEADERS_MOBILE = {
118+
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', # noqa
119+
'Accept-Charset': 'UTF-8,*;q=0.5',
120+
'Accept-Encoding': 'gzip,deflate,sdch',
121+
'Accept-Language': 'en-US,en;q=0.8',
122+
'User-Agent': 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36', # noqa
123+
}

lulu/extractors/miaopai.py

Lines changed: 51 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
#!/usr/bin/env python
22

3-
__all__ = ['miaopai_download']
3+
import re
4+
import json
5+
6+
from lulu import config
7+
from lulu.common import (
8+
match1,
9+
url_info,
10+
url_size,
11+
print_info,
12+
get_content,
13+
download_urls,
14+
playlist_not_supported,
15+
)
416

5-
from ..common import *
6-
import urllib.error
7-
import urllib.parse
17+
__all__ = ['miaopai_download']
18+
site_info = 'miaopai'
819

9-
fake_headers_mobile = {
10-
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
11-
'Accept-Charset': 'UTF-8,*;q=0.5',
12-
'Accept-Encoding': 'gzip,deflate,sdch',
13-
'Accept-Language': 'en-US,en;q=0.8',
14-
'User-Agent': 'Mozilla/5.0 (Linux; Android 4.4.2; Nexus 4 Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.114 Mobile Safari/537.36'
15-
}
1620

17-
def miaopai_download_by_fid(fid, output_dir = '.', merge = False, info_only = False, **kwargs):
18-
'''Source: Android mobile'''
21+
def miaopai_download_by_fid(
22+
fid, output_dir='.', merge=False, info_only=False, **kwargs
23+
):
1924
page_url = 'http://video.weibo.com/show?fid=' + fid + '&type=mp4'
2025

21-
mobile_page = get_content(page_url, headers=fake_headers_mobile)
26+
mobile_page = get_content(page_url, headers=config.FAKE_HEADERS_MOBILE)
2227
url = match1(mobile_page, r'<video id=.*?src=[\'"](.*?)[\'"]\W')
2328
title = match1(mobile_page, r'<title>((.|\n)+?)</title>')
2429
if not title:
@@ -27,25 +32,43 @@ def miaopai_download_by_fid(fid, output_dir = '.', merge = False, info_only = Fa
2732
ext, size = 'mp4', url_info(url)[2]
2833
print_info(site_info, title, ext, size)
2934
if not info_only:
30-
download_urls([url], title, ext, total_size=None, output_dir=output_dir, merge=merge)
35+
download_urls(
36+
[url], title, ext, total_size=None, output_dir=output_dir,
37+
merge=merge
38+
)
3139

32-
#----------------------------------------------------------------------
33-
def miaopai_download(url, output_dir = '.', merge = False, info_only = False, **kwargs):
40+
41+
def miaopai_download(
42+
url, output_dir='.', merge=False, info_only=False, **kwargs
43+
):
3444
fid = match1(url, r'\?fid=(\d{4}:\w{32})')
35-
if fid is not None:
45+
if fid:
3646
miaopai_download_by_fid(fid, output_dir, merge, info_only)
3747
elif '/p/230444' in url:
3848
fid = match1(url, r'/p/230444(\w+)')
3949
miaopai_download_by_fid('1034:'+fid, output_dir, merge, info_only)
4050
else:
41-
mobile_page = get_content(url, headers = fake_headers_mobile)
42-
hit = re.search(r'"page_url"\s*:\s*"([^"]+)"', mobile_page)
43-
if not hit:
44-
raise Exception('Unknown pattern')
45-
else:
46-
escaped_url = hit.group(1)
47-
miaopai_download(urllib.parse.unquote(escaped_url), output_dir=output_dir, merge=merge, info_only=info_only, **kwargs)
48-
49-
site_info = "miaopai"
51+
mobile_page = get_content(url, headers=config.FAKE_HEADERS_MOBILE)
52+
match_rule = re.compile(
53+
r'var \$render_data = \[(.*?)\]\[0\]',
54+
re.DOTALL
55+
)
56+
video_info = json.loads(match_rule.findall(mobile_page)[0])
57+
video_url = video_info['status']['page_info']['media_info'][
58+
'stream_url'
59+
]
60+
title = video_info['status']['page_info']['content2']
61+
video_format = 'mp4'
62+
size = url_size(video_url)
63+
print_info(
64+
site_info=site_info, title=title, type=video_format, size=size
65+
)
66+
if not info_only:
67+
download_urls(
68+
urls=[video_url], title=title, ext=video_format,
69+
total_size=size, **kwargs
70+
)
71+
72+
5073
download = miaopai_download
51-
download_playlist = playlist_not_supported('miaopai')
74+
download_playlist = playlist_not_supported(site_info)

lulu/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22

3-
__version__ = '0.1.1'
3+
__version__ = '0.1.2'
44
script_name = 'lulu'

tests/main.py renamed to tests/download.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
youtube,
99
yixia,
1010
bilibili,
11+
douyin,
12+
miaopai,
1113
)
1214

1315

@@ -46,6 +48,15 @@ def test_bilibili(self):
4648
'https://www.bilibili.com/video/av13228063/', info_only=True
4749
)
4850

51+
def test_douyin(self):
52+
douyin.download(
53+
'https://www.douyin.com/share/video/6492273288897629454',
54+
info_only=True
55+
)
56+
57+
def test_weibo(self):
58+
miaopai.download('https://m.weibo.cn/status/FEFq863WF', info_only=True)
59+
4960

5061
if __name__ == '__main__':
5162
unittest.main()

tests/runtests.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@
55

66

77
TEST_MODULES = [
8-
'tests.main',
8+
'tests.download',
99
]
1010

1111

1212
def all():
1313
return unittest.defaultTestLoader.loadTestsFromNames(TEST_MODULES)
1414

15+
1516
if __name__ == '__main__':
1617
unittest.main(defaultTest='all')

0 commit comments

Comments
 (0)