getnetradio/main.py

78 lines
1.3 KiB
Python
Raw Normal View History

2022-10-06 10:55:32 +00:00
from os import listdir,system as ss
from time import sleep
#Theards
import threading
#Time&Date
from datetime import date
import time
2022-10-06 19:28:51 +00:00
#Config
import config
#Check bitrate and convert audio
from pydub import AudioSegment
from pydub.utils import mediainfo
2022-10-06 19:28:51 +00:00
2022-10-06 10:55:32 +00:00
2022-10-06 19:28:51 +00:00
#Import config
tw = int(config.time)
prefix = config.prefix
rad = config.radio
dev = bool(config.dev)
###
2022-10-06 19:28:51 +00:00
2022-10-06 17:03:49 +00:00
if dev == False:
wait = tw*60
else:
wait = tw
2022-10-06 10:55:32 +00:00
2022-10-06 17:03:49 +00:00
#Check bitrate and upload
def tg(i,tim,group):
bit = int(mediainfo(i)['bit_rate'])/1000
if bit > 128:
sound = AudioSegment.from_file(i)
sound.export("tt.mp3", format="mp3", bitrate="128k")
ss("mv tt.mp3 "+i)
2022-10-06 17:03:49 +00:00
ss("telegram-upload "+i+" --to "+group+" --caption "+tim)
ss("mkdir "+prefix+tim)
ss("mv "+i+" "+prefix+tim+"/")
2022-10-06 10:55:32 +00:00
#Upload in telegram
def upload():
today = date.today()
files = listdir()
#Set date&time
tim = today.strftime("%d.%m.%Y_"+time.strftime("%H:%M", time.localtime()))
for i in files:
2022-10-06 19:28:51 +00:00
if rad.get(i) == None:
pass
else:
print("SUCCESS")
tg(i,tim,rad[i][1])
#Download function
def rad_down(ch):
ss("wget "+ch)
2022-10-06 17:03:49 +00:00
2022-10-06 10:55:32 +00:00
while True:
2022-10-06 10:55:32 +00:00
#Run download
for i in rad:
2022-10-06 19:28:51 +00:00
th = threading.Thread(target=rad_down, args=(rad[i][0],))
2022-10-06 10:55:32 +00:00
th.start()
#Wait
sleep(wait)
#Stop download
ss("killall wget")
#Upload
upload()
2022-10-06 17:03:49 +00:00
if dev == True:
break