-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduled_post.py
168 lines (157 loc) · 6.44 KB
/
scheduled_post.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
import argparse
import logging
import os
import random
from clip_creator.adb.not_bort import ADBScroll
from clip_creator.adb.tiktok_auto import upload_phsyphone
from clip_creator.conf import (
CLIPS_FOLDER,
LOGGER,
POSSIBLE_TRANSLATE_LANGS,
)
from clip_creator.db.db import (
get_rows_where_tiktok_not_null_or_empty,
update_reddit_post_clip_sc,
update_reddit_post_clip_sc_com,
get_rows_where_tiktok_not_null_or_empty_com
)
from clip_creator.lang.translate import translate_en_to
from clip_creator.utils.scan_text import (
reddit_remove_bad_words,
)
def sched_run(skipscroll=False):
"""
This function is used to run the scheduled task.
It will run the scheduled task every few hours-4x a day.
"""
parser = argparse.ArgumentParser(description="AI Clip Creator")
parser.add_argument("--scrolltime", type=int, help="Number of minutes to scroll")
parser.add_argument("--skipscroll", action="store_true", help="Skip scrolling")
parser.add_argument("--scrollonly", action="store_true", help="scroll")
args = parser.parse_args()
#####################################
# Scroll more
#####################################
if not args.skipscroll and not skipscroll:
############## Scroll ##############
try:
adb = ADBScroll()
if args.scrolltime:
max_time_min = args.scrolltime
else:
max_time_min = random.randint(5, 10)
adb.scroll_tiktok(max_time_min=max_time_min)
LOGGER.info("Stopping...")
adb.running = False
adb.kill_apps()
LOGGER.info("Stopped.")
except Exception as e:
LOGGER.error("Error scrolling: %s", e)
if args.scrollonly:
return
#####################################
# Setup logging
#####################################
file_handler = logging.FileHandler("logs/schedule_runner.log")
file_handler.setFormatter(
logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
)
LOGGER.addHandler(file_handler)
LOGGER.info("Running scheduled task...")
all_posts_to_post = [post for post in get_rows_where_tiktok_not_null_or_empty() if post.get("tiktok_posted") and post.get("tiktok_uploaded") != "None" and not post.get("tiktok_uploaded")]
more_posts = [post for post in get_rows_where_tiktok_not_null_or_empty_com() if post.get("tiktok_posted") and post.get("tiktok_uploaded") != "None" and not post.get("tiktok_uploaded")]
LOGGER.info("more_posts: %s", len(more_posts))
all_all_reddit = []
for post in more_posts:
post["type"] = "coms"
all_all_reddit.append(post)
for post in all_posts_to_post:
post["type"] = "posts"
all_all_reddit.append(post)
posts_to_use = {}
random.shuffle(all_all_reddit)
for post in all_all_reddit:
if (
post.get("tiktok_posted")
and post.get("tiktok_posted") != "None"
and not post.get("tiktok_uploaded")
):
# No need to check date, just that it hasn't been posted
############## Post ##############
LOGGER.info("Post to use %s", post["post_id"])
posts_to_use[post["post_id"]] = post
posts_to_use[post["post_id"]][
"vfile"
] = f"{CLIPS_FOLDER}/reddit_{post['post_id']}.mp4"
if post.get("parts", 1) > 1:
posts_to_use[post["post_id"]]["desc"] = []
for i in range(post["parts"]):
posts_to_use[post["post_id"]]["desc"].append(
f"Part {i+1} |"
f" {post['title']}\n\n#part{i+1} #reddit"
" #reddittreadings #reddit_tiktok \n #redditstorytime"
" #askreddit #fyp"
)
else:
posts_to_use[post["post_id"]]["desc"] = (
f"{post['title']}\n\n#reddit"
" #reddittreadings #reddit_tiktok \n #redditstorytime #askreddit"
" #fyp"
)
for lang in POSSIBLE_TRANSLATE_LANGS:
posts_to_use[post["post_id"]][
f"vfile_{lang}"
] = f"{CLIPS_FOLDER}/reddit{lang}_{post['post_id']}.mp4"
if post.get("parts", 1) > 1:
posts_to_use[post["post_id"]][f"desc_{lang}"] = []
for i in range(post["parts"]):
posts_to_use[post["post_id"]][f"desc_{lang}"].append(
translate_en_to(
posts_to_use[post["post_id"]]["desc"][i], lang
)
)
else:
posts_to_use[post["post_id"]][f"desc_{lang}"] = translate_en_to(
posts_to_use[post["post_id"]]["desc"], lang
)
for pid, post in posts_to_use.items():
LOGGER.info(f"Running scheduled task for {pid}...")
suvvedd = upload_phsyphone(
os.path.abspath(posts_to_use[post["post_id"]]["vfile"]),
posts_to_use[post["post_id"]]["desc"],
)
if not suvvedd:
LOGGER.error("Error uploading video")
continue
if post["type"] == "coms":
update_reddit_post_clip_sc_com(post["post_id"], True)
else:
update_reddit_post_clip_sc(post["post_id"], True)
for lang in POSSIBLE_TRANSLATE_LANGS:
LOGGER.info(f"Running scheduled task for {lang}...")
upload_phsyphone(
os.path.abspath(posts_to_use[post["post_id"]][f"vfile_{lang}"]),
posts_to_use[post["post_id"]][f"desc_{lang}"],
lang=lang,
)
LOGGER.info("Scheduled task completed.")
break
#####################################
# Scroll more
#####################################
if not args.skipscroll and not skipscroll:
try:
adb = ADBScroll()
if args.scrolltime:
max_time_min = args.scrolltime
else:
max_time_min = random.randint(10, 35)
adb.scroll_tiktok(max_time_min=max_time_min)
LOGGER.info("Stopping...")
adb.running = False
adb.kill_apps()
LOGGER.info("Stopped.")
except Exception as e:
LOGGER.error("Error scrolling: %s", e)
if __name__ == "__main__":
sched_run()