Advertisement
Smokahontas

Tiktok Date Extractor

Mar 13th, 2024 (edited)
992
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.33 KB | None | 0 0
  1. from datetime import datetime, timezone, timedelta
  2.  
  3. def get_vid_id(tiktok_url):
  4.     # This regex should be safe as "Only letters, numbers, underscores, or periods are allowed" in TikTok usernames.
  5.     import re
  6.     regex = re.compile(r'(?<=\/video\/)(.*?)(?=$|[^0-9])')
  7.     vid_id = regex.search(tiktok_url).group(0)
  8.     return vid_id
  9.  
  10. def extract_unix_timestamp(vid_id):
  11.     # BigInt needed as we need to treat vid_id as 64 bit decimal. This reduces browser support.
  12.     as_binary = bin(int(vid_id))[2:]
  13.     first_31_chars = as_binary[:31]
  14.     timestamp = int(first_31_chars, 2)
  15.     return timestamp
  16.  
  17. def unix_timestamp_to_brasilia_date(timestamp):
  18.     date_object = datetime.utcfromtimestamp(timestamp)
  19.     brasilia_timezone = timezone(timedelta(hours=-3))  # UTC-3 for Brasília
  20.     brasilia_date_object = date_object.replace(tzinfo=timezone.utc).astimezone(brasilia_timezone)
  21.     brasilia_date_format = brasilia_date_object.strftime('%a, %d %b %Y %H:%M:%S Brasília')
  22.     return brasilia_date_format
  23.  
  24. def get_date():
  25.     tiktok_url = input("Enter TikTok video URL: ")
  26.     vid_id = get_vid_id(tiktok_url)
  27.     unix_timestamp = extract_unix_timestamp(vid_id)
  28.     brasilia_date_format = unix_timestamp_to_brasilia_date(unix_timestamp)
  29.     print("Uploaded on:", brasilia_date_format)
  30.  
  31. if __name__ == "__main__":
  32.     get_date()
  33.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement