Skip to content

Commit

Permalink
Update __init__.py
Browse files Browse the repository at this point in the history
  • Loading branch information
leranp authored Oct 14, 2023
1 parent 69e495c commit 49658a0
Showing 1 changed file with 65 additions and 68 deletions.
133 changes: 65 additions & 68 deletions custom_components/galatz_news/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,83 +73,80 @@ def play_galatz_news_service(call):

_LOGGER = logging.getLogger(__name__) # Import _LOGGER here

x = datetime.datetime.now()
Day = x.strftime("%d")
month = x.strftime("%m")
Year = x.strftime("%y")
Year2 = x.strftime("%Y")
Hour = x.strftime("%H")
Minute = x.strftime("%M")


user_agents = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/111.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
]
random_user_agent = random.choice(user_agents)

session = requests.Session()

headers = {
'User-Agent': random_user_agent,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}

# Save Http Get Responed from the hourlynews Page to hourlynewsHTML.txt file
response = session.get("https://www.kan.org.il/radio/hourlynews.aspx", headers=headers)

with open("/hourlynewsHTML.txt", "w", encoding="utf8") as f:
f.write(response.text)

# Take the entryId and Hour values from the hourlynewsHTML.txt file
try:
with open('/hourlynewsHTML.txt', encoding="utf8") as myfile:
content = myfile.read()
entryId = re.search(r'entryId/(.*?)\/format',
x = datetime.datetime.now()
Day = x.strftime("%d")
month = x.strftime("%m")
Year = x.strftime("%y")
Year2 = x.strftime("%Y")
Hour = x.strftime("%H")
Minute = x.strftime("%M")


user_agents = [
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/110.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:109.0) Gecko/20100101 Firefox/111.0",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/109.0.0.0 Safari/537.36"
]
random_user_agent = random.choice(user_agents)

session = requests.Session()

headers = {
'User-Agent': random_user_agent,
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
}

# Save Http Get Responed from the hourlynews Page to hourlynewsHTML.txt file
response = session.get("https://www.kan.org.il/radio/hourlynews.aspx", headers=headers)

with open("/hourlynewsHTML.txt", "w", encoding="utf8") as f:
f.write(response.text)

# Take the entryId and Hour values from the hourlynewsHTML.txt file
try:
with open('/hourlynewsHTML.txt', encoding="utf8") as myfile:
content = myfile.read()
entryId = re.search(r'entryId/(.*?)\/format',
content, re.DOTALL).group(1)
hour = re.search(r'<span class="title player-title">(\d{2}:\d{2})</span>',
hour = re.search(r'<span class="title player-title">(\d{2}:\d{2})</span>',
content, re.DOTALL).group(1)
except AttributeError:
entryId = "not_found"
hour = "not_found"

_LOGGER.error("No Url was found")
except AttributeError:
entryId = "not_found"
hour = "not_found"

_LOGGER.error("No Url was found")

# Create the Url for the next HTTP request
Url = "https://cdnapisec.kaltura.com/html5/html5lib/v2.92/mwEmbedFrame.php/p/2717431/uiconf_id/47265863/entry_id/" + \
entryId+"?wid=_2717431&iframeembed=true&playerId=kaltura_player_1615983371&entry_id=value"

# Save Http Get Responed from the Secound Page to response2.txt file
response2 = requests.get(Url)
with open("/response2.txt", "w", encoding="utf8") as f:
f.write(response2.text)
# Create the Url for the next HTTP request
Url = "https://cdnapisec.kaltura.com/html5/html5lib/v2.92/mwEmbedFrame.php/p/2717431/uiconf_id/47265863/entry_id/" + \
entryId+"?wid=_2717431&iframeembed=true&playerId=kaltura_player_1615983371&entry_id=value"

# Take the flavorId value from the response2.txt file
try:
with open('/response2.txt', encoding="utf8") as myfile:
content = myfile.read()
# flavorId = re.search(r'flavorId/(.*?)\/format',
# content, re.DOTALL).group(1)
flavorId = re.search(r'containerFormat":"mp3","videoCodecId":null,"status":2,"id":"(.*?)\","entryId',
content, re.DOTALL).group(1)
except AttributeError:
flavorId = "not_found"
_LOGGER.error("not_found")
# Save Http Get Responed from the Secound Page to response2.txt file
response2 = requests.get(Url)
with open("/response2.txt", "w", encoding="utf8") as f:
f.write(response2.text)

# Take the flavorId value from the response2.txt file
try:
with open('/response2.txt', encoding="utf8") as myfile:
content = myfile.read()
flavorId = re.search(r'containerFormat":"mp3","videoCodecId":null,"status":2,"id":"(.*?)\","entryId',
content, re.DOTALL).group(1)
Url = "https://vod.media.kan.org.il/pd/p/2717431/sp/271743100/serveFlavor/entryId/" + \
entryId+"/v/1/ev/3/flavorId/"+flavorId+"/name/a.mp3"
_LOGGER.error(Url)

service_data = {
"entity_id": media_player_entity_id,
"media_content_id": Url,
"media_content_type": "music",
}
hass.services.call("media_player", "play_media", service_data)
except AttributeError:
flavorId = "not_found"
_LOGGER.error("not_found")

# Print the URL of the MP3 file
Url = "https://vod.media.kan.org.il/pd/p/2717431/sp/271743100/serveFlavor/entryId/" + \
entryId+"/v/1/ev/3/flavorId/"+flavorId+"/name/a.mp3"
_LOGGER.error(Url)

service_data = {
"entity_id": media_player_entity_id,
"media_content_id": Url,
"media_content_type": "music",
}
hass.services.call("media_player", "play_media", service_data)

hass.services.register(DOMAIN, "play_galatz_news", play_galatz_news_service)
hass.services.register(DOMAIN, "play_kan_news", play_kan_news_service)
Expand Down

0 comments on commit 49658a0

Please sign in to comment.