tiktok_archiver.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import os, traceback
  2. import tiktok_downloader
  3. from loguru import logger
  4. from .base_archiver import Archiver, ArchiveResult
  5. from storages import Storage
  6. class TiktokArchiver(Archiver):
  7. name = "tiktok"
  8. def download(self, url, check_if_exists=False):
  9. if 'tiktok.com' not in url:
  10. return False
  11. status = 'success'
  12. try:
  13. info = tiktok_downloader.info_post(url)
  14. key = self.get_key(f'{info.id}.mp4')
  15. filename = os.path.join(Storage.TMP_FOLDER, key)
  16. logger.info(f'found video {key=}')
  17. if check_if_exists and self.storage.exists(key):
  18. status = 'already archived'
  19. media = tiktok_downloader.snaptik(url).get_media()
  20. if len(media) <= 0:
  21. if status == 'already archived':
  22. return ArchiveResult(status='Could not download media, but already archived', cdn_url=self.storage.get_cdn_url(key))
  23. else:
  24. return ArchiveResult(status='Could not download media')
  25. logger.info(f'downloading video {key=}')
  26. media[0].download(filename)
  27. if status != 'already archived':
  28. logger.info(f'uploading video {key=}')
  29. self.storage.upload(filename, key)
  30. try:
  31. key_thumb, thumb_index = self.get_thumbnails(filename, key, duration=info.duration)
  32. except Exception as e:
  33. logger.error(e)
  34. key_thumb = ''
  35. thumb_index = 'error creating thumbnails'
  36. hash = self.get_hash(filename)
  37. screenshot = self.get_screenshot(url)
  38. try: os.remove(filename)
  39. except FileNotFoundError:
  40. logger.info(f'tmp file not found thus not deleted {filename}')
  41. cdn_url = self.storage.get_cdn_url(key)
  42. timestamp = info.create.isoformat() if hasattr(info, "create") else None
  43. return ArchiveResult(status=status, cdn_url=cdn_url, thumbnail=key_thumb,
  44. thumbnail_index=thumb_index, duration=getattr(info, "duration", 0), title=getattr(info, "caption", ""),
  45. timestamp=timestamp, hash=hash, screenshot=screenshot)
  46. except tiktok_downloader.Except.InvalidUrl as e:
  47. status = 'Invalid URL'
  48. logger.warning(f'Invalid URL on {url} {e}\n{traceback.format_exc()}')
  49. return ArchiveResult(status=status)
  50. except:
  51. error = traceback.format_exc()
  52. status = 'Other Tiktok error: ' + str(error)
  53. logger.warning(f'Other Tiktok error' + str(error))
  54. return ArchiveResult(status=status)