diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy-api.yml similarity index 95% rename from .github/workflows/deploy.yml rename to .github/workflows/deploy-api.yml index 9c3d509..ec00d1d 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy-api.yml @@ -1,4 +1,4 @@ -name: Build and Deploy to Cloud Run +name: Build and Deploy the API to Cloud Run on: push: diff --git a/.github/workflows/deploy-function.yml b/.github/workflows/deploy-function.yml new file mode 100644 index 0000000..4623e83 --- /dev/null +++ b/.github/workflows/deploy-function.yml @@ -0,0 +1,44 @@ +name: Deploy Google Cloud Function + +on: + push: + branches: + - develop + - main + +jobs: + deploy: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v2 + + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install -r function/requirements.txt + + - name: Authenticate to Google Cloud + uses: google-github-actions/auth@v1 + with: + credentials_json: ${{ secrets.GCP_SA_KEY }} + + - name: Deploy Google Cloud Function + run: | + gcloud functions deploy moneyprinterturbo \ + --runtime python310 \ + --trigger-http \ + --entry-point main \ + --source function \ + --region ${{ secrets.GCP_REGION }} \ + --set-env-vars API_URL=${{ secrets.API_URL }},VIDEO_SUBJECT=${{ vars.VIDEO_SUBJECT }},VIDEO_DESCRIPTION=${{ vars.VIDEO_DESCRIPTION }},ACCOUNT_NAME=${{ vars.ACCOUNT_NAME }}, TIME_BEFORE_DOWNLOAD=${{ vars.TIME_BEFORE_DOWNLOAD }} + + - name: Upload music.mp3 to Google Cloud Storage + run: | + gsutil cp function/music.mp3 gs://${{ secrets.GCP_BUCKET_NAME }}/music.mp3 \ No newline at end of file diff --git a/.github/workflows/import_secrets_and_variables.yml b/.github/workflows/import_secrets_and_variables.yml new file mode 100644 index 0000000..cd17f1d --- /dev/null +++ b/.github/workflows/import_secrets_and_variables.yml @@ -0,0 +1,30 @@ +on: + push: + branches: + - develop + - main + +jobs: + recreate-json: + runs-on: ubuntu-latest + steps: + - name: Checkout du repo + uses: actions/checkout@v3 + + - name: Recreate JSON file + env: + SECRET_JSON: ${{ secrets.TK_COOKIES_JSON }} + JSON_FILENAME: ${{ vars.JSON_FILENAME }} + run: | + echo "TK_COOKIES_JSON" > function/$JSON_FILENAME + recreate-function: + runs-on: ubuntu-latest + steps: + - name: Checkout du repo + uses: actions/checkout@v3 + + - name: Recreate function with variables + env: + API_URL: ${{ vars.API_URL }} + run: | + echo "API_URL = '$API_URL'" > function/function.py diff --git a/.gitignore b/.gitignore index cf3cd39..cc1ac65 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,5 @@ node_modules /sites/docs/.vuepress/dist # 模型目录 /models/ -./models/* \ No newline at end of file +./models/* +/function/.venv/ diff --git a/function/function.py b/function/function.py new file mode 100644 index 0000000..60c43d6 --- /dev/null +++ b/function/function.py @@ -0,0 +1,92 @@ +import os +import requests +import json +import time +from tiktokautouploader import upload_tiktok + +api_base_url = os.getenv("API_URL") +headers = {"Content-Type": "application/json"} + +video_subject = os.getenv("VIDEO_SUBJECT") +video_language = "en-US" +subtitle_font = "STHeitiMedium.ttc" +subtitle_position = "bottom" +subtitle_font_color = "#FFFFFF" +subtitle_outline_color = "#000000" +subtitle_font_size = 60 +subtitle_outline_width = 1.5 +video_source = "pexels" +video_concat_mode = "random" +video_transition_mode = "None" +video_aspect_ratio = "9:16" +video_clip_duration = 3 +video_count = 1 +speech_voice = "en-US-GuyNeural-Male" +speech_volume = 1.0 +speech_rate = 1.0 +song_file_path = "./music.mp3" +bgm_file_path = "../resource/songs/music.mp3" +bgm_volume = 0.2 + +upload_music_url = f"{api_base_url}/musics" +files = {'file': open(song_file_path, 'rb')} +response = requests.post(upload_music_url, files=files) +bgm_file = response.json()['data']['file'] + +generate_script_url = f"{api_base_url}/scripts" +script_payload = { + "video_subject": video_subject, + "video_language": video_language, + "paragraph_number": 1 +} +response = requests.post(generate_script_url, headers=headers, data=json.dumps(script_payload)) +video_script = response.json()['data']['video_script'] + +generate_keywords_url = f"{api_base_url}/terms" +keywords_payload = { + "video_subject": video_subject, + "video_script": video_script, + "amount": 5 +} +response = requests.post(generate_keywords_url, headers=headers, data=json.dumps(keywords_payload)) +video_terms = response.json()['data']['video_terms'] + +generate_video_url = f"{api_base_url}/videos" +video_payload = { + "video_subject": video_subject, + "video_script": video_script, + "video_terms": video_terms, + "video_aspect": video_aspect_ratio, + "video_concat_mode": video_concat_mode, + "video_transition_mode": video_transition_mode, + "video_clip_duration": video_clip_duration, + "video_count": video_count, + "video_source": video_source, + "video_language": video_language, + "voice_name": speech_voice, + "voice_volume": speech_volume, + "voice_rate": speech_rate, + "bgm_type": "custom", + "bgm_file": bgm_file, + "bgm_volume": bgm_volume, + "subtitle_enabled": False, +} + +response = requests.post(generate_video_url, headers=headers, data=json.dumps(video_payload)) +print(response.json()) + +task_id = response.json()['data']['task_id'] + +time.sleep(float(os.getenv("TIME_BEFORE_DOWNLOAD"))) + +download_url = f"{api_base_url}/download/{task_id}/final-1.mp4" +response = requests.get(download_url, headers=headers) +with open('output_video.mp4', 'wb') as f: + f.write(response.content) + +video_path = "output_video.mp4" +description = os.getenv("VIDEO_DESCRIPTION") +account_name = os.getenv("ACCOUNT_NAME") +hashtags = ['fyp'] + +upload_tiktok(video=video_path, description=description, accountname=account_name) \ No newline at end of file diff --git a/function/music.mp3 b/function/music.mp3 new file mode 100644 index 0000000..b3a2b00 Binary files /dev/null and b/function/music.mp3 differ diff --git a/function/requirements.txt b/function/requirements.txt new file mode 100644 index 0000000..9fb8d6d Binary files /dev/null and b/function/requirements.txt differ