Skip to content

Commit ba01a6a

Browse files
Examples directory updates (#1055)
* Fixed scripts in examples directory that didn't work, deleted any redundant examples. * Added examples for methods related to audiobooks, shows and episodes * Updated changelog --------- Co-authored-by: Stéphane Bruckert <stephane.bruckert@gmail.com>
1 parent 645ed6d commit ba01a6a

18 files changed

+268
-57
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
Add your changes below.
1010

1111
### Added
12+
- Added examples for audiobooks, shows and episodes methods to examples directory
1213

1314
### Fixed
15+
- Fixed scripts in examples directory that didn't run correctly
1416

1517
### Removed
1618

examples/add_saved_episodes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Add episodes to current user's library
3+
Usage: add_saved_episodes.py -e episode_id episode_id ...
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
scope = 'user-library-modify'
11+
12+
def get_args():
13+
parser = argparse.ArgumentParser(description='Add episodes to library')
14+
# Default args set to This American Life episodes 814 and 815
15+
parser.add_argument('-e', '--eids', nargs='+',
16+
default=['6rxg9Lpt2ywNHFea8LxEBO', '7q8or6oYYRFQFYlA0remoy'],
17+
help='Episode ids')
18+
return parser.parse_args()
19+
20+
def main():
21+
args = get_args()
22+
print('Adding following episode ids to library: ' + str(args.eids))
23+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
24+
sp.current_user_saved_episodes_add(episodes=args.eids)
25+
26+
27+
if __name__ == '__main__':
28+
main()

examples/add_saved_shows.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Add shows to current user's library
3+
Usage: add_saved_shows.py -s show_id show_id ...
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
scope = 'user-library-modify'
11+
12+
def get_args():
13+
parser = argparse.ArgumentParser(description='Add shows to library')
14+
# Default args set to Radiolab and 99% invisible
15+
parser.add_argument('-s', '--sids', nargs='+',
16+
default=['2hmkzUtix0qTqvtpPcMzEL', '2VRS1IJCTn2Nlkg33ZVfkM'],
17+
help='Show ids')
18+
return parser.parse_args()
19+
20+
def main():
21+
args = get_args()
22+
print('Adding following show ids to library: ' + str(args.sids))
23+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
24+
sp.current_user_saved_shows_add(shows=args.sids)
25+
26+
27+
if __name__ == '__main__':
28+
main()

examples/check_show_is_saved.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""
2+
Check if shows are saved in user's library
3+
Usage: check_show_is_saved -s show_id show_id ...
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
scope = 'user-library-read'
11+
12+
def get_args():
13+
parser = argparse.ArgumentParser(description='Check that a show is saved')
14+
# Default args set to Radiolab and 99% invisible
15+
parser.add_argument('-s', '--sids', nargs='+',
16+
default=['2hmkzUtix0qTqvtpPcMzEL', '2VRS1IJCTn2Nlkg33ZVfkM'],
17+
help='Show ids')
18+
return parser.parse_args()
19+
20+
def main():
21+
args = get_args()
22+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
23+
results = sp.current_user_saved_episodes_contains(episodes=args.sids)
24+
show_names = sp.shows(shows=args.sids)
25+
# Print show names and if show is saved by current user
26+
for i, show in enumerate(show_names['shows']):
27+
print(show['name'] + ': ' + str(results[i]))
28+
29+
30+
if __name__ == '__main__':
31+
main()

examples/delete_saved_episodes.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""
2+
Delete episodes from current user's library
3+
Usage: delete_saved_episodes.py -e episode_id episode_id ...
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
scope = 'user-library-modify'
11+
12+
def get_args():
13+
parser = argparse.ArgumentParser(description='Delete episodes from library')
14+
# Default args set to This American Life episodes 814 and 815
15+
parser.add_argument('-e', '--eids', nargs='+',
16+
default=['6rxg9Lpt2ywNHFea8LxEBO', '7q8or6oYYRFQFYlA0remoy'],
17+
help='Episode ids')
18+
return parser.parse_args()
19+
20+
def main():
21+
args = get_args()
22+
print('Deleting following episode ids from library: ' + str(args.eids))
23+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
24+
sp.current_user_saved_episodes_delete(episodes=args.eids)
25+
26+
27+
if __name__ == '__main__':
28+
main()

examples/follow_playlist.py

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
import argparse
1+
# Follow a playlist
22

3+
import argparse
34
import spotipy
45
from spotipy.oauth2 import SpotifyOAuth
56

7+
scope = 'playlist-modify-public'
8+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(scope=scope))
69

710
def get_args():
811
parser = argparse.ArgumentParser(description='Follows a playlist based on playlist ID')
9-
parser.add_argument('-p', '--playlist', required=True, help='Playlist ID')
10-
12+
# Default to Top 50 Global if no playlist is provided
13+
parser.add_argument('-p', '--playlist', help='Playlist ID', nargs='?', default='37i9dQZEVXbMDoHDwVN2tF')
1114
return parser.parse_args()
1215

1316

1417
def main():
1518
args = get_args()
16-
17-
if args.playlist is None:
18-
# Uses the Spotify Global Top 50 playlist
19-
spotipy.Spotify(auth_manager=SpotifyOAuth()).current_user_follow_playlist(
20-
'37i9dQZEVXbMDoHDwVN2tF')
21-
22-
else:
23-
spotipy.Spotify(auth_manager=SpotifyOAuth()).current_user_follow_playlist(args.playlist)
24-
19+
sp.current_user_follow_playlist(args.playlist)
2520

2621
if __name__ == '__main__':
27-
main()
22+
main()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Print chapter titles and lengths for given audiobook
3+
Usage: get_audiobooks_chapters_info.py -a audiobook_id
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
11+
def get_args():
12+
parser = argparse.ArgumentParser(description='Get chapter info for an audiobook')
13+
# Default set to Dune
14+
parser.add_argument('-a', '--audiobook', default='2h01INWMBvfpzNMpGFzhdF', help='Audiobook id')
15+
return parser.parse_args()
16+
17+
18+
def main():
19+
args = get_args()
20+
print('Getting chapter info for follow audiobook id: ' + str(args.audiobook))
21+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
22+
results = sp.get_audiobook_chapters(id=args.audiobook)
23+
# Print chapter name and length
24+
for item in results['items']:
25+
print('Name: ' + item['name'] + ', length: ' + str(round(item['duration_ms']/60000,1)) + ' minutes')
26+
27+
28+
if __name__ == '__main__':
29+
main()

examples/get_audiobooks_info.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""
2+
Print audiobook title and description for a list of audiobook ids
3+
Usage: get_audiobooks_info.py -a audiobook_id audiobook_id ...
4+
"""
5+
6+
import argparse
7+
import spotipy
8+
from spotipy.oauth2 import SpotifyOAuth
9+
10+
def get_args():
11+
parser = argparse.ArgumentParser(description='Get information for a list of audiobooks')
12+
# Defaults set to The Great Gatsby, The Chronicles of Narnia and Dune
13+
parser.add_argument('-a', '--aids', nargs='+',
14+
default=['6qjpt1CUHhKXiNoeNoU7nu', '1ezmXd68LbDtxebvygEQ2U', '2h01INWMBvfpzNMpGFzhdF'],
15+
help='Audiobook ids')
16+
return parser.parse_args()
17+
18+
def main():
19+
args = get_args()
20+
print('Getting info for follow audiobook ids: ' + str(args.aids) + '\n')
21+
sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
22+
results = sp.get_audiobooks(ids=args.aids)
23+
# Print book title and description
24+
for book in results['audiobooks']:
25+
print('Title: ' + book['name'] + '\n' + book['description'] + '\n')
26+
27+
28+
if __name__ == '__main__':
29+
main()

examples/playlist_add_items.py

Lines changed: 0 additions & 12 deletions
This file was deleted.

examples/read_a_playlist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
client_credentials_manager = SpotifyClientCredentials()
66
sp = spotipy.Spotify(client_credentials_manager=client_credentials_manager)
77

8-
playlist_id = 'spotify:user:spotifycharts:playlist:37i9dQZEVXbJiZcmkrIHGU'
8+
playlist_id = '37i9dQZEVXbJiZcmkrIHGU'
99
results = sp.playlist(playlist_id)
1010
print(json.dumps(results, indent=4))

0 commit comments

Comments
 (0)