-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.rb
executable file
·161 lines (147 loc) · 3.64 KB
/
common.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require 'rubygems'
require 'dm-core'
require 'dm-validations'
require 'dm-migrations'
require 'net/http'
require 'json'
require 'time'
require 'cgi'
class Everyone
def initialize(o)
@obj = o
end
def method_missing(name, *args)
if @obj.is_a?(Array)
@obj.collect { |x| x.send(name, *args) }
elsif @obj.is_a?(Hash)
a = {}
@obj.each_pair { |k,v| a[k] = v.send(name, *args) }
a
else
@obj.send(name, *args)
end
end
end
class DateTime
def to_millis
(self.to_time.to_f * 1000).to_i
end
end
class Object
def everyone
@everyone ||= Everyone.new(self)
end
def methods_uniq
self.methods.reject { |m| Object.methods.include?(m) }.sort
end
end
class Hash
def to_json(x=nil)
'{' + self.keys.collect { |k| k.to_json(x) + ':' + self[k].to_json(x) }.join(',') + '}'
end
end
class Array
def to_json(x=nil)
'[' + self.collect { |k| k.to_json(x) }.join(',') + ']'
end
def find_by_index(idx)
self.reject { |o| o.idx != idx }
end
def select_collect
collect { |a| yield(a) }.compact
end
end
class String
def repword(k, v)
self.split(' ').collect { |a| a == k ? v : a }.join(' ')
end
def sanitise
self.strip.gsub('\'', '').gsub(/[^a-zA-Z0-9\(\) ]/, '-').gsub('-', ' - ').gsub(' ', ' ')
end
def to_searchterm
self.strip.downcase.gsub(/[^a-z0-9'\:\&\+\. ]/,'')
end
def to_json(x = nil)
return '"' + self + '.json"' if self.match(/boxsetter\.gomes\.com\.es\/[^\.]*$/)
super(x)
end
def blank?
self == ''
end
def escape
URI.escape(self)
end
def to_crid
if self.match(/^(crid:\/\/)?fp\.bbc\.co\.uk\/([0-9A-Z]+)$/)
$2
elsif self.match(/^(crid:\/\/)?bds\.tv\/([0-9A-Z]+)$/)
$2
elsif self.match(/^(crid:\/\/)?www\.itv\.com\/([0-9#]+)$/)
$2.gsub('#','-')
elsif self.match(/^(crid:\/\/)?www\.channel4\.com\/([0-9]+)\/([0-9]+)$/)
$2 + '-' + $3
elsif self.match(/^(crid:\/\/)?www\.five\.tv\/([A-Z0-9#]+)$/)
$2.gsub('#','-') + '$V'
else
self.delete("^a-zA-Z0-9")
end
end
def hexEncode
self.each_byte.map { |b| sprintf('%02x', b) }.join
end
def hexDecode
self.scan(/../).map { |x| x.hex }.pack('c*')
end
def titleise
@@commoners ||= ['and', 'a', 'an', 'the', 'of', 'on', 'in', 'with']
res = []
self.downcase.split(/\s+/).each_with_index do |a, i|
res << (i == 0 || !@@commoners.include?(a) ? a.capitalize : a)
end
res.join(' ')
end
end
def nil.blank?
true
end
REDUX_HOST = 'devapi.bbcredux.com'
REDUX_SCHEME = 'http'
REDUX_URL = REDUX_SCHEME + '://' + REDUX_HOST
REDUX_URLS = 'https://' + REDUX_HOST
BXSTR_HOST = 'boxsetter.gomes.com.es'
BXSTR_SCHEME = 'https'
BXSTR_URL = BXSTR_SCHEME + '://' + BXSTR_HOST
DEFAULT_IMAGE = 'default.png'
EPOCH = DateTime.new(1965, 11, 30)
DataMapper.setup(:default, "sqlite3://#{Dir.pwd}/db_boxsetter.sqlite3")
require './models/broadcast_entity'
require './models/user'
require './models/channel'
require './models/boxset'
require './models/season'
require './models/programme'
DataMapper.auto_upgrade!
[
['BBC One', Channel::TYPE_BBC],
['BBC Two', Channel::TYPE_BBC],
['BBC Three', Channel::TYPE_BBC],
['BBC Four', Channel::TYPE_BBC],
['CBBC', Channel::TYPE_BBC],
['CBeebies', Channel::TYPE_BBC],
['ITV1', Channel::TYPE_ITV],
['ITV2', Channel::TYPE_ITV],
['ITV3', Channel::TYPE_ITV],
['ITV4', Channel::TYPE_ITV],
['Channel 4', Channel::TYPE_CH4],
['More4', Channel::TYPE_CH4],
['E4', Channel::TYPE_CH4],
['Five', Channel::TYPE_CH5],
['Dave', Channel::TYPE_EXT],
['S4C', Channel::TYPE_EXT]
].each_with_index do |data, index|
fullname, channeltype = data
c = Channel.first_or_create(:fullname => fullname)
c.channeltype = channeltype
c.idx = index
c.save
end