tvtid 0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.yardopts +5 -0
- data/README.md +2 -0
- data/library/tvtid.rb +16 -0
- data/library/tvtid/category.rb +26 -0
- data/library/tvtid/channel.rb +30 -0
- data/library/tvtid/client.rb +79 -0
- data/library/tvtid/program.rb +64 -0
- data/library/tvtid/schedule.rb +14 -0
- data/library/tvtid/version.rb +3 -0
- metadata +94 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a4fed7edb8d062f0e970ea68728474290b4c4f0c
|
4
|
+
data.tar.gz: c54fd7e6cceb49c6c659a8362adc1d88cede8bbe
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f0899ab3cbd46626d7c1214b3a6c6912b0d6db747f3177d669b88d9c041d47bf62346b05f79f89c3ee5ed422ccde58b1237de2fc41a31c261956c3ddd0291ac5
|
7
|
+
data.tar.gz: b9d639a7c57e2bc49d3c37db1a1c0189e5fac2eb4a5b577a77e3f9a83b4a7aeb13b8464db7b274f3f7193758336f302dab7d3e67c67a6be72b64852e65e66923
|
data/.yardopts
ADDED
data/README.md
ADDED
data/library/tvtid.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'net/http'
|
4
|
+
|
5
|
+
require 'oj'
|
6
|
+
require 'lrucache'
|
7
|
+
require 'multi_json'
|
8
|
+
|
9
|
+
require 'tvtid/client'
|
10
|
+
require 'tvtid/channel'
|
11
|
+
require 'tvtid/program'
|
12
|
+
require 'tvtid/schedule'
|
13
|
+
require 'tvtid/category'
|
14
|
+
|
15
|
+
module TVTid
|
16
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TVTid
|
4
|
+
# A category for an EPG entry.
|
5
|
+
class Category
|
6
|
+
attr_accessor :color, :shade
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
# Constructs a new category with a name.
|
10
|
+
def initialize name
|
11
|
+
@name = name
|
12
|
+
end
|
13
|
+
|
14
|
+
# Creates a new category from a json object.
|
15
|
+
#
|
16
|
+
# @returns a category.
|
17
|
+
def self.from_json json
|
18
|
+
return nil unless json['name']
|
19
|
+
|
20
|
+
Category.new(json['name']).tap do |category|
|
21
|
+
category.color = json['color']
|
22
|
+
category.shade = json['shade']
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TVTid
|
4
|
+
class Channel
|
5
|
+
attr_accessor :icon, :logo, :logo_svg, :category, :region, :language
|
6
|
+
attr_reader :id, :title
|
7
|
+
|
8
|
+
# Constructs a new channel with an id and a title.
|
9
|
+
def initialize id, title
|
10
|
+
@id = id
|
11
|
+
@title = title
|
12
|
+
end
|
13
|
+
|
14
|
+
# Creates a new channel from a json object.
|
15
|
+
#
|
16
|
+
# @returns a channel.
|
17
|
+
def self.from_json json
|
18
|
+
return nil unless json['id'] and json['title']
|
19
|
+
|
20
|
+
Channel.new(json['id'], json['title']).tap do |channel|
|
21
|
+
channel.icon = json['icon']
|
22
|
+
channel.logo = json['logo']
|
23
|
+
channel.logo_svg = json['svgLogo']
|
24
|
+
channel.category = json['category']
|
25
|
+
channel.region = json['region']
|
26
|
+
channel.language = json['lang']
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module TVTid
|
4
|
+
class Client
|
5
|
+
# The cache time to live.
|
6
|
+
CACHE_TTL = 1 * 60 * 60 * 24 # 1 day
|
7
|
+
# The soft cache time to live.
|
8
|
+
CACHE_SOFT_TTL = 7 * 60 * 60 * 24 # 7 days
|
9
|
+
# The API backend host.
|
10
|
+
API_BASE_URI = URI 'http://tvtid-app-backend.tv2.dk'
|
11
|
+
|
12
|
+
# The default HTTP request headers
|
13
|
+
HTTP_REQUEST_HEADERS = {
|
14
|
+
'User-Agent' => 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.76 Safari/537.36'
|
15
|
+
}
|
16
|
+
|
17
|
+
# The default channels to return in a days schedule
|
18
|
+
DEFAULT_CHANNELS = [1, 3, 5, 2, 31, 133, 7, 6, 4, 10155, 10154, 10153, 8,
|
19
|
+
77, 156, 10093, 10066, 14, 10089, 12566, 10111, 70,
|
20
|
+
118, 153, 94, 12948, 145, 185, 157, 15, 71, 93, 15049,
|
21
|
+
219, 37, 248, 186]
|
22
|
+
|
23
|
+
# Constructs a new client
|
24
|
+
def initialize
|
25
|
+
@cache = LRUCache.new ttl: CACHE_TTL, soft_ttl: CACHE_SOFT_TTL
|
26
|
+
@http = Net::HTTP.new API_BASE_URI.host, API_BASE_URI.port
|
27
|
+
end
|
28
|
+
|
29
|
+
# Returns a schedule for the given date
|
30
|
+
#
|
31
|
+
# @param date A date
|
32
|
+
# @param channels A list of channel ids to request schedules for
|
33
|
+
def schedules_for date, channels = []
|
34
|
+
return nil unless date.is_a? Date
|
35
|
+
|
36
|
+
channels = self.channels.select{|c| DEFAULT_CHANNELS.include? c.id } if channels.empty?
|
37
|
+
formatted_date = date.iso8601
|
38
|
+
cache_key = "schedule-#{formatted_date}-#{channels.map(&:id).join ','}"
|
39
|
+
|
40
|
+
@cache.fetch cache_key do
|
41
|
+
channel_queries = channels.map{|c| "ch=#{c.id}" }.join '&'
|
42
|
+
response = @http.get "/api/tvtid/v1/dayviews/#{formatted_date}?#{channel_queries}", HTTP_REQUEST_HEADERS
|
43
|
+
json_data = MultiJson.load response.body
|
44
|
+
|
45
|
+
json_data.map do |schedule|
|
46
|
+
channel = channels.find{|channel| channel.id == schedule['id']}
|
47
|
+
programs = schedule['programs'].map{|program| Program.from_json program }
|
48
|
+
|
49
|
+
Schedule.new channel, programs
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
# Returns a list of schedules for today
|
55
|
+
#
|
56
|
+
# This is the same as using `schedules_for Date.today`
|
57
|
+
def schedules_for_today channels = []
|
58
|
+
schedules_for Date.today, channels
|
59
|
+
end
|
60
|
+
|
61
|
+
# Returns a days schedule for a given channel and date
|
62
|
+
#
|
63
|
+
# @param channel [Channel] The channel to get the schedule for
|
64
|
+
# @param date [Date] The date of the schedule
|
65
|
+
def channel_schedule channel, date = Date.today
|
66
|
+
schedules_for(date, [channel]).first
|
67
|
+
end
|
68
|
+
|
69
|
+
# Returns a list of channels
|
70
|
+
def channels
|
71
|
+
@cache.fetch 'channels' do
|
72
|
+
response = @http.get '/api/tvtid/v1/channels', HTTP_REQUEST_HEADERS
|
73
|
+
|
74
|
+
json_data = MultiJson.load response.body
|
75
|
+
json_data.map{|json_channel_data| Channel.from_json json_channel_data }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
module TVTid
|
2
|
+
class Program
|
3
|
+
# The id of the program.
|
4
|
+
attr_reader :id
|
5
|
+
# The title of the program.
|
6
|
+
attr_reader :title
|
7
|
+
# The time the program starts at.
|
8
|
+
attr_accessor :start_time
|
9
|
+
# The time the program stops at.
|
10
|
+
attr_accessor :stop_time
|
11
|
+
# A url where the user can see more information about the program.
|
12
|
+
attr_accessor :url
|
13
|
+
# The ID of the channel.
|
14
|
+
attr_accessor :channel_id
|
15
|
+
# The category of the program.
|
16
|
+
attr_accessor :category
|
17
|
+
# The description of the program.
|
18
|
+
attr_accessor :description
|
19
|
+
# The original non-localized title of the program.
|
20
|
+
attr_accessor :original_title
|
21
|
+
# The year of production of the program.
|
22
|
+
attr_accessor :production_year
|
23
|
+
# The production country of the program.
|
24
|
+
attr_accessor :production_country
|
25
|
+
# Teaser text for the program.
|
26
|
+
attr_accessor :teaser
|
27
|
+
# Unique series id if the program is a series.
|
28
|
+
attr_accessor :series_id
|
29
|
+
# Episode and season information if the program is a series.
|
30
|
+
attr_accessor :series_info
|
31
|
+
|
32
|
+
# Constructs a new `Program` with an `id` and a `title`.
|
33
|
+
def initialize id, title
|
34
|
+
@id = id
|
35
|
+
@title = title
|
36
|
+
end
|
37
|
+
|
38
|
+
# Updates the program information from a json object.
|
39
|
+
def parse_json! json
|
40
|
+
@start_time = Time.at(json['start']).to_datetime
|
41
|
+
@stop_time = Time.at(json['stop']).to_datetime
|
42
|
+
@url = json['url'] if json.key?('url')
|
43
|
+
@channel_id = json['channelId'] if json.key?('channelId')
|
44
|
+
@category = json['category'] if json.key?('category')
|
45
|
+
@description = json['desc'] if json.key?('desc')
|
46
|
+
@production_year = json['prodYear'] if json.key?('prodYear')
|
47
|
+
@production_country = json['prodCountry'] if json.key?('prodCountry')
|
48
|
+
@teaser = json['teaser'] if json.key?('teaser')
|
49
|
+
@series_id = json['series_id'] if json.key?('seriesId')
|
50
|
+
@series_info = json['series'] if json.key?('series')
|
51
|
+
end
|
52
|
+
|
53
|
+
# Constructs a `Program` from a json object.
|
54
|
+
#
|
55
|
+
# @returns Program if json object has an `id` and a `title` - nil otherwise.
|
56
|
+
def self.from_json json
|
57
|
+
return nil unless json['id'] and json['title']
|
58
|
+
|
59
|
+
Program.new(json['id'], json['title']).tap do |program|
|
60
|
+
program.parse_json! json
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module TVTid
|
2
|
+
class Schedule
|
3
|
+
# The channel the schedule belongs to.
|
4
|
+
attr_reader :channel
|
5
|
+
# The list of programs in the schedule.
|
6
|
+
attr_reader :programs
|
7
|
+
|
8
|
+
# Constructs a new schedule for a `channel`.
|
9
|
+
def initialize channel, programs = []
|
10
|
+
@channel = channel
|
11
|
+
@programs = programs
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tvtid
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mikkel Kroman
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-01-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: multi_json
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.12'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.12'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: lrucache
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 0.1.4
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 0.1.4
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: oj
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '2.18'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '2.18'
|
55
|
+
description:
|
56
|
+
email: mk@uplink.io
|
57
|
+
executables: []
|
58
|
+
extensions: []
|
59
|
+
extra_rdoc_files: []
|
60
|
+
files:
|
61
|
+
- ".yardopts"
|
62
|
+
- README.md
|
63
|
+
- library/tvtid.rb
|
64
|
+
- library/tvtid/category.rb
|
65
|
+
- library/tvtid/channel.rb
|
66
|
+
- library/tvtid/client.rb
|
67
|
+
- library/tvtid/program.rb
|
68
|
+
- library/tvtid/schedule.rb
|
69
|
+
- library/tvtid/version.rb
|
70
|
+
homepage: https://github.com/mkroman/tvtid
|
71
|
+
licenses:
|
72
|
+
- MIT
|
73
|
+
metadata: {}
|
74
|
+
post_install_message:
|
75
|
+
rdoc_options: []
|
76
|
+
require_paths:
|
77
|
+
- library
|
78
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 1.9.1
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - ">="
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '0'
|
88
|
+
requirements: []
|
89
|
+
rubyforge_project:
|
90
|
+
rubygems_version: 2.5.2
|
91
|
+
signing_key:
|
92
|
+
specification_version: 4
|
93
|
+
summary: Client library for tvtid.tv2.dk
|
94
|
+
test_files: []
|