-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
315 lines (277 loc) · 11.4 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
from bs4 import BeautifulSoup
import requests
from telethon import events, Button
from config import bot, channel_id , api , reddit_personal_use_script, reddit_secret, agent, subreddit, channel_id, flair, main_channel_id
import asyncio
import yt_dlp
import shutil
import subprocess
import os
from telethon.tl.types import DocumentAttributeVideo
import asyncpraw
reddit = asyncpraw.Reddit(client_id = reddit_personal_use_script, client_secret = reddit_secret, user_agent = agent)
try:
channel_id = int(channel_id)
except:
channel_id = channel_id
def delete_files():
folder = 'videos'
for filename in os.listdir(folder):
file_path = os.path.join(folder, filename)
try:
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
print('Failed to delete %s. Reason: %s' % (file_path, e))
ydl_opts = {
'writethumbnail': True,
'noplaylist' : True,
'format': '136+140''398+140' '22' 'bestvideo[ext!=webm]+bestaudio[ext!=webm]/best[ext!=webm]',
'outtmpl': 'videos/youtube.mp4'
}
def dwl_vid(url):
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
ydl.download([url])
def check_thumb():
if os.path.isfile("videos/youtube.webp"):
os.system("ffmpeg -i videos/youtube.webp videos/thumb.png")
# return "videos/thumb.png"
elif os.path.isfile("videos/youtube.png"):
os.system("ffmpeg -i videos/youtube.png videos/thumb.png")
elif os.path.isfile("videos/youtube.jpg"):
os.system("ffmpeg -i videos/youtube.jpg videos/thumb.png")
# return "videos/thumb.png"
else:
return None
def get_duration():
result = subprocess.run(["ffprobe", "-v", "error", "-show_entries",
"format=duration", "-of",
"default=noprint_wrappers=1:nokey=1", "videos/youtube.mp4"],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
return float(result.stdout)
async def send_message(file,title):
channel = await bot.get_entity(f"t.me/{channel_id}")
try:
await bot.send_message(
channel,
f"{title}\n\n@{channel_id}",
file=file,
)
except Exception as e:
print(e)
# channel = bot.get_entity(f"t.me/{channel_id}")
def write_file(url,source):
with open(f"{source}_link_log.txt","w") as f:
f.write(url)
def read_file(source):
with open(f"{source}_link_log.txt", "r") as f:
file = f.read()
return file
async def reddit_fetch(last):
channel = await bot.get_entity(channel_id)
# channel = channel_id
subred = await reddit.subreddit(subreddit)
if flair == None:
new = subred.new(limit = 1)
else:
new = subred.search(f'flair:"{flair}"', sort = "new",syntax='lucene', limit=1)
rev_list = []
async for i in new:
rev_list.append([i.url,i.title])
rev_list.reverse()
for i in rev_list:
url = i[0]
title = i[1]
split = url.split("/")
print(url)
print("from reddit")
if 'i.redd.it' in split:
if url != last:
try:
await bot.send_message(
channel,
f"{title}\n\n@{main_channel_id}",
file=url,
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
write_file(url,"reddit")
except Exception as e:
print(e)
else:
if "playlist" in url:
print("playlist found, cancelling download")
else:
word_dict = ["trailer", "Trailer"," pv"," PV", " pv", " PV", "teaser", "Teaser","visual", "Visual","preview", "Preview", "Announced","announced","project", "Project"," op", " OP", " ed", " ed ", " op ", " ED ", " OP "," ED","op","OP","ED","ed", "announcement", "Announcement", "anime", "Anime","official", "Official"]
if url != last:
try:
delete_files()
dwl_vid(url)
location = "videos/youtube.mp4"
file = location
check_thumb()
thumbnail = "videos/thumb.png"
dur = get_duration()
print(dur)
dur = int(dur)
print(dur)
new_title = f"[{title}]({url})"
for word in word_dict:
if word in title:
new_title = title.replace(word, f"[{word}]({url})")
break
await bot.send_message(
channel,
f"{new_title}\n\n@{main_channel_id}",
file=file,
thumb=thumbnail,
supports_streaming=True,
attributes=[DocumentAttributeVideo(
duration=dur,
w=1260,
h=720,
supports_streaming=True,
)],
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
print("uploaded")
write_file(url,"reddit")
except Exception as e:
try:
new_title = f"[{title}]({url})"
for word in word_dict:
if word in title:
new_title = title.replace(word, f"[{word}]({url})")
break
await bot.send_message(
channel,
f"{new_title}\n\n@{main_channel_id}",
file=url,
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
write_file(url,"reddit")
except Exception as e:
print(e)
async def livechart_fetch(last):
channel = await bot.get_entity(channel_id)
# channel = channel_id
print("from livechart")
url = "https://www.livechart.me/feeds/headlines"
response = requests.get(url)
response_html = response.content
soup = BeautifulSoup(response_html,'xml')
title_list = soup.find('item').title.contents
title = title_list[0]
link_list = soup.find('item').link.contents
link = link_list[0]
thumb = soup.find('media:thumbnail')["url"]
print(thumb)
word_dict = ["trailer", "Trailer","TRAILER", "pv", "PV", "teaser", "Teaser","visual", "Visual","preview", "Preview", "Announced","announced","project", "Project","op", "OP", "ed", "ED", "Announcement","TRAILER","anime","begins"]
if link != last:
try:
new_title = f"[{title}]({link})"
for word in word_dict:
if word in title:
new_title = title.replace(word, f"[{word}]({link})")
break
delete_files()
dwl_vid(link)
location = "videos/youtube.mp4"
file = location
check_thumb()
thumbnail = "videos/thumb.png"
dur = get_duration()
print(dur)
dur = int(dur)
print(dur)
await bot.send_message(
channel,
f"{new_title}\n\n@{main_channel_id}",
file=file,
thumb=thumbnail,
supports_streaming=True,
attributes=[DocumentAttributeVideo(
duration=dur,
w=1260,
h=720,
supports_streaming=True
)],
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
print("uploaded")
write_file(link,"livechart")
except Exception as e:
print(e)
try:
print("video not found looking for twiiter image")
split = link.split('/')
id = split[-1]
status = api.get_status(id,tweet_mode='extended')
media = status.entities['media']
for image in media:
media_url = image['media_url']
new_title = f"[{title}]({link})"
for word in word_dict:
if word in title:
new_title = title.replace(word, f"[{word}]({link})")
break
await bot.send_message(
channel,
f"{new_title}\n\n@{main_channel_id}",
file=media_url,
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
write_file(link,"livechart")
except Exception as e:
print(e)
try:
print("nothing found downloading thumbnail")
split = thumb.split('?')
img = split[0]
new_title = f"[{title}]({link})"
for word in word_dict:
if word in title:
new_title = title.replace(word, f"[{word}]({link})")
break
await bot.send_message(
channel,
f"{new_title}\n\n@{main_channel_id}",
file=img,
buttons=[Button.inline('approve', b'approve'),Button.inline('reject', b'reject')]
)
write_file(link,"livechart")
except Exception as e:
print(e)
@bot.on(events.CallbackQuery)
async def click_handler(event):
# channel = await bot.get_entity(f"t.me/{channel_id}")
channel = channel_id
main_channel = await bot.get_entity(f"t.me/{main_channel_id}")
message = event.message_id
userid = event.query.user_id
print(userid)
user_info = await bot.get_entity(userid)
messages = await bot.get_messages(channel,ids=message)
msg_txt = messages.message
if event.data == b'approve':
await bot.send_message(main_channel,messages,buttons = Button.clear())
await bot.edit_message(channel,message,f"{msg_txt}\n\nthis message was posted by @{user_info.username}")
elif event.data == b'reject':
await bot.edit_message(channel,message,f"{msg_txt}\n\nthis message was rejected by @{user_info.username}")
loop = asyncio.get_event_loop()
async def fetch_news():
# channel = await bot.get_entity(f"t.me/{channel_id}")
while True:
livechart_last = read_file("livechart")
reddit_last = read_file("reddit")
await reddit_fetch(reddit_last)
await livechart_fetch(livechart_last)
print((reddit_last,livechart_last))
print('going to sleep')
await asyncio.sleep(60)
print("nothing")
loop.run_until_complete(fetch_news())
bot.start()
bot.run_until_disconnected()