Class: Kodipity::Kodi

Inherits:
Object
  • Object
show all
Defined in:
lib/kodipity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Kodi

Returns a new instance of Kodi.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/kodipity.rb', line 12

def initialize(url)
  @url = url
  @media = {movies: {}, recordings: [], tv_shows: {}, channels: {}}
  @headers = {"Content-Type" => 'application/json'}

  @_movies = '{"jsonrpc": "2.0", "method": "VideoLibrary.GetMovies", "params": { "properties" : ["art", "rating", "thumbnail", "playcount", "file"], "sort": { "order": "ascending", "method": "label", "ignorearticle": true } }, "id": "libMovies"}'
  @_player_active = '{"jsonrpc": "2.0", "id": 1, "method": "Player.GetProperties", "params": {"properties": ["speed"], "playerid": 1}}'
  @_channels = '{"jsonrpc": "2.0", "id": 1, "method": "PVR.GetChannelGroups", "params": {"channeltype" : "tv"}}}'
  @_recordings = '{"jsonrpc": "2.0", "id": 1, "method": "PVR.GetRecordings"}}'
  @_player_pause = '{"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": 1 }, "id": 1}'
end

Instance Attribute Details

#_channelsObject (readonly)

Returns the value of attribute _channels.



9
10
11
# File 'lib/kodipity.rb', line 9

def _channels
  @_channels
end

#_moviesObject (readonly)

Returns the value of attribute _movies.



9
10
11
# File 'lib/kodipity.rb', line 9

def _movies
  @_movies
end

#_player_activeObject (readonly)

Returns the value of attribute _player_active.



9
10
11
# File 'lib/kodipity.rb', line 9

def _player_active
  @_player_active
end

#_player_pauseObject (readonly)

Returns the value of attribute _player_pause.



9
10
11
# File 'lib/kodipity.rb', line 9

def _player_pause
  @_player_pause
end

#_recordingsObject (readonly)

Returns the value of attribute _recordings.



9
10
11
# File 'lib/kodipity.rb', line 9

def _recordings
  @_recordings
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/kodipity.rb', line 9

def headers
  @headers
end

#mediaObject (readonly)

Returns the value of attribute media.



10
11
12
# File 'lib/kodipity.rb', line 10

def media
  @media
end

#urlObject (readonly)

Returns the value of attribute url.



9
10
11
# File 'lib/kodipity.rb', line 9

def url
  @url
end

Instance Method Details

#active_playersObject



78
79
80
81
82
83
# File 'lib/kodipity.rb', line 78

def active_players
  out = {}
  HTTParty.post(url, headers: headers, body: '{"jsonrpc": "2.0", "method": "Player.GetActivePlayers", "id": 1}')['result'].each do |player|
    out = player.merge(out)
  end
end

#channelsObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/kodipity.rb', line 42

def channels
  return media[:channels] unless media[:channels].empty?

  channel_groups_resp = HTTParty.post url, headers: headers, body: _channels
  channel_groups = channel_groups_resp['result']['channelgroups'].map{ |group| group['channelgroupid'] }

  channel_groups.each do |id|
    channel_group = {jsonrpc: '2.0', id: 1, method: 'PVR.GetChannels', params: {channelgroupid: id}, playerid: 1}
    HTTParty.post(@url, headers: @headers, body: channel_group.to_json)['result']['channels'].each do |channel|
      media[:channels][channel['label']] = Kodipity::Channel.new(channel['label'], channel['channelid'])
    end
  end
  media[:channels]
end

#docs(param) ⇒ Object



70
71
72
# File 'lib/kodipity.rb', line 70

def docs(param)
  HTTParty.post(url, headers: headers, body: '{ "jsonrpc": "2.0", "method": "JSONRPC.Introspect", "params": { "filter": { "id": "Player.Open", "type": "method" } }, "id": 1 }')
end

#moviesObject

_movies = ‘{“jsonrpc”: “2.0”, “method”: “VideoLibrary.GetMovies”, “params”: { “properties” : [“art”, “rating”, “thumbnail”, “playcount”, “file”], “sort”: { “order”: “ascending”, “method”: “label”, “ignorearticle”: true } }, “id”: “libMovies”}’ _player_active = ‘“2.0”, “id”: 1, “method”: “Player.GetProperties”, “params”: {“properties”: [“speed”], “playerid”: 1}’ _channels = ‘“2.0”, “id”: 1, “method”: “PVR.GetChannelGroups”, “params”: {“channeltype” : “tv”}}’ _recordings = ‘“2.0”, “id”: 1, “method”: “PVR.GetRecordings”}’ _player_pause = ‘{“jsonrpc”: “2.0”, “method”: “Player.PlayPause”, “params”: { “playerid”: 1 }, “id”: 1}’



34
35
36
37
38
39
40
# File 'lib/kodipity.rb', line 34

def movies
  return media[:movies] unless media[:movies].empty?
  HTTParty.post(url, headers: headers, body: _movies)['result']['movies'].each do |movie|
    media[:movies][movie['label']] = movie['movieid']
  end
  media[:movies]
end

#pauseObject



90
91
92
# File 'lib/kodipity.rb', line 90

def pause
  HTTParty.post(url, headers: headers, body: _player_pause)
end

#play(file) ⇒ Object



85
86
87
88
# File 'lib/kodipity.rb', line 85

def play(file)
  file_url = URI.encode(file)
  HTTParty.post(url, headers: headers, body: '{"jsonrpc": "2.0", "method": "Player.Open", "params": { "item": {"recordingid": "pvr://recordings/active///The%20Goldbergs%20The%20Greatest%20Musical%20Ever%20Written,%20TV%20(7.1%20WJLADT),%2020161201_010000.pvr"} }, id": 1}')
end

#play_channel(channel_name) ⇒ Object



112
113
114
115
116
# File 'lib/kodipity.rb', line 112

def play_channel(channel_name)
  channels.each do |channel, channel_obj|
    return channel_obj if channel.start_with? channel_name
  end
end

#play_next_recording(tv_show) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/kodipity.rb', line 94

def play_next_recording(tv_show)
  recs = recordings.select{ |rec| rec.title.include? tv_show }
  recs.sort_by!{ |rec| rec.start_time}

  watched_episode = false

  recs.each do |rec|
    if watched_episode
      # rec.play 
      return rec
    end
    watched_episode = true if rec.play_count > 0
  end

  # recs[0].play
  recs[0]
end

#playing?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/kodipity.rb', line 74

def playing?
  Kodipity.active_players.size > 0
end

#recObject



66
67
68
# File 'lib/kodipity.rb', line 66

def rec
  HTTParty.post(url, headers: headers, body: '{"jsonrpc": "2.0", "id": 1, "method": "PVR.GetRecordingDetails", "params": {"recordingid" : 122, "properties": ["title", "plot", "plotoutline", "file"]}, "playerid": 1}')
end

#recordings(metadata = true) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/kodipity.rb', line 57

def recordings( = true)
  return media[:recordings] unless media[:recordings].empty?
  HTTParty.post(url, headers: headers, body: _recordings)['result']['recordings'].each do |recording|
    # puts recording.to_s
    media[:recordings] << Kodipity::PVRRecording.new(recording['recordingid'], )
  end
  media[:recordings]
end