-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
394 lines (310 loc) · 10.9 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
from typing import List
from bs4 import BeautifulSoup
from collections import defaultdict
import argparse
import logging
import os
import requests
from Account import Account
from Scheduler import Scheduler
from clipper import make_clips
from constants import *
from util import create_directory, download_youtube_video
# Configure the logger
logging.basicConfig(
level=logging.INFO, format="[%(levelname)s][%(asctime)s]: %(message)s"
)
logger = logging.getLogger(__name__)
def main():
# Parse command line arguments
parser = argparse.ArgumentParser(description="Tool for processing YouTube videos.")
parser.add_argument(
"emails",
metavar="emails",
type=str,
nargs="*",
help="List of emails for the accounts to schedule videos for.",
)
parser.add_argument(
"-c", "--create_account", action="store_true", help="Form to add a new account."
)
parser.add_argument(
"-e",
"--edit_account",
nargs=1,
type=str,
help="Edit a specific account by it's email.",
)
parser.add_argument(
"-a",
"--all_accounts",
action="store_true",
help="Schedules videos for all accounts.",
)
parser.add_argument(
"--add_content",
nargs="*",
type=str,
help="URLs to secondary content on YouTube (ex: GTA Ramps, Minecraft Parkour, etc...)",
)
args = parser.parse_args()
if args.create_account:
add_account_form()
elif args.edit_account:
account = args.edit_account[0]
edit_account_form(account)
elif args.add_content:
for url in args.add_content:
logger.info(f"Saving {url}...")
save_secondary_content(url)
else:
if args.all_accounts:
emails = get_all_emails()
else:
emails = args.emails
for email in emails:
process_account_videos(email)
def save_secondary_content(url: str):
# Create the secondary content directory
create_directory(SECONDARY_CONTENT_PATH)
# Download video
download_youtube_video(url, SECONDARY_CONTENT_PATH)
def add_account_form() -> Account:
print(
"Welcome to TikTok Account Setup.\n"
"You will not be asked for the password until logging in to TikTok, "
"after which the cookie will be saved, and you will not be asked for the password until it expires.\n"
)
email = input("Enter the email for the account: ")
channels = get_input_list(
message="Enter the channels you want to source videos from (type 'done' when finished): ",
prompt="Channel username: ",
)
schedule = get_input_list(
message="Enter the times you want to post videos (HH:mm) (type 'done' when finished): ",
prompt="Time of day (HH:mm): ",
)
subtitles = get_yes_no_input("Do you want to have subtitles in the videos (y/N)? ")
secondary_content = get_yes_no_input(
"Do you want to have secondary content in the videos (ex: GTA Ramps) (y/N)? "
)
video_length = int(
input("What is the max source video duration in seconds (default: 600)? ")
or "600"
)
clip_length = int(
input("What length do you want your clips to be in seconds (default: 60)? ")
or "60"
)
account = Account.create(
email,
channels,
schedule,
secondary_content,
subtitles,
clip_length,
video_length,
)
return account
def edit_account_form(account_email: str):
"""
A form for the user to edit an account, it displays previous account values. If the user wants to keep the previous setting the user should just press Enter.
"""
account = Account(account_email)
print(
f"Welcome to the editing form for {account}!",
"If you want to leave the values the same as before, simply leave the inputs empty.",
"If you entered something wrong, you will be asked to confirm your changes at the end.",
)
schedule = (
get_input_list(
message=f"Add your schedule again, type 'done' when finished ({account.schedule}).",
prompt="Time of day (HH:mm): ",
)
or account.schedule
)
account.schedule = schedule
channels = (
get_input_list(
message=f"Enter the channels you want to source videos from again. Type 'done' when finished ({account.channels}).",
prompt="Channel username: ",
)
or account.channels
)
account.channels = channels
subtitles = get_yes_no_input("Do you want to have subtitles in the videos (y/N)? ")
account.subtitles = subtitles
secondary_content = get_yes_no_input(
"Do you want to have secondary content in the videos (ex: GTA Ramps) (y/N)? "
)
account.secondary_content = secondary_content
video_length = int(
input(
f"What is the max source video duration in seconds ({account.video_length})? "
)
or account.video_length
)
account.video_length = video_length
clip_length = int(
input(
f"What length do you want your clips to be in seconds ({account.clip_length})? "
)
or account.clip_length
)
account.clip_length = clip_length
confirm = get_yes_no_input("Confirm changes (y/N)? ")
# Save changes
if confirm:
account.save()
def get_input_list(message: str, prompt: str) -> List[str]:
print(message)
inputs = []
while True:
user_input = input(prompt)
if user_input.lower() == "done":
break
# Check if the input has data in it
if user_input:
inputs.append(user_input)
print() # newline
return inputs
def get_yes_no_input(message: str) -> bool:
response = input(message)
return response.lower() == "y"
def process_account_videos(email: str):
"""
The main function that handles, making and scheduling videos for an account.
Args:
email (str): email of an existing account in the "accounts" folder.
"""
logger.info(f"Initializing {email}...")
account = Account(email)
# Get valid clip dates
valid_dates = get_valid_dates(account)
# Check if there are no valid dates
if not valid_dates:
logging.info("The schedule is full!")
return
logger.info(f"Calculated {len(valid_dates)} dates")
# Get unused clips
unused_clips = account.get_processed_videos()
# Calculate clips data
clips_data = calculate_clips_data(account, valid_dates, unused_clips)
# Schedule videos if any
schedule_videos(account, clips_data)
def get_valid_dates(account: Account) -> List[str]:
"""
TikTok allows users to schedule videos up to 10 days in advance
Returns:
List[str]: list of available dates to post a video
"""
valid_dates = []
date = None
while True:
date = account.get_next_date(date)
if date:
valid_dates.append(date)
else:
break
return valid_dates
def calculate_clips_data(
account: Account, valid_dates: List[str], unused_clips: List[str]
) -> List[dict]:
"""
Pairs existing or newly created clips with valid dates.
Returns:
List[dict]: [{"path": "path/to/clip.mp4", "date": valid_date}, ...]
"""
# Initialize the list to store the clip data
clips_data = []
# Find last scheduled clip if any
last_scheduled_clip = account.last_scheduled_video()
last_scheduled_id = last_scheduled_clip["id"] if last_scheduled_clip else None
# Group unused clips by their ID
id_clips_map = defaultdict(list)
for clip in unused_clips:
_, _, clip_id = os.path.basename(clip).removesuffix(".mp4").split(",")
id_clips_map[clip_id].append(clip)
# Order clips by ID and group IDs
ordered_clips = []
if unused_clips:
ordered_clips.extend(id_clips_map.pop(last_scheduled_id, []))
for clips in id_clips_map.values():
ordered_clips.extend(clips)
# Pair unused clips with dates if available
for clip in ordered_clips:
if valid_dates:
clip_data = {"path": clip, "date": valid_dates.pop(0)}
clips_data.append(clip_data)
else:
break
# Generate new clips if needed
while valid_dates:
# Create clips from a video
id = account.get_videos(1)[0]
url = f"https://www.youtube.com/watch?v={id}"
logger.info(f"Creating clips from {url}...")
clips = make_clips(
url,
account.email,
account.secondary_content,
account.subtitles,
)
# Pair up clips with as many valid dates left
for clip in clips:
if valid_dates:
clip_data = {"path": clip, "date": valid_dates.pop(0)}
clips_data.append(clip_data)
else:
break
return clips_data
def schedule_videos(account: Account, clips_data: List[dict]):
"""
Handles the scheduling of videos, adding captions and saving the data to the account.
"""
scheduler = Scheduler(account)
logger.info("Logging in to the TikTok...")
for clip in clips_data:
video_path = clip["path"]
date = clip["date"]
logger.info(f"Scheduling the video for {date}...")
# Create the caption for the video
video_name = os.path.basename(video_path).removesuffix(".mp4")
id = video_name.split(",")[-1]
part_number = int(video_name.split(",")[1]) + 1
caption = generate_caption(id, f"Part {part_number}")
# Schedule the post and add it to the history
scheduler.post(video_path, caption, date)
account.add_video_to_history(id, date)
# Remove video once posted
logger.info("Removing the video")
os.remove(video_path)
def generate_caption(id: str, part_text: str | None) -> str:
"""
Returns the title of a YouTube video with some hashtags
Args:
id (str): ID of the YouTube video
part_text (str): indicates what part the video is (optional)
Returns:
str: the caption
"""
r = requests.get(f"https://www.youtube.com/watch?v={id}")
soup = BeautifulSoup(r.text, features="html.parser")
link = soup.find_all(name="title")[0]
title = str(link)
title = title.replace("<title>", "")
title = title.replace("</title>", "")
title = title.removesuffix(" - YouTube")
main_text = title
if part_text:
main_text += f" | {part_text}"
caption = f"{main_text} #reels #viral #tiktok #explore #foryou #explorepage #trending #reelsinstagram #foryoupage #love #fashion #instagram #instagood #music #likeforlikes"
return caption
def get_all_emails() -> List[str]:
# Get all files in the accounts path
account_files = os.listdir(ACCOUNTS_PATH)
# Remove the extension name to get the email
emails = [file.removesuffix(".pkl") for file in account_files]
return emails
if __name__ == "__main__":
main()