Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitde83783

Browse files
Simple CLI to access youtube songs, videos etc
1 parent329f883 commitde83783

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

‎gameofthrones_quiz/quiz.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/usr/bin/env python3
2+
importsys
23
importjson
34
importrandom
45

@@ -9,7 +10,10 @@ def get_questions():
910
returnquestions_obj['questions']
1011

1112
defprint_choices(choices):
13+
# enumerate() is used to
14+
# extract index value from the list
1215
forindex,choiceinenumerate(choices):
16+
print(choice)
1317
print('{}.{}'.format(index+1,choice),'\n')
1418

1519
defis_correct(ans,user_answer):
@@ -38,7 +42,6 @@ def start_quiz():
3842
else:
3943
print('✘')
4044
print(get_greeting_msg(points,len(questions)))
41-
4245

4346
if__name__=='__main__':
4447
canPlay=input('Press y/Y to play the Game of Thrones quiz 🦁\n')

‎open-song/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#Open Song
2+
###Open youtube song from cli
3+
[-] Install dependencies`BeautifulSoup` and`Requests`
4+
[-] ```bash
5+
cd I-Learn-Python/open-song
6+
python3 open-song.py
7+
```
8+
[-] You can also alias the command if you quickly wanna open it from the terminal
9+
``` bash
10+
echo`alias open-song='python3 /path/to/the/file/'`
11+
```

‎open-song/open-song.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#!/usr/bin/env python3
2+
3+
importos
4+
importplatform
5+
importsys
6+
importrequests
7+
importre
8+
frombs4importBeautifulSoup
9+
10+
YOUTUBE_BASE='https://www.youtube.com'
11+
TARGET_CLASS='yt-simple-endpoint style-scope ytd-video-renderer'
12+
13+
defget_song_links(song):
14+
song_links= []
15+
search_query='/results?search_query='
16+
song=song.replace(' ','+')
17+
search_url=YOUTUBE_BASE+search_query+song
18+
yt_response=requests.get(search_url)
19+
soup=BeautifulSoup(yt_response.text,'html.parser')
20+
start_indices= [res.start()forresinre.finditer('/watch?',str(soup))]
21+
22+
forindexinstart_indices:
23+
link=str(soup)[index:index+20]
24+
25+
if'?v='inlink:
26+
song_links.append(YOUTUBE_BASE+link)
27+
28+
return [search_url]+song_links
29+
30+
31+
defget_open_command():
32+
os_name=platform.system()
33+
ifos_name=='Linux':
34+
return'xdg-open'
35+
ifos_name=='Darwin':
36+
return'open'
37+
ifos_name=='Windows':
38+
return'start'
39+
40+
41+
defget_song_names():
42+
args=sys.argv
43+
song_names=args[1:]
44+
returnsong_names
45+
46+
defopen_url(open_command,link):
47+
open_p=os.system('{} {}'.format(open_command,link))
48+
49+
defopen_target_links(song,ong_links):
50+
open_command=get_open_command()
51+
print('For song '+song)
52+
print (
53+
'''
54+
Enter one of these
55+
1. Open search results
56+
2. Open first result on youtube
57+
3. Open top five results
58+
or other key/s to exit
59+
'''
60+
)
61+
62+
choice=int(input())
63+
ifchoicenotin [1,2,3]:
64+
sys.exit(-1)
65+
else:
66+
ifchoice==1:
67+
open_url(open_command,song_links[0])
68+
elifchoice==2:
69+
open_url(open_command,song_links[1])
70+
else:
71+
forlinkinsong_links[1:6]:
72+
open_url(open_command,link)
73+
74+
75+
if (__name__=='__main__'):
76+
song_names=get_song_names()
77+
forsonginsong_names:
78+
song_links=get_song_links(song)
79+
open_target_links(song,song_links)
80+
81+
sys.exit(-1)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp