1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| import requests
import sys
import glob
import os
from os.path import basename, splitext
import RPi.GPIO as GPIO
from time import sleep
import threading
##############################################################
class StopThread(StopIteration): pass
threading.SystemExit = SystemExit, StopThread
class Thread2(threading.Thread):
def stop(self):
self.__stop = True
def _bootstrap(self):
if threading._trace_hook is not None:
raise ValueError('Cannot run thread with tracing!')
self.__stop = False
sys.settrace(self.__trace)
super()._bootstrap()
def __trace(self, frame, event, arg):
if self.__stop:
raise StopThread()
return self.__trace
#############################################################
filepath = glob.glob('Music/*.mp3')[0]
filename = splitext(basename(filepath))[0]
print (filename)
LED = 24
GPIO.setmode(GPIO.BCM)
GPIO.setup(LED, GPIO.OUT)
GPIO.setwarnings(False)
def download_led():
while True:
GPIO.output(LED, GPIO.HIGH)
sleep(1)
GPIO.output(LED, GPIO.LOW)
sleep(1)
def flashLED(speed, time):
for x in range(0, time):
GPIO.output(LED, GPIO.LOW)
sleep(speed)
GPIO.output(LED, GPIO.HIGH)
sleep(speed)
def downloadMusic(lien) :
link = lien
file_name = "Music/"+lien[54:]
with open(file_name, "wb") as f:
print ("Downloading %s" % file_name)
response = requests.get(link, stream=True)
total_length = response.headers.get('content-length')
#GPIO.output(LED, GPIO.HIGH)
if total_length is None: # no content length header
f.write(response.content)
else:
dl = 0
total_length = int(total_length)
for data in response.iter_content(chunk_size=4096):
flashing_thread = Thread2(target=download_led())
flashing_thread.start()
dl += len(data)
f.write(data)
done = int(50 * dl / total_length)
sys.stdout.write("\r[%s%s]" % ('=' * done, ' ' * (50-done)))
sys.stdout.flush()
if flashing_thread and flashing_thread.isAlive():
flashing_thread.stop()
liendl = "https://archive.org/download/auboutdufil-archives/481/BranchImmersion-Movement-02Correlation.mp3"
fileliendl = liendl[54:]
liendlsansex = fileliendl[:-4]
if filename != liendlsansex:
#flashLED(0.1, 10)
downloadMusic(liendl)
os.system("rm Music/"+filename+".mp3")
#flashLED(0.1, 10)
GPIO.cleanup() |
Partager