mirror of
https://github.com/Paillat-dev/viralfactory.git
synced 2026-01-02 09:16:19 +00:00
Add UploadEngine and its dependencies
This commit is contained in:
75
src/engines/UploadEngine/TikTokUploadEngine.py
Normal file
75
src/engines/UploadEngine/TikTokUploadEngine.py
Normal file
@@ -0,0 +1,75 @@
|
||||
import gradio as gr
|
||||
|
||||
from tiktok_uploader.upload import upload_video
|
||||
|
||||
from .BaseUploadEngine import BaseUploadEngine
|
||||
|
||||
|
||||
class TikTokUploadEngine(BaseUploadEngine):
|
||||
name = "TikTokUpload"
|
||||
description = "Upload to TikTok"
|
||||
|
||||
num_options = 1
|
||||
|
||||
def __init__(self, options) -> None:
|
||||
self.hashtags = options[0]
|
||||
super().__init__()
|
||||
|
||||
def upload(self):
|
||||
cookies = self.get_setting(type="cookies")["cookies"]
|
||||
if cookies == None:
|
||||
gr.Warning(
|
||||
"Skipping upload to TikTok because no cookies were provided. Please provide cookies in the settings."
|
||||
)
|
||||
return
|
||||
title: str = self.ctx.title
|
||||
description: str = self.ctx.description
|
||||
hastags = self.hashtags.strip().split(" ")
|
||||
|
||||
# extract any hashtags from the description / title and remove them from the description
|
||||
for word in title.split():
|
||||
if word.startswith("#"):
|
||||
hastags.append(word)
|
||||
title = title.replace(word, "")
|
||||
for word in description.split():
|
||||
if word.startswith("#"):
|
||||
hastags.append(word)
|
||||
description = description.replace(word, "")
|
||||
|
||||
title = title.strip()
|
||||
description = description.strip()
|
||||
hastags_str = " ".join(hastags) + " " if hastags else ""
|
||||
failed = upload_video(
|
||||
filename=self.ctx.get_file_path("final.mp4"),
|
||||
description=f"{title} {description} {hastags_str}",
|
||||
cookies_str=cookies,
|
||||
browser="firefox",
|
||||
)
|
||||
for _ in failed:
|
||||
gr.Error(f"Failed to upload to TikTok")
|
||||
|
||||
@classmethod
|
||||
def get_options(cls):
|
||||
hashtags = gr.Textbox(label="hashtags", type="text", value="#fyp #foryou")
|
||||
return [hashtags]
|
||||
|
||||
@classmethod
|
||||
def get_settings(cls):
|
||||
current_settings = cls.get_setting(type="cookies") or {"cookies": ""}
|
||||
gr.Markdown(
|
||||
"Input your TikTok session cookies. You can get them as shown [here](https://github.com/wkaisertexas/tiktok-uploader?tab=readme-ov-file#authentication)."
|
||||
)
|
||||
cookies_input = gr.Textbox(
|
||||
lines=20,
|
||||
max_lines=50,
|
||||
label="cookies",
|
||||
type="text",
|
||||
value=current_settings["cookies"],
|
||||
)
|
||||
cookies_save_btn = gr.Button("Save")
|
||||
|
||||
def save(cookies: str):
|
||||
cls.store_setting(type="cookies", data={"cookies": cookies})
|
||||
gr.Info("Cookies saved successfully")
|
||||
|
||||
cookies_save_btn.click(save, inputs=[cookies_input])
|
||||
Reference in New Issue
Block a user