Skip to content

Commit 6315a3c

Browse files
authored
Merge pull request #3 from lindell/play_random
Added play_random
2 parents 6e3557e + ff84796 commit 6315a3c

File tree

4 files changed

+65
-3
lines changed

4 files changed

+65
-3
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ Play the latest video or clip from a specific program. There exist an option to
2525
category: Intervjuer # Optional
2626
```
2727
28+
### Play random
29+
Play a random video or clip from a specific program. There exist an option to just random from specific categories.
30+
```yaml
31+
- service: svt_play.play_random
32+
entity_id: media_player.living_room_tv
33+
data:
34+
program_name: skavlan
35+
category: Intervjuer # Optional
36+
```
37+
2838
### Play Channel
2939
Play one of the svt channels.
3040
```yaml
@@ -40,7 +50,7 @@ Play one of the svt channels.
4050
4151
Copy the `custom_components/svt_play` folder in this repository to `<home assistant config>/custom_components/svt_play`
4252

43-
Or:
53+
Or:
4454

4555
Install via [Home Assistant Community Store](https://hacs.xyz/)
4656

custom_components/svt_play/__init__.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
import homeassistant.helpers.config_validation as cv
33
import voluptuous as vol
4-
from .video_url_fetch.video_fetch import video_url_by_video_id, video_id_by_time, video_url_by_channel, suggested_video_id
4+
from .video_url_fetch.video_fetch import video_url_by_video_id, video_id_by_time, video_url_by_channel, suggested_video_id, random_video_id
55
from .validation import category_names
66

77
DOMAIN = "svt_play"
@@ -26,6 +26,13 @@
2626
CONF_CATEGORY: category_names,
2727
})
2828

29+
SERVICE_PLAY_RANDOM = 'play_random'
30+
SERVICE_PLAY_RANDOM_SCHEMA = vol.Schema({
31+
CONF_ENTITY_ID: cv.entity_ids,
32+
CONF_PROGRAM_NAME: str,
33+
CONF_CATEGORY: category_names,
34+
})
35+
2936
SERVICE_PLAY_CHANNEL = 'play_channel'
3037
SERVICE_PLAY_CHANNEL_SCHEMA = vol.Schema({
3138
CONF_ENTITY_ID: cv.entity_ids,
@@ -75,6 +82,26 @@ async def play_latest(service):
7582
DOMAIN, SERVICE_PLAY_LATEST, play_latest, SERVICE_PLAY_LATEST_SCHEMA
7683
)
7784

85+
async def play_random(service):
86+
"""Play a random svt play video from a specified program"""
87+
88+
entity_id = service.data.get(CONF_ENTITY_ID)
89+
program_name = service.data.get(CONF_PROGRAM_NAME)
90+
category = service.data.get(CONF_CATEGORY)
91+
92+
video_url = video_url_by_video_id(
93+
random_video_id(program_name, categories=category)
94+
)
95+
96+
await hass.services.async_call('media_player', 'play_media', {
97+
'entity_id': entity_id,
98+
'media_content_id': video_url,
99+
'media_content_type': 'video'
100+
})
101+
hass.services.async_register(
102+
DOMAIN, SERVICE_PLAY_RANDOM, play_random, SERVICE_PLAY_RANDOM_SCHEMA
103+
)
104+
78105
async def play_channel(service):
79106
"""Play the specified channel"""
80107

custom_components/svt_play/video_url_fetch/live_test.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from video_fetch import video_url_by_channel, video_id_by_time, video_url_by_video_id, suggested_video_id
1+
from video_fetch import video_url_by_channel, video_id_by_time, video_url_by_video_id, suggested_video_id, random_video_id
22
import pytest
33

44

@@ -32,3 +32,14 @@ def test_not_found_by_suggested():
3232
def test_not_found_channel():
3333
with pytest.raises(Exception, match="Could not fetch video url: Not found"):
3434
video_url_by_channel("svt1337")
35+
36+
37+
def test_random():
38+
id = random_video_id('skavlan')
39+
same_counter = 0
40+
for _ in range(5):
41+
new_id = random_video_id('skavlan')
42+
print(new_id)
43+
if id == new_id:
44+
same_counter += 1
45+
assert same_counter != 5

custom_components/svt_play/video_url_fetch/video_fetch.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from requests import post
33
from datetime import datetime
44
from datetime import timezone
5+
import random
56
import iso8601
67

78
default_formats = ['dashhbbtv', 'hls-cmaf', 'hls']
@@ -66,6 +67,19 @@ def video_id_by_time(program_id, index=0, categories=None):
6667
return videos[index]['item']['videoSvtId']
6768

6869

70+
def random_video_id(program_id, categories=None):
71+
"Get a random video id from the specified program"
72+
program_data = information_by_program_id(program_id)
73+
74+
videos = []
75+
for content in program_data['associatedContent']:
76+
if categories is None or content['name'] in categories:
77+
videos += content['items']
78+
79+
index = random.randint(0, len(videos) - 1)
80+
return videos[index]['item']['videoSvtId']
81+
82+
6983
def suggested_video_id(program_id):
7084
"Get the video id if the suggested video"
7185
program_data = information_by_program_id(program_id)

0 commit comments

Comments
 (0)