-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtermtube.py
More file actions
executable file
·238 lines (184 loc) · 8.48 KB
/
Copy pathtermtube.py
File metadata and controls
executable file
·238 lines (184 loc) · 8.48 KB
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
#!/bin/env python3
import os
import uyts
import youtube_dl
import requests
from bs4 import BeautifulSoup
import mpv
import sys
player = mpv.MPV(input_default_bindings=True, input_vo_keyboard=True, osc=True)
videos = []
def help():
print("""
Usage: termtube [OPTIONS] [QUERY]/[Channel List]
Options:
-h, --help prints this help message
n where n is the number of video results you want displayed
-l, --link copies the link of the youtube video to your clipboard
-f, --file specify the file with the list of channels on it and it fetches the 3 most recent videos of these channels.
-d, --download This option gives you a list of 10 videos based on your query and lets you pick one to download.
-p, --playlist This option gives you a list of playlists based on your query and lets you pick one to download.
-rss, --get-rss specify the file with the list of channels on it and it fetches the RSS links of these channels and stores them in another file. It will ask you to name this new file.
Query:
This is what the script looks for on YouTube. Please enter it in quotes, eg. 'Messi vs Ronaldo'
Channel List:
This is the file containing the names of the channels whose videos you want in your feed. Make sure you have only one channel per line. The program prints the latest 3 videos for each channel. Comments can be added in this file with '#'.
If this is used with the -rss or --get-rss option, it gets the RSS links of these channels and stores them in a new file in the same directory. It will ask you to name the new file containing the RSS links of these channels.
Examples:
termtube 3 'call me kevin' This prints out 5 videos with the query 'call me kevin' and asks you which one you want to play. If the number isn't specified, it prints out 5 videos. The max is 18.
termtube -f sublist This prints out the latest 3 videos from the channels in the file 'subfile' and asks you which one you want to play.
termtube -d 'call me kevin' This prints out videos with the query 'call me kevin' adn asks you which one you want to download.
termtube -p 'call me kevin' This prints out playlists for you to download.
termtube -rss sublist This gets rss feed links for all the channels in the file 'subfile'
""")
def playlistSearch(query, num):
search = uyts.Search(query)
for res in search.results:
if res.resultType == 'playlist':
videos.append({'title':res.title, 'url':'https://www.youtube.com/playlist?list='+res.id, 'creator': res.author, "length":res.length, 'type':res.resultType})
if len(videos) == num:
break
def videoSearch(query, num):
search = uyts.Search(query)
for res in search.results:
if res.resultType == 'video':
videos.append({'title':res.title, 'url':'https://www.youtube.com/watch?v='+res.id, 'creator': res.author, "length":res.duration, "type":res.resultType})
if len(videos) == num:
break
def downloadVideo(choice):
chosenVideo = videos[int(choice) - 1]
url = chosenVideo['url']
if chosenVideo['type'] == 'playlist':
ydl_opts = {
'format': 'best',
}
else:
ydl_opts = {
'format': 'best',
'outtmpl': chosenVideo['title'],
}
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
info_dict = ydl.extract_info(url, download=True)
def getRSS(channelFile):
RSSlinks = []
with open(channelFile, 'r') as file:
listOfChannels = file.readlines()
for channel in listOfChannels:
if channel[0] != '#':
search = uyts.Search(channel)
for res in search.results:
if res.resultType == 'channel':
channelID = res.id
channelName = res.title
RSSlinks.append({"creator":channelName, "link":"https://www.youtube.com/feeds/videos.xml?channel_id="+channelID})
break
return RSSlinks
def getVideosFromChannel(channel, num):
search = uyts.Search(channel)
for res in search.results:
if res.resultType == 'channel':
channelID = res.id
channelName = res.title
break
url = "https://www.youtube.com/feeds/videos.xml?channel_id="+channelID
html = requests.get(url)
soup = BeautifulSoup(html.text, "lxml")
entries = soup.find_all("entry")
number = int(num)
for i in range(number):
videos.append({"title":entries[i].title.text, "url":entries[i].link.attrs["href"], "creator": channelName})
def playVideo(choice):
chosenVideo = videos[int(choice) - 1]
print("Playing: " + chosenVideo['title'])
chosenVideoLink = chosenVideo['url']
player.play(chosenVideoLink)
player.wait_for_playback()
def getLink(choice):
chosenVideo = videos[int(choice) - 1]
chosenVideoLink = chosenVideo['url']
if sys.platform == 'linux':
cmd = "echo "+chosenVideoLink+" | xclip -sel clip"
os.system(cmd)
elif sys.platform == 'darwin':
cmd = "echo "+chosenVideoLink+" | pbcopy -sel clip"
os.system(cmd)
elif sys.platform == 'win32':
cmd = "echo "+chosenVideoLink+" | clip"
os.system(cmd)
print("The chosen video's link has been copied to the system clipboard")
def display(videoList):
for x, video in enumerate(videos):
print(f'{x+1}. ' + videos[x]['title'] + ' : ' + videos[x]['creator'] + " (" + videos[x]['length'] + ")")
def displayScraped(videoList):
for x, video in enumerate(videos):
print(f'{x+1}. ' + videos[x]['title'] + ' : ' + videos[x]['creator'])
if len(sys.argv) == 1:
help()
elif sys.argv[1] == '-h' or sys.argv[1] == '--help':
help()
elif sys.argv[1] == '-f' or sys.argv[1] == '--file':
subfile = sys.argv[2]
print("Looking for videos from channels in '" + subfile + "' file..")
with open(subfile, 'r') as file:
listOfChannels = file.readlines()
for channel in listOfChannels:
if channel[0] != '#':
getVideosFromChannel(channel, 3)
displayScraped(videos)
ch = input('Enter Choice: ')
try:
getLink(ch)
except:
print("Couldn't copy the link to clipboard. Make sure you have the right command line tools installed. You can check what tools this script uses for your OS in the help menu.")
playVideo(ch)
elif sys.argv[1] == '-rss' or sys.argv[1] == '--get-rss':
subfile = sys.argv[2]
print("Getting RSS feed links for channels in '" + subfile + "' file..")
RSSlinks = getRSS(subfile)
RSSfile = input("Enter the name of the file you want to store the links in: ")
with open(RSSfile, 'a') as file:
for channel in RSSlinks:
file.writelines(channel['creator'] + " : " + channel['link'] + "\n")
elif sys.argv[1] == '-d' or sys.argv[1] == '--download':
searchQuery = sys.argv[2]
number = input("Enter the number of videos you want to get back (max: 18): ")
videoSearch(searchQuery, number)
display(videos)
ch = input('Enter Choice: ')
downloadVideo(ch)
elif sys.argv[1] == '-p' or sys.argv[1] == '--playlist':
searchQuery = sys.argv[2]
number = input("Enter the number of playlists you want to get back (max: 15): ")
playlistSearch(searchQuery, number)
display(videos)
ch = input('Enter Choice: ')
downloadVideo(ch)
elif sys.argv[1] == '-c' or sys.argv[1] == '--channel':
searchQuery = sys.argv[2]
number = input("Enter the number of videos you want to get back (max: 15): ")
getVideosFromChannel(searchQuery, number)
displayScraped(videos)
ch = input("Enter choice: ")
playVideo(ch)
elif sys.argv[1] == '-l' or sys.argv[1] == '--link':
searchQuery = sys.argv[2]
number = input("Enter the number of videos you want to get back (max: 15): ")
videoSearch(searchQuery, number)
display(videos)
ch = input('Enter Choice: ')
try:
getLink(ch)
except:
print("Couldn't copy the link to clipboard. Make sure you have the right command line tools installed. You can check what tools this script uses for your OS in the help menu.")
else:
try:
number = int(sys.argv[1])
searchQuery = sys.argv[2]
except:
print("Number of results not provided. Defaulting to 5...")
number = 5
searchQuery = sys.argv[1]
videoSearch(searchQuery, number)
display(videos)
ch = input('Enter Choice: ')
playVideo(ch)