spotipy/examples/get_audiobook_chapters_info.py
jonathan-huston ba01a6aee5
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>
2025-01-12 14:47:10 +00:00

29 lines
898 B
Python

"""
Print chapter titles and lengths for given audiobook
Usage: get_audiobooks_chapters_info.py -a audiobook_id
"""
import argparse
import spotipy
from spotipy.oauth2 import SpotifyOAuth
def get_args():
parser = argparse.ArgumentParser(description='Get chapter info for an audiobook')
# Default set to Dune
parser.add_argument('-a', '--audiobook', default='2h01INWMBvfpzNMpGFzhdF', help='Audiobook id')
return parser.parse_args()
def main():
args = get_args()
print('Getting chapter info for follow audiobook id: ' + str(args.audiobook))
sp = spotipy.Spotify(auth_manager=SpotifyOAuth())
results = sp.get_audiobook_chapters(id=args.audiobook)
# Print chapter name and length
for item in results['items']:
print('Name: ' + item['name'] + ', length: ' + str(round(item['duration_ms']/60000,1)) + ' minutes')
if __name__ == '__main__':
main()