Skip to content

Instantly share code, notes, and snippets.

@mrhalix
Created October 31, 2021 20:49
Show Gist options
  • Save mrhalix/8fdd69312b61f4cba2f943772d502ecc to your computer and use it in GitHub Desktop.
Save mrhalix/8fdd69312b61f4cba2f943772d502ecc to your computer and use it in GitHub Desktop.
Raspberry Pi, Python 2 Music player from RadioJavan, Controlled by a Flask Webserver
import requests, json, os, subprocess, vlc, sys
from os.path import basename
def mp3fromURL(url):
"""
Catch MP3 file from spotify share url.
"""
try:
print "Downloading MP3 File ..."
response = requests.get("http://receiverdl.com/api/extract/action.php?link=" + url + "&isNoFilter=1")
#response = """{"status":"ok","message":"it's ok","groups":[{"title":"Musics","items":[{"title":"Imagine Dragons - Believer.mp3","link":"http:\/\/receiverdl.com\/api\/extract\/music\/Imagine%20Dragons%20-%20Believer.mp3"}]}]}"""
print "Got the information"
jdat = json.loads(response.text)
#jdat = json.loads(response)
mp3url = jdat['groups'][0]['items'][0]['link']
mp3filename = jdat['groups'][0]['items'][0]['title']
if mp3filename == "MP3":
mp3filename = basename(mp3url)
if os.path.isfile('mp3files/' + mp3filename):
print "File already exist"
p = vlc.MediaPlayer("file:////home/pi/Desktop/smartTV/mp3files/" + mp3filename)
p.play()
print "\nPlayed!"
return p
print "File doesn\'t exist", "Downloading it"
# ------------------ download and save part
#mp3response = requests.get(mp3url)
with open("mp3files/" + mp3filename, "wb") as mp3file:
#mp3file.write(mp3response.content)
#mp3file.close()
mp3response = requests.get(mp3url, stream=True)
total_length = mp3response.headers.get('content-length')
if total_length == "0":
print "File cannot be downloaded. the lenght is 0"
return False
if total_length is None: # no content length header
mp3file.write(mp3response.content)
else:
dl = 0
total_length = int(total_length)
for data in mp3response.iter_content(chunk_size=4096):
dl += len(data)
mp3file.write(data)
done = int(50 * dl / total_length)
#sys.stdout.write("\r[%s%s] - %s" % ('=' * done, ' ' * (50-done), done*2) )
sys.stdout.write("\r[{}{}] - {}%".format('=' * done, ' ' * (50-done), done*2) )
sys.stdout.flush()
# ------------------ download and save part
p = vlc.MediaPlayer("file:////home/pi/Desktop/smartTV/mp3files/" + mp3filename)
p.play()
print "\nPlayed!"
return p
except Exception as e:
print e
return False
import subprocess, time
import functions as funcs
from flask import Flask, request
app = Flask(__name__)
# ------------------- config
global playing
playing = False
# ------------------- config
@app.route("/")
def dd():
return "running"
@app.route("/start")
def hello():
gottenlink = request.args.get('url')
if playing:
return "error, file is already being played"
global player
player = funcs.mp3fromURL(gottenlink)
if player:
global playing
playing = True
return "started"
return "error player is not started"
@app.route("/stop")
def stop():
if playing:
player.stop()
global playing
playing = False
return "stopped"
@app.route("/seek")
def seektest():
if playing:
percentage = request.args.get('p')
player.set_position(int(percentage)/100.0)
return "set to " + str(int(percentage)/100.0)
return "not playing"
if __name__ == '__main__':
app.run("0.0.0.0", debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment