Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.idea
.vscode
download
.venv
__pycache__
56 changes: 55 additions & 1 deletion amemv-video-ripper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def get_dytk(url):
res = requests.get(url, headers=HEADERS)
if not res:
return None
dytk = re.findall("dytk: '(.*)'", res.content.decode('utf-8'))
dytk = re.findall(r'dytk: ["\'](.*)["\']', res.content.decode('utf-8'))
if len(dytk):
return dytk[0]
return None
Expand All @@ -136,6 +136,7 @@ def __init__(self, items):
self.numbers = []
self.challenges = []
self.musics = []
self.videos = []
for i in range(len(items)):
url = get_real_address(items[i])
if not url:
Expand All @@ -146,6 +147,8 @@ def __init__(self, items):
self.challenges.append(url)
if re.search('share/music', url):
self.musics.append(url)
if re.search('share/video', url):
self.videos.append(url)

self.queue = Queue.Queue()
self.scheduling()
Expand All @@ -167,6 +170,8 @@ def scheduling(self):
self.download_challenge_videos(url)
for url in self.musics:
self.download_music_videos(url)
for url in self.videos:
self.download_videos(url)

def download_user_videos(self, url):
number = re.findall(r'share/user/(\d+)', url)
Expand Down Expand Up @@ -205,6 +210,22 @@ def download_music_videos(self, url):
(musics_id, video_count))
print("\nFinish Downloading All the videos from @%s\n\n" % musics_id)

def download_videos(self, url):
video = re.findall('share/video/(\d+)', url)
if not len(video):
return
video_id = video[0]
hostname = urllib.parse.urlparse(url).hostname
dytk = get_dytk(f'https://{hostname}/share/video/{video_id}/?mid')
hostname = urllib.parse.urlparse(url).hostname
if hostname != 't.tiktok.com' and not dytk:
return
video_count = self._download_video_media(video_id, dytk, url)
self.queue.join()
print("\nAweme video @%s, video number %d\n\n" %
(video_id, video_count))
print("\nFinish Downloading All the videos from @%s\n\n" % video_id)

def _join_download_queue(self, aweme, target_folder):
try:
if aweme.get('video', None):
Expand Down Expand Up @@ -448,6 +469,39 @@ def _download_music_media(self, music_id, url):
print("There's no video in music %s." % music_id)
return video_count

def _download_video_media(self, video_id, dytk, url):
if not video_id:
print("Video #%s does not exist" % video_id)
return
current_folder = os.getcwd()
target_folder = os.path.join(current_folder, 'download/@%s' % video_id)
if not os.path.isdir(target_folder):
os.mkdir(target_folder)

hostname = urllib.parse.urlparse(url).hostname
signature = self.generateSignature(str(video_id))
url = "https://%s/web/api/v2/aweme/iteminfo/?{0}" % hostname
params = {
'item_ids': str(video_id),
'dytk': str(dytk)
}

video_count = 0
while True:
res = self.requestWebApi(url, params)
if not res:
break
item_list = res.get('item_list', [])
if not item_list:
break
for item in item_list:
video_count += 1
self._join_download_queue(item, target_folder)
break
if video_count == 0:
print("There's no video in video %s." % video_id)
return int(video_id)

def requestWebApi(self, url, params):
headers = copy.deepcopy(HEADERS)
headers['cookie'] = '_ga=GA1.2.1280899533.15586873031; _gid=GA1.2.2142818962.1559528881'
Expand Down