Category Archives: Python

yfinance 0.2.3 on raspberry pi

I have a silly little script that checks the shares each day and sends me a telegram, but it stopped working. After an hour or so of analysis I determined that the version of yfinance on raspberry pi that I had, 1.74, was returning None on any call. If I upgrade to 2.3 on Windows (update pip,pip install -upgrade pip, then pip install –upgrade yfinance, it all starts working again.

Getting it to work on Raspberry Pi was not so simple. This is a record of what I did.

Show the currently installed packages in the venv

source ./.venv/bin/activate
pip list

Upgrade pip and try to upgrade yfinance

pip install --upgrade pip
pip install --upgrade yfinance

You get an error saying that rust is the wrong version. Doing sudo apt install rustc does not install a version of rust that is high enough. So you turn to rust istelf and use:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh 

Then follow the instructions. You need to kill the shell or read the message and source “$HOME/.cargo/env”. Then try to upgrade yfinance again.

pip install --upgrade yfinance

It now fails with:

error: command '/usr/bin/arm-linux-gnueabihf-gcc' failed with exit code 1
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for cryptography
Failed to build cryptography
ERROR: Could not build wheels for cryptography, which is required to install pyproject.toml-based projects

So try to get gcc working. I read that installing gcc libffi-dev libssl-dev and python3-dev might help so did that, though I already had gcc and python3-dev installed so just needed this:

sudo apt-get install libffi-dev libssl-dev
pip install --upgrade yfinance

And THAT fixed it 😁so I now have yfinance 0.2.3 and yfinance works.

python as systemctl service

Thank you to codementor.io/@ufukafak/how-to-run-a-python-script-in-linux-with-systemd-1nh2x3hi0e

That worked really well. I had to put full path to the settings files in the py script for it to work though. This command works fine.

sudo systemctl xxxxx teslabot.service

This means I can retire one of my Rpi for other projects.

Update: I changed the user in the .service file to openhabian so that it would use the ~/ path successfully forthe config and I would not have to manually change it.

Spotipy – Squeezeserver

We have an old squeezeserver but hardly use it. Nostalgic for the music, I set myself the task of programatically making a Spotify playlist of all the tracks. Slow progress but I do progress:

  1. Find the squeezeserer sqllite database and export it using Firefox plugin SQLite manage;. I exported csv and cleaned it up in Excel. I can export that as a csv once I nail the rest of it.
  2. Install Spotipy. Follow all the instructions about making an account with the developer side of Spotify.
  3. Try to get the Spotipy example for playlists working. FAIL (authentication).
  4. Try to get WSL Debian to launch a brower so you can authenticate. FAIL.
  5. Try to get Powershell to launch the same. Success! but later fail.
  6. Try to set environment variables in Powershell. FAIL
  7. Tear hair out.
  8. Realise you can hard code the variables in…

Spotify API and Squeezebox transfer

Had fun and games getting the API to work with spotipy. All due t orustiness and inexpeerience. I just couldn’t get it to authenticate until I realised how to put my credentials in the script.

This was all part of a project to move our entire library of ripped CDs from the Squeezebox to Spotify. I was able to export the database to excel, clean it up, use the excel to make lines of code, then went through in batches of 100. I figured this would be quicker than hacking away maging it work with the spotify paging system as it was a one off task.

I did put the API authentication details in to a json file though:

{"client_id": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"client_secret": "xxxxxxxxxxxxxxxxxxxxxxxxxx",
"redirect_uri": "https://localhost:8080"
}

The code (no promises it is not changed a bit), but it might help someone.

# Shows a user's playlists

import spotipy
from spotipy.oauth2 import SpotifyOAuth
import json

import sys
import pprint

# Opening JSON file
f = open('authorisation.json')
# returns JSON object as 
# a dictionary
data = json.load(f)

f = open('playlist_track_ids.json')
a_track_ids = json.load(f)

# scope = 'playlist-read-private'
# sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=data['client_id'],
#                                     client_secret=data['client_secret'],
#                                     redirect_uri=data['redirect_uri'],
#                                     scope=scope))

# results = sp.current_user_playlists(limit=50)
# for i, item in enumerate(results['items']):
#     print("%d %s %s" % (i, item['name'],item['id']))

scope = 'playlist-modify-private'
sp = spotipy.Spotify(auth_manager=SpotifyOAuth(client_id=data['client_id'],
                                    client_secret=data['client_secret'],
                                    redirect_uri=data['redirect_uri'],
                                    scope=scope))


songsAndArtists = {
#'Afro Angel': 'Will Smith',
'Sandstorm': 'Cast',
'Mis-Shapes': 'Pulp',
'Mansize Rooster': 'Supergrass',
'Sleeping In': 'Menswear',
'Getting Better': 'Shed Seven',
'From a Window': 'Northern Uproar',
'The Riverboat Song': 'Ocean Colour Scene',
'Weak': 'Skunk Anansie',
'Divebomb': 'Number One Cup',
'Time': 'Marion',
'Far': 'Longpigs',
'The Universal': 'Blur',
'Dark Therapy': 'Echobelly',
'For the Dead': 'Gene',
'Ten Storey Love Song': 'The Stone Roses',
'Just': 'Radiohead',
'Lucky You': 'The Lightning Seeds',
'Carnival': 'The Cardigans',
'Anywhere': 'Dubstar',
'Come Together': 'The Smokin\' Mojo Filters',
'Fade Away': 'Oasis and Friends',
'Flying': 'Cast',
'Beautiful Ones': 'Suede',
'Everything Must Go': 'Manic Street Preachers',
'Fighting Fit': 'Gene',
'Twisted (Everyday Hurts)': 'Skunk Anansie',
'Lost Myself': 'Longpigs',
'Disco 2000': 'Pulp',
'Ready or Not': 'The Lightning Seeds',
'You\'ve Got It Bad': 'Ocean Colour Scene',
'C\'mon Kids': 'The Boo Radleys',
'Lava': 'Silver Sun',
'Kandy Pop': 'Bis',
'Teenage Angst': 'Placebo',
'Burden in My Hand': 'Soundgarden',
'Celebrity Hit List': 'Terrorvision',
'Boy Wonder': 'Speedy',
'No One Speaks': 'Geneva',
'Great Things': 'Echobelly',
'Nice Guy Eddie': 'Sleeper',
'Sparkle': 'My Life Story',
'Morning Glory': 'Oasis',
'Milk': 'Garbage featuring Tricky',
'6 Underground': 'Sneaker Pimps',
'If You\'re Thinking of Me': 'Dodgy',
'Chasing Rainbows': 'Shed Seven',
'For You': 'Electronic',
'You\'re Gorgeous': 'Babybird',
'Patio Song': 'Gorky\'s Zygotic Mynci',
'Neighbourhood': 'Space',
'Girls & Boys': 'Blur',
'The Frog Princess': 'The Divine Comedy',
'Whatever': 'Oasis',
'Lazy Lover': 'The Supernaturals',
'Lenny': 'Supergrass',
'If You Don\'t Want Me to Destroy You': 'Super Furry Animals',
'Broken Stones': 'Paul Weller',
'If I Could Talk I\'d Tell You': 'The Lemonheads',
'Street Spirit (Fade Out)': 'Radiohead',
'Not So Manic Now': 'Dubstar',
'You\'ve Got a Lot to Answer For': 'Catatonia',
'One Night Stand': 'The Aloof',


}

songsAndAlbums = {
#'Deep Blue Day': 'Trainspotting',

}

i=0
# Iterating over values
for name, artist in songsAndArtists.items():
    
    results = sp.search(q='track:' + name + ' artist:' + artist, type='track')
    items = results['tracks']['items']
    if len(items) > 0:

        track = items[0]
        try:
            a_track_ids.index(track['id'])
            #print('Found: '+track['name'])
        except:
            # #add to the list
            # sp.playlist_add_items(playlist_id='2CE0LjqBpPFqdv2CnToUIh', items=['spotify:track:'+track['id']+''], position=None)
            # a_track_ids.append(track['id'])
            print('***Added: '+track['name'])


i=0
# Iterating over values
for name, album in songsAndAlbums.items():
    
    results = sp.search(q='track:' + name + ' album:' + album, type='track')
    items = results['tracks']['items']
    if len(items) > 0:

        track = items[0]
        
        try:
            a_track_ids.index(track['id'])
            #print('Found: '+track['name'])
        except:
            # #add to the list
            # sp.playlist_add_items(playlist_id='2CE0LjqBpPFqdv2CnToUIh', items=['spotify:track:'+track['id']+''], position=None)
            # a_track_ids.append(track['id'])
            print('***Added: '+track['name'])


with open("playlist_track_ids.json", "w") as outfile: 
    json.dump(a_track_ids, outfile)