import os
import sys
import traceback
from threading import Thread
from PyQt5.QtGui import *
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import *
from PyQt5.QtMultimedia import *
from PyQt5.QtCore import *
from PyQt5 import uic
import requests
from lxml import html
import pyglet
from bs4 import BeautifulSoup
from selenium import webdriver
from time import sleep
def log_uncaught_exceptions(ex_cls, ex, tb):
text = '{}: {}:\n\n'.format(ex_cls.__name__, ex)
text += ''.join(traceback.format_tb(tb))
QMessageBox.critical(None, 'Ошибка!', text)
quit()
sys.excepthook = log_uncaught_exceptions
class Radio(QWidget):
def __init__(self):
super().__init__()
avbin = os.path.join(os.path.dirname(__file__), 'avbin')
pyglet.lib.load_library(avbin)
pyglet.have_avbin=True
self.player = pyglet.media.Player()
self.initUI()
def resource_path(self, relative):
if hasattr(sys, '_MEIPASS'):
return os.path.join(sys._MEIPASS, relative)
else:
return os.path.join(os.path.abspath("."), relative)
def initUI(self):
self.title = ''
# загружаем файл (window.ui)
self.window = uic.loadUi(self.resource_path("window.ui"), self)
#self.label.setStyleSheet('background-image: url(self.resource_path("bg.jpg"))')
self.pixmap = QPixmap(self.resource_path("bg.jpg"))
self.label.setPixmap(self.pixmap)
self.setWindowTitle('Интернет радио')
self.setWindowIcon(QIcon(self.resource_path('icon.jpg')))
#self.setWindowOpacity(0.5)
self.pushButton.clicked.connect(self.running)
self.pushButton.setIconSize(QSize(41,41))
self.pushButton.setIcon(QIcon(QPixmap("button.jpg")))
self.label_4.setVisible(False)
self.label_3.setVisible(False)
def mouseMoveEvent(self, event):
txt = "Mouse 위치 ; x={0},y={1}, global={2},{3}".format(event.x(), event.y(), event.globalX(), event.globalY())
print(event.x())
def running(self):
self.th = Thread(target=self.run)
self.th.daemon = True
self.th.start()
def run(self):
self.label_4.setText('00')
self.label_4.setVisible(True)
self.label_3.setVisible(True)
try:
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2) AppleWebKit/537.36\
(KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Referer': 'https://radiopotok.ru/radio/178/',
'Host': 'listen1.myradio24.com:9000',
'Accept': '*/*'
}
url = 'http://listen1.myradio24.com:9000/4455'
r = requests.get(url, headers=headers, stream=True)
except Exception as exc:
print(exc)
try:
i = 0 # счетчик блоков
SIZE_BLOCK = 1024
with open("radio.mp3", 'wb') as f:
for block in r.iter_content(SIZE_BLOCK):
f.write(block)
i+=1
if i == 100:
self.play()
self.setWindowState(Qt.WindowActive)
self.label_3.setVisible(False)
self.label_4.setVisible(False)
self.th2 = Thread(target=self.data)
self.th2.daemon = True
self.th2.start()
elif i <= 100:
self.label_4.setText(str(i))
except Exception as exc:
print(exc)
def play(self):
try:
source = pyglet.media.load("radio.mp3",streaming=True)
self.player.queue(source)
self.player.play()
except Exception as exc:
print(exc)
def closeEvent(self, event):
pass
def data(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2)\
AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/63.0.3239.84 Safari/537.36',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Cookie': 'VID=08kKmh1IoFnf0000080G541f:::; p=d6MAAKD7tAAA; mrcu=E0E45A5A8FEB27F2A1E777B8DFB0; b=iEQCAGAtNVcAERfeEAAACOdiYBcA; c=7o9aWgEAAHsTAAAUAAAACQAQ'
}
url = 'https://radiopotok.ru/radio/178'
r = requests.get(url, headers=headers, stream=True)
parsed_body = html.fromstring(r.text)
self.title = parsed_body.xpath('//title/text()')[0]
self.label_2.setText(self.title)
# Beautiful Soup
item = BeautifulSoup(r.text, 'lxml')
a = item.find('div', {'class': 'player-song-wrapper'}).find('a').get('href')[0]
t = item.find('a', {'class': 'just-link small-caps player-song'}).text
#m = parsed_body.xpath('.//div[@class = "player-control player-control-navbar sprite play-black-32"]/a/text()')
print(a)
print(t)
if __name__ == '__main__':
app = QApplication(sys.argv)
radio = Radio()
radio.show()
sys.exit(app.exec_())
def data(self):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.2)\
AppleWebKit/537.36 (KHTML, like Gecko)\
Chrome/63.0.3239.84 Safari/537.36',
'Accept-Language': 'ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7',
'Cookie': 'VID=08kKmh1IoFnf0000080G541f:::; p=d6MAAKD7tAAA; mrcu=E0E45A5A8FEB27F2A1E777B8DFB0; b=iEQCAGAtNVcAERfeEAAACOdiYBcA; c=7o9aWgEAAHsTAAAUAAAACQAQ'
}
url = 'https://radiopotok.ru/radio/178'
r = requests.get(url, headers=headers, stream=True)
parsed_body = html.fromstring(r.text)
self.title = parsed_body.xpath('//title/text()')[0]
self.label_2.setText(self.title)
# Beautiful Soup
item = BeautifulSoup(r.text, 'lxml')
a = item.find('div', {'class': 'player-song-wrapper'}).find('a').get('href')[0]
t = item.find('a', {'class': 'just-link small-caps player-song'}).text