Lakpriya Seneviratna commited on
Commit
4e36dd3
·
1 Parent(s): 95062f0

chore: Update upload_video_to_tiktok function to optimize video upload process

Browse files
Files changed (1) hide show
  1. app.py +12 -12
app.py CHANGED
@@ -519,22 +519,27 @@ app.add_middleware(
519
 
520
  def upload_video_to_tiktok(access_token: str, video_file_path: str):
521
  try:
522
- # Step 1: Calculate video file size
523
  video_size = os.path.getsize(video_file_path)
524
-
525
- # Step 2: Initialize video upload
526
  init_response = requests.post(
527
- 'https://open.tiktokapis.com/v2/post/publish/inbox/video/init/',
528
  headers={
529
  'Authorization': f'Bearer {access_token}',
530
  'Content-Type': 'application/json; charset=UTF-8'
531
  },
532
  json={
 
 
 
 
 
 
 
 
533
  "source_info": {
534
  "source": "FILE_UPLOAD",
535
  "video_size": video_size,
536
- "chunk_size": video_size, # Adjust this if you want to upload in smaller chunks
537
- "total_chunk_count": 1 # This is 1 because we are uploading the file in one go
538
  }
539
  }
540
  )
@@ -544,14 +549,12 @@ def upload_video_to_tiktok(access_token: str, video_file_path: str):
544
 
545
  init_data = init_response.json()
546
 
547
- # Check if the initialization returned an error
548
  if 'error' in init_data and init_data['error']['code'] != 'ok':
549
  raise Exception(f"Initialization error: {init_data['error']['message']}")
550
 
551
  publish_id = init_data['data']['publish_id']
552
  upload_url = init_data['data']['upload_url']
553
 
554
- # Step 3: Upload the video file
555
  with open(video_file_path, 'rb') as video_file:
556
  video_data = video_file.read()
557
  upload_response = requests.put(
@@ -566,7 +569,6 @@ def upload_video_to_tiktok(access_token: str, video_file_path: str):
566
  if upload_response.status_code != 200:
567
  raise Exception(f"Failed to upload video: {upload_response.status_code} {upload_response.text}")
568
 
569
- # Optionally, Step 4: Check the status of the post
570
  status_response = requests.post(
571
  'https://open.tiktokapis.com/v2/post/publish/status/fetch/',
572
  headers={
@@ -660,8 +662,6 @@ async def tiktok_callback(request: Request):
660
  @app.get("/generate_video")
661
  def generate_video(request: Request):
662
  access_token = request.query_params.get('access_token')
663
- open_id = request.query_params.get('open_id')
664
-
665
  try:
666
  api_key = 'VhLwkCKi3iu5Pf37LXfz-Lp7hTW69EV8uw_hkLAPkiA' # Replace with your Unsplash API key
667
  background_image = fetch_random_nature_image(api_key)
@@ -692,7 +692,7 @@ def generate_video(request: Request):
692
 
693
  video_path = os.path.join(output_folder, final_filename + '.mp4')
694
 
695
- upload_video_to_tiktok(access_token, open_id, video_path)
696
 
697
  return {"status": "success", "filename": final_filename + '.mp4'}
698
  else:
 
519
 
520
  def upload_video_to_tiktok(access_token: str, video_file_path: str):
521
  try:
 
522
  video_size = os.path.getsize(video_file_path)
 
 
523
  init_response = requests.post(
524
+ 'https://open.tiktokapis.com/v2/post/publish/video/init/',
525
  headers={
526
  'Authorization': f'Bearer {access_token}',
527
  'Content-Type': 'application/json; charset=UTF-8'
528
  },
529
  json={
530
+ "post_info": {
531
+ "title": "this will be a funny #cat video on your @tiktok #fyp",
532
+ "privacy_level": "MUTUAL_FOLLOW_FRIENDS",
533
+ "disable_duet": False,
534
+ "disable_comment": True,
535
+ "disable_stitch": False,
536
+ "video_cover_timestamp_ms": 1000
537
+ },
538
  "source_info": {
539
  "source": "FILE_UPLOAD",
540
  "video_size": video_size,
541
+ "chunk_size": video_size,
542
+ "total_chunk_count": 1
543
  }
544
  }
545
  )
 
549
 
550
  init_data = init_response.json()
551
 
 
552
  if 'error' in init_data and init_data['error']['code'] != 'ok':
553
  raise Exception(f"Initialization error: {init_data['error']['message']}")
554
 
555
  publish_id = init_data['data']['publish_id']
556
  upload_url = init_data['data']['upload_url']
557
 
 
558
  with open(video_file_path, 'rb') as video_file:
559
  video_data = video_file.read()
560
  upload_response = requests.put(
 
569
  if upload_response.status_code != 200:
570
  raise Exception(f"Failed to upload video: {upload_response.status_code} {upload_response.text}")
571
 
 
572
  status_response = requests.post(
573
  'https://open.tiktokapis.com/v2/post/publish/status/fetch/',
574
  headers={
 
662
  @app.get("/generate_video")
663
  def generate_video(request: Request):
664
  access_token = request.query_params.get('access_token')
 
 
665
  try:
666
  api_key = 'VhLwkCKi3iu5Pf37LXfz-Lp7hTW69EV8uw_hkLAPkiA' # Replace with your Unsplash API key
667
  background_image = fetch_random_nature_image(api_key)
 
692
 
693
  video_path = os.path.join(output_folder, final_filename + '.mp4')
694
 
695
+ upload_video_to_tiktok(access_token, video_path)
696
 
697
  return {"status": "success", "filename": final_filename + '.mp4'}
698
  else: