-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlexRedis.rb
106 lines (87 loc) · 2.62 KB
/
PlexRedis.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
require 'redis'
require 'text/double_metaphone'
class PlexRedis
CLEAN = /[^A-Za-z0-9_]/
def initialize
if REDIS_NAME.to_s == ''
raise ArgumentError, 'REDIS_NAME is not valid. Must be a string.'
elsif REDIS_NAME =~ CLEAN
raise ArgumentError, 'REDIS_NAME should only have alphanumberic characters and underscores ("_").'
end
connect
end
def search server_name, section_id, query
words = query.upcase.strip.gsub(/\s+/, ' ').gsub(/[^A-Z0-9 ]/, '').split(' ').uniq
meta_words = []
words.each do |word|
meta_words += Text::Metaphone.double_metaphone(word)
end
base_key = real_key 'index_'+server_name, section_id
keys = []
meta_words.uniq.each do |meta_word|
if meta_word != nil && meta_word != ''
keys << base_key+':'+meta_word
end
end
return @redis.sunion keys
end
# Search Indexing
INDEX_KEYS = ['title', 'parentTitle', 'grandparentTitle', 'summary', 'originalTitle', 'titleSort']
def reindex server_name, section_type, section_id, xml
if section_type == 'show' || section_type == 'movie'
search = 'MediaContainer/Video'
elsif section_type == 'artist'
search = 'MediaContainer/Track'
end
REXML::Document.new(xml).elements.each(search) do |element|
string = ''
INDEX_KEYS.each do |key|
if element.attributes.has_key? key
string += ' ' + element.attributes[key]
end
end
words = string.upcase.strip.gsub(/\s+/, ' ').gsub(/[^A-Z0-9 ]/, '').split(' ').uniq
meta_words = []
words.each do |word|
meta_words += Text::Metaphone.double_metaphone(word)
end
id = element.attributes['key'].sub('/library/metadata/','').to_i
base_key = real_key 'index_'+server_name, section_id
meta_words.uniq.each do |meta_word|
if meta_word != nil && meta_word != ''
@redis.sadd base_key+':'+meta_word, id
end
end
end
set_config server_name+'_'+section_id+'_sha1sum', Digest::SHA1.base64digest(xml)
end
# Config
def set_config key, value
return @redis.set real_key('config', key), value
end
def get_config key
return @redis.get real_key('config', key)
end
#cleaners
RKS = ':'
def real_key realm, key
return REDIS_NAME + RKS + clean_string(realm) + RKS + clean_string(key)
end
def clean_string string
return string.to_s.gsub(CLEAN, '')
end
# setup connection
def connect
if REDIS_SOCK != false
@redis = Redis.new(:path => REDIS_SOCK)
elsif REDIS_HOST == false && REDIS_PORT == false
@redis = Redis.new
elsif REDIS_HOST == false
@redis = Redis.new(:port => REDIS_PORT)
elsif REDIS_PORT == false
@redis = Redis.new(:host => REDIS_HOST)
else
@redis = Redis.new(:host => REDIS_HOST, :port => REDIS_PORT)
end
end
end