-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbackup.py
103 lines (87 loc) · 3.08 KB
/
backup.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
# Copyright (c) 2022 Amit Sharma < https://github.com/buddhhu >
#
# License can be found in < https://github.com/budhhu/YT-Channel-Downloader/blob/main/License > .
# - - - - - - - - - - Imports - - - - - - - - - -
from glob import glob
from os import system
from os.path import getsize
from re import search
from decouple import config
from pyrogram import Client
from youtubesearchpython import Playlist
from youtubesearchpython.core.utils import playlist_from_channel_id
from yt_dlp import YoutubeDL
# - - - - - - - - - - Variables - - - - - - - - - -
API_ID = config("API_ID", default=6, cast=int)
API_HASH = config("API_HASH", default="eb06d4abfb49dc3eeb1aeb98ae0f581e", cast=str)
BOT_TOKEN = config("BOT_TOKEN", default="", cast=str)
TG_CHANNEL_ID = config("TG_CHANNEL_ID", default=0, cast=int)
YT_CHANNEL_LINK = config("YT_CHANNEL_LINK", default="", cast=str)
# - - - - - - - - - - Constants - - - - - - - - - -
channel = Playlist(playlist_from_channel_id(YT_CHANNEL_LINK.split("/")[-1]))
youtube_link = "https://www.youtube.com/watch?v={}"
thumbnail_link = "https://i.ytimg.com/vi/{}/maxresdefault.jpg"
client = Client(
name="bot",
api_id=API_ID,
api_hash=API_HASH,
bot_token=BOT_TOKEN,
in_memory=True,
parse_mode="md",
)
opts = {
"quiet": True,
"noprogress": True,
"format": "bestvideo+bestaudio",
"prefer_ffmpeg": True,
"geo-bypass": True,
"nocheckcertificate": True,
"outtmpl": "%(id)s.%(ext)s",
"postprocessors": [
{"key": "FFmpegMetadata"},
{"key": "FFmpegVideoConvertor", "preferedformat": "mp4"},
],
}
total_video = "0"
# - - - - - - - - Do not edit below this line - - - - - - - -
try:
total_video = open("videos.txt", "r").read()
except FileNotFoundError:
open("videos.txt", "w").write("0")
def write_tvideos(to_add):
open("videos.txt", "w").write(str(to_add))
print(f"Total {len(channel.videos)} videos")
async def main():
await client.start()
total_videos = int(total_video)
for vids in range(total_videos+1, len(channel.videos)+1):
video_id = search(r"\?v=([(\w+)\-]*)", channel.videos[-abs(vids)]["link"]).group(1)
print(f"Downloading {vids}")
info = YoutubeDL(opts).extract_info(youtube_link.format(video_id))
system(f"wget -qO {info['id']}.jpg " + thumbnail_link.format(info["id"]))
files = glob(f"{info['id']}*")
thumb, video = "", ""
for file in files:
if file.endswith(".jpg"):
thumb = file
else:
video = file
if getsize(thumb) == 0:
thumb = None
await client.send_video(
TG_CHANNEL_ID,
video,
caption=f"`{info['title']}`",
thumb=thumb,
duration=info["duration"],
height=info["height"],
width=info["width"],
file_name=info["title"],
)
total_videos = vids
system(f"rm {info['id']}*")
print(f"Completed {total_videos}")
write_tvideos(total_videos)
print(f"Completed {total_videos}")
await client.stop()
client.run(main())