auto-archiver/archivers/tiktok_archiver.py

72 wiersze
2.7 KiB
Python
Czysty Zwykły widok Historia

2022-02-21 13:19:09 +00:00
import os, traceback
import tiktok_downloader
from loguru import logger
from .base_archiver import Archiver, ArchiveResult
2022-05-09 15:45:54 +00:00
from storages import Storage
2022-02-21 13:19:09 +00:00
2022-06-03 16:30:12 +00:00
2022-02-21 13:19:09 +00:00
class TiktokArchiver(Archiver):
name = "tiktok"
2022-02-21 13:19:09 +00:00
def download(self, url, check_if_exists=False):
if 'tiktok.com' not in url:
return False
status = 'success'
try:
info = tiktok_downloader.info_post(url)
key = self.get_key(f'{info.id}.mp4')
2022-06-08 09:11:09 +00:00
filename = os.path.join(Storage.TMP_FOLDER, key)
2022-06-07 16:41:58 +00:00
logger.info(f'found video {key=}')
2022-02-21 13:19:09 +00:00
if check_if_exists and self.storage.exists(key):
status = 'already archived'
2022-02-21 13:19:09 +00:00
media = tiktok_downloader.snaptik(url).get_media()
2022-02-21 13:19:09 +00:00
if len(media) <= 0:
if status == 'already archived':
2022-06-07 16:41:58 +00:00
return ArchiveResult(status='Could not download media, but already archived', cdn_url=self.storage.get_cdn_url(key))
else:
return ArchiveResult(status='Could not download media')
2022-02-21 13:19:09 +00:00
2022-06-07 16:41:58 +00:00
logger.info(f'downloading video {key=}')
media[0].download(filename)
2022-02-21 13:19:09 +00:00
if status != 'already archived':
2022-06-07 16:41:58 +00:00
logger.info(f'uploading video {key=}')
self.storage.upload(filename, key)
2022-02-21 13:19:09 +00:00
try:
2022-02-23 15:07:58 +00:00
key_thumb, thumb_index = self.get_thumbnails(filename, key, duration=info.duration)
except Exception as e:
logger.error(e)
2022-02-21 13:19:09 +00:00
key_thumb = ''
thumb_index = 'error creating thumbnails'
hash = self.get_hash(filename)
screenshot = self.get_screenshot(url)
2022-02-21 13:19:09 +00:00
try: os.remove(filename)
except FileNotFoundError:
logger.info(f'tmp file not found thus not deleted {filename}')
2022-06-07 16:41:58 +00:00
cdn_url = self.storage.get_cdn_url(key)
2022-06-08 09:44:49 +00:00
timestamp = info.create.isoformat() if hasattr(info, "create") else None
2022-02-21 13:19:09 +00:00
return ArchiveResult(status=status, cdn_url=cdn_url, thumbnail=key_thumb,
2022-06-08 09:44:49 +00:00
thumbnail_index=thumb_index, duration=getattr(info, "duration", 0), title=getattr(info, "caption", ""),
timestamp=timestamp, hash=hash, screenshot=screenshot)
2022-02-21 13:19:09 +00:00
except tiktok_downloader.Except.InvalidUrl as e:
2022-02-21 13:19:09 +00:00
status = 'Invalid URL'
logger.warning(f'Invalid URL on {url} {e}\n{traceback.format_exc()}')
2022-02-21 13:19:09 +00:00
return ArchiveResult(status=status)
except:
error = traceback.format_exc()
status = 'Other Tiktok error: ' + str(error)
logger.warning(f'Other Tiktok error' + str(error))
2022-02-21 13:19:09 +00:00
return ArchiveResult(status=status)