Skip to main content

Python API Wrapper for SecondHandSongs.com API

Project description

SecondHandSongs.com Python API Wrapper

Python API Wrapper for SecondHandSongs.com API


Features:

  • Supports SHS API key
  • Automatic throttle control
  • Cached requests
  • Download Youtube and Spotify content (may require specific API keys)

Requirements

pip install secondhandsongs

Using the SecondHandSongs.com API

Get performances

from secondhandsongs import Api

def print_performance(p):
    print("id:", p.id)
    print("title:", p.title)
    print("uri:", p.uri)
    print("performer_name:", p.performer_name)
    print("cover_ids:", p.cover_ids)
    print("work_ids:", p.work_ids)
    print("release_ids:", p.release_ids)
    print("originals:", p.originals)
    print("is_original:", p.is_original)
    print("has_spotify_link:", p.has_spotify_link)
    print("has_youtube_link:", p.has_youtube_link)
    print("youtube_url:", p.youtube_url)
    print("spotify_url:", p.spotify_url)


SHS_API_KEY = "YOUR_SHS_API_KEY"
api = Api(api_key=SHS_API_KEY)

performance_id = 290542
performance = api.get_performance(performance_id)
print_performance(performance)

expected result:

id: 290542
title: Summertime
uri: https://secondhandsongs.com/performance/290542
performer_name: Jane Powell
cover_ids: []
originals: ['4681']
is_original: False
has_spotify_link: True
has_youtube_link: True
youtube_url: https://www.youtube.com/watch?v=1me2dpYihvM
spotify_url: https://open.spotify.com/track/0lpMCyBjknQ7WHYXo4IY3P

The retrieved payload is available as well:

  {
    'entityType': 'performance',
    'uri': 'https://secondhandsongs.com/performance/290542',
    'title': 'Summertime',
    'performer': {
      'uri': 'https://secondhandsongs.com/artist/17651',
      'name': 'Jane Powell'},
    'isOriginal': False,
    'date': None,
    'releases': [
      {'entityType': 'release',
      'entitySubType': 'album',
      'uri': 'https://secondhandsongs.com/release/100032',
      'title': 'Date with Jane Powell',
      'performer': {
        'uri': 'https://secondhandsongs.com/artist/17651',
        'name': 'Jane Powell'}
      }
    ],
    'works': [
      {'entityType': 'work',
       'entitySubType': 'song',
       'uri': 'https://secondhandsongs.com/work/4681',
       'title': 'Summertime'
      }
    ],
    'originals': [
      {'entityType': 'work',
       'entitySubType': 'song',
       'uri': 'https://secondhandsongs.com/work/4681',
       'title': 'Summertime',
       'isRootWork': True,
       'original':
        {'entityType': 'performance',
        'uri': 'https://secondhandsongs.com/performance/4681',
        'title': 'Summertime',
        'performer':
          {'uri': 'https://secondhandsongs.com/artist/2223',
           'name': 'Abbie Mitchell'
          },
          'isOriginal': True
        }
      }
    ],
    'covers': [],
    'derivedWorks': [],
    'sampledBy': [],
    'usesSamplesFrom': []
  }

Get the original performance:

original_performance = performance.get_original()
print_performance(original_performance)

expected result:

id: 4681
title: Summertime
uri: https://secondhandsongs.com/performance/4681
performer_name: Abbie Mitchell
cover_ids: ['307264', '307273', ..., '1392891', '1388762']     #  (2242 records)
originals: []
is_original: True
has_spotify_link: False
has_youtube_link: False
youtube_url: None
spotify_url: None

Get the original work:

print(performance.work_ids)  # => ['4681']
work = performance.get_works()[0]

def print_work(w):
    print("id:", w.id)
    print("title:", w.title)
    print("uri:", w.uri)
    print("language:", w.language)
    print("version_ids (first 5):", w.version_ids[:5])
    print("total versions:", len(w.version_ids))

print_work(work)

Or retrieve the work directly by the id:

print(api.get_work(4681).title)  # => Summertime

Get artists

print(api.get_artist(1234))

Creating datasets

NOTE: Youtube and Spotify links are delivered only for requests authenticated with valid SHS api keys.

from dotenv import load_dotenv
import os
from secondhandsongs import download as dl
from secondhandsongs import dataset as ds
from secondhandsongs import Api

# Set the folder where the dataset will be generated
OUTPUT_FOLDER = './output'
DATASET_FILE = './slice.csv'

# Loading the API key from an .env file using dotenv
load_dotenv()
SHS_API_KEY = os.getenv('SHS_API_KEY')
api = Api(api_key=SHS_API_KEY)


# You can set a list of stopwords
# to skip items containing these terms in the title
STOPWORDS = ['Christmas', 'Santa', 'Claus', 'Silent Night', 'God',
             'Bells', 'Nosed', 'Glory', 'Pachelbel', 'Jesus',
             'Greensleeves', 'Snowman', 'Noel', 'Noël' ]

# Build a dataset slice with 80 different works, with 4 performances per work
ds.Dataset().create_slice(DATASET_FILE, OUTPUT_FOLDER, api,
                          num_items=80,
                          cluster_size=4,
                          stopwords=STOPWORDS)

# download the performances contained in the novel dataset file
dl.yt_download(DATASET_FILE, OUTPUT_FOLDER)

the resulting dataset will be like this:

work_id,performance_id,title,url
21356,312493,The Little Drummer Boy,https://www.youtube.com/watch?v=vujkelpIfEk
21356,321027,Little Drummer Boy,https://www.youtube.com/watch?v=nk9zMGGtRC4
21356,332119,Little Drummer Boy,https://www.youtube.com/watch?v=zYkQssoj2FU
21356,341595,The Little Drummer Boy,https://www.youtube.com/watch?v=rxaTD_bHjK8
1409,323951,Yesterday,https://www.youtube.com/watch?v=dWO_HpkY-N0
1409,326659,Yesterday,https://www.youtube.com/watch?v=MaDlTw5nG7I
1409,338349,Yesterday,https://www.youtube.com/watch?v=RMh_wTZEvk8
1409,346860,Yesterday,https://www.youtube.com/watch?v=1XjU6unPID0
...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

secondhandsongs-1.0.1.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

secondhandsongs-1.0.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file secondhandsongs-1.0.1.tar.gz.

File metadata

  • Download URL: secondhandsongs-1.0.1.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.2

File hashes

Hashes for secondhandsongs-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2f5dc44c2f516c2e60daac8b4f4f08c6564ec0a21a8bc84d88b8016b06452c15
MD5 c53a7ad1af51497b1a9fc29f90421447
BLAKE2b-256 95acb04c64c058aa3aad2c245d98236bb7b48ba88619b1aed8e66c1f355754d9

See more details on using hashes here.

File details

Details for the file secondhandsongs-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: secondhandsongs-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 8.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/52.0.0.post20210125 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.8.2

File hashes

Hashes for secondhandsongs-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 937c95ad4a9746852c04b2c0ac587b833c2a6af8a918d131c870a61b4691bbb0
MD5 4257b53cb068214aab347294c8ad205c
BLAKE2b-256 ad7f7567b04bfaf34be7bb44a850e9965b12f47af5ec0cecdfb8e0b017bc91b6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page