mns_subscriber 0.7.0 → 0.7.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c9a3f061eca9581f000c9fd37c9341da37e29e723a883627b220045bc15daa68
4
- data.tar.gz: 2c7ac5bb79b3b0bbc7edaf86c512aa06fd63d258ed2429788ee6d2f18644f26d
3
+ metadata.gz: e2ddc06764160255bf154806e01be715caa37041f21966e01d79b75e6329639b
4
+ data.tar.gz: 18c8c1b25c56b449761ab622742219a5b10a7f485e907902027d3303c0c2e069
5
5
  SHA512:
6
- metadata.gz: 7ec27ad80dd5ccf4d69e9156a98ee5fc64870be0789f3d0eeaed9a449d52f1abec50eea61821d3e7ef97184c35882d0f1000a071507c7dd99ab45bba88141d44
7
- data.tar.gz: 2adfaff285eb6677b02a0d4b501cf0f60400e91f0a6abe1dcc11a47602f8ae245b35ffbe66884fce01dc769742ce0201b7fe059e1a58ceb233307ce1f0534d2d
6
+ metadata.gz: 12c18783de474d14ba80b5baefaea3aad9c1dae802ae48e1c0d80cf4787c07a07f394c12b1619390aac611cf804757c733d0f429cf0903eadc4d1b543b81c4d3
7
+ data.tar.gz: a0779ad109d9f9347ec6f298a5ca490a54507294c542d8c27908df8835debd5d3f846fcae13e8626d37d791d684ae178fe1dab03ebc1b2fda871f60a65ef2042
checksums.yaml.gz.sig CHANGED
Binary file
@@ -10,22 +10,23 @@ require 'recordx_sqlite'
10
10
  require 'easyimg_utils'
11
11
 
12
12
 
13
- module MNSSubscriber
14
-
13
+ module MNSSubscriber
14
+
15
15
  class NoticeMgr
16
+ include RXFHelperModule
16
17
  using ColouredText
17
-
18
- def initialize(sps=nil, dir: '.', options: {}, timeline: nil,
18
+
19
+ def initialize(sps=nil, dir: '.', options: {}, timeline: nil,
19
20
  hashtag_url: nil, log: nil, debug: false)
20
-
21
+
21
22
  @sps, @log, @debug = sps, log, debug
22
-
23
- # note: a valid url_base must be provided
23
+
24
+ # note: a valid url_base must be provided
24
25
  @options = {
25
- url_base: 'http://yourwebsitehere.co.uk/',
26
- dx_xslt: '/xsl/dynarex.xsl',
27
- rss_xslt: '/xsl/feed.xsl',
28
- target_page: :page,
26
+ url_base: 'http://yourwebsitehere.co.uk/',
27
+ dx_xslt: '/xsl/dynarex.xsl',
28
+ rss_xslt: '/xsl/feed.xsl',
29
+ target_page: :page,
29
30
  target_xslt: '/xsl/page.xsl',
30
31
  local_media_path: '/home/user/media',
31
32
  url_media_path: 'http://media.yourwebsitehere.co.uk/'
@@ -33,190 +34,210 @@ module MNSSubscriber
33
34
 
34
35
 
35
36
  @filepath, @timeline = dir, timeline
36
-
37
+
37
38
  @index = nil
38
39
  @hashtags = nil
39
-
40
-
40
+
41
+
41
42
  if hashtag_url then
42
-
43
- @hashtag_url = @options[:url_base] + hashtag_url.sub(/^\//,'')
44
-
43
+
44
+ @hashtag_url = @options[:url_base] + hashtag_url.sub(/^\//,'')
45
+
45
46
  hashtag_path = File.join(dir, 'hashtag')
46
47
  tagdb = File.join(hashtag_path, 'index.db')
47
- FileUtils.mkdir_p File.dirname(tagdb)
48
-
48
+ FileX.mkdir_p File.dirname(tagdb)
49
+
49
50
  h = {hashtags: {id: '', tag: '', topic: '', noticeid: ''}}
50
- @hashtags = RecordxSqlite.new(tagdb, table: h )
51
+ @hashtags = RecordxSqlite.new(tagdb, table: h )
51
52
 
52
- end
53
+ end
53
54
  end
54
-
55
+
55
56
  def incoming(topic, msg)
56
-
57
+
57
58
  a = topic.split('/')
58
59
  context = a.last.to_sym
59
-
60
+
60
61
  case context
61
62
  when :profile
62
-
63
- update_attributes(:description, subtopic=a[-2], profile=msg)
63
+
64
+ #update_attributes(:description, subtopic=a[-2], profile=msg)
65
+ h = JSON.parse msg, symbolize_names: true
66
+ update_profile(subtopic=a[-2], h)
64
67
 
65
68
  when :link
66
69
  puts 'we have a link' if @debug
67
70
  update_attributes(:link, subtopic=a[-2], link=msg)
68
-
71
+
69
72
  when :title
70
-
73
+
71
74
  update_attributes(:title, subtopic=a[-2], title=msg)
72
-
75
+
73
76
  when :image
74
77
  #puts 'we have an image'
75
- update_attributes(:image, subtopic=a[-2], title=msg)
76
-
78
+ update_attributes(:image, subtopic=a[-2], title=msg)
79
+
77
80
  when :delete
78
-
81
+
79
82
  delete_notice(subtopic=a[-2], msg)
80
-
81
- when :json
82
-
83
- # JSON contains a message and 1 or more media files
83
+
84
+ when :json
85
+
86
+ # JSON contains a message and 1 or more media files
84
87
  h = JSON.parse msg, symbolize_names: true
85
88
  puts 'h: ' + h.inspect if @debug
86
-
89
+
87
90
  subtopic = a[-2]
88
-
91
+
89
92
  t = Time.now
90
93
  id = t.to_i.to_s + t.strftime("%2N")
91
-
94
+
92
95
  filepath = File.join(@options[:local_media_path], 'images')
93
-
96
+
94
97
  a = h[:files].map.with_index do |f,i|
95
-
98
+
96
99
  # is f an Array object containing various sized images of the 1 file?
97
100
  a2 = f.is_a?(Array) ? f : [f]
98
-
101
+
99
102
  scale = a2.map do |imgfile|
100
-
103
+
101
104
  scale = imgfile[/(_\w+)\.\w+$/,1].to_s
102
105
  file = "%s%s%s" % [(id.to_i + i+1).to_s(36).reverse, scale, File.extname(imgfile)]
103
106
  dest = File.join(filepath, file)
104
-
105
- FileUtils.cp imgfile, dest
106
- FileUtils.chmod 0755, dest
107
-
107
+
108
+ FileX.cp imgfile, dest
109
+ FileX.chmod 0755, dest
110
+
108
111
  file
109
-
112
+
110
113
  end
111
-
112
- index = scale[1] ? 1 : 0
114
+ puts 'scale: ' + scale.inspect
115
+ index = scale[2] ? 2 : (scale[1] ? 1 : 0)
113
116
  file1 = scale[index]
114
-
117
+
115
118
  dir = File.extname(file1) =~ /\.(?:jpg|png)/ ? 'images' : ''
116
119
  url = [@options[:url_media_path], dir, file1].join('/')
117
-
118
- href = [@options[:url_base].sub(/\/$/,''), subtopic, 'status', id,
120
+
121
+ href = [@options[:url_base].sub(/\/$/,''), subtopic, 'status', id,
119
122
  'photo', (i+1).to_s ].join('/')
120
123
  # find the best y position for the image letterbox view
121
124
  pct = EasyImgUtils.new(a2[index]).best_viewport
122
125
  pct2 = (pct / 10.0).to_i * 10
123
126
 
124
127
  align = if pct < 35 then
125
-
128
+
126
129
  if pct < 10 then
127
130
  :top
128
131
  else
129
132
  'y' + pct2.to_s
130
133
  end
131
-
134
+
132
135
  elsif pct >= 70
133
136
  :bottom
134
137
  else
135
138
  #'y' + pct.to_s
136
139
  :center
137
- end
138
-
140
+ end
141
+
139
142
  {url: url, align: align}
140
-
143
+
141
144
  end
142
-
143
-
144
- card = if h[:card] and h[:card][:summary_large_image] then
145
-
146
- h[:card][:summary_large_image][:img] = a[1][:url] if a.any?
147
- h[:card]
148
-
145
+
146
+
147
+ card = if h[:card] then
148
+
149
+ puts 'h[:card]: ' + h[:card].inspect
150
+ puts 'a: ' + a.inspect
151
+
152
+ if h[:card][:summary_large_image] then
153
+
154
+ h[:card][:summary_large_image][:img] = a[2][:url] if a.any?
155
+ h[:card]
156
+
157
+ elsif h[:card][:summary]
158
+
159
+ h[:card][:summary][:img] = a[0][:url] if a.any?
160
+ h[:card]
161
+
162
+ end
163
+
149
164
  elsif a.any?
150
165
  {images: a}
151
166
  end
152
- #puts ('card: ' + card.inspect).debug
153
-
167
+ puts 'before build'
168
+ #puts ('card: ' + card.inspect).debug
169
+
154
170
  h[:msg] += build(card) if card
155
171
  add_notice(subtopic, h[:msg].gsub('<','&lt;')\
156
- .gsub('>','&gt;').gsub("\r",''), id,
172
+ .gsub('>','&gt;').gsub("\r",''), id,
157
173
  (card ? card.to_json : nil))
158
-
159
- else
160
-
174
+
175
+ else
176
+
161
177
  subtopic, id = a[1..-1]
162
- add_notice(subtopic, msg, id)
163
-
178
+ add_notice(subtopic, msg, id)
179
+
164
180
  end
165
181
  end
166
-
182
+
167
183
  private
168
-
184
+
169
185
  def add_notice(topic, raw_msg, raw_id=nil, card=nil)
170
186
 
171
187
  @log.info 'mns_subscriber/add_notice: active' if @log
172
188
  topic_dir = File.join(@filepath, 'u', topic)
173
-
189
+
174
190
  options = @options.clone
175
191
  options.delete :local_media_path
176
192
  options.delete :url_media_path
177
-
178
- notices = DailyNotices.new topic_dir, options.merge(identifier: topic,
193
+
194
+ notices = DailyNotices.new topic_dir, options.merge(identifier: topic,
179
195
  title: topic.capitalize + ' daily notices', log: @log)
180
196
 
181
197
  t = Time.now
182
198
  id = (raw_id || t.to_i.to_s + t.strftime("%2N")).to_i
183
-
199
+
184
200
  # strip out any JSON from the end of the message
185
- msg, raw_json = raw_msg.split(/(?=\{.*)/)
186
-
201
+ msg, raw_json = raw_msg.split(/(?=\{.*)/)
202
+
187
203
  msg = ' ' if msg.nil?
188
204
  mtlite = MTLite.new(msg)
189
-
205
+
190
206
  desc = if mtlite.to_html(para: false) =~ /<\w+/ then
191
207
  mtlite.to_html(para: true, ignore_domainlabel:true)
192
208
  else
193
209
  mtlite.to_s
194
210
  end
195
-
211
+
196
212
  if @hashtag_url then
197
213
 
198
214
  tags = desc.scan(/(?<= #)\w+/)
199
-
200
- desc.gsub!(/ #\w+/) do |x|
201
- "<a href='%s%s'>%s</a>" % [@hashtag_url, x[2..-1], x]
215
+
216
+ desc.gsub!(/ [#\$][a-zA-Z]\w+/) do |rawx|
217
+
218
+ x = rawx.lstrip
219
+ baseurl = x[0] == '#' ? @hashtag_url : 'search?q='
220
+ url = baseurl + CGI.escape(x.sub(/^#/,''))
221
+ " <a href='%s'>%s</a>" % [url, x]
222
+
202
223
  end
203
-
204
- # add the record to the database
224
+
225
+ # add the record to the database
205
226
  tags.each do |tag|
206
-
227
+
207
228
  t = Time.now
208
- id2 = (t.to_i.to_s + t.strftime("%2N")).to_i
229
+ id2 = (t.to_i.to_s + t.strftime("%2N")).to_i
209
230
  h = {id: id2, tag: tag, topic: topic, noticeid: id}
210
231
 
211
232
  @hashtags.create h if @hashtags
212
-
233
+
213
234
  end
214
-
235
+
215
236
  end
216
-
237
+
217
238
  title = mtlite.to_s.lines.first.chomp
218
239
  title = title[0..136] + ' ...' if title.length > 140
219
-
240
+
220
241
  h = {
221
242
  title: title,
222
243
  description: desc,
@@ -226,77 +247,78 @@ module MNSSubscriber
226
247
  #puts 'inside add_notice h: ' + h.inspect
227
248
  return_status = notices.add(item: h, id: id.to_s)
228
249
  #puts 'return_status: ' + return_status.inspect
229
-
250
+
230
251
  return if return_status == :duplicate
231
-
252
+
232
253
  rxnotices = RecordxSqlite.new(File.join(topic_dir, 'notices.db'),
233
254
  table: {notices: {id: 0, description: '', message: ''}})
234
-
255
+
235
256
  begin
236
257
  rxnotices.create id: id.to_s, description: desc, message: msg
237
258
  rescue
238
259
  puts 'warning: rxnotices.create -> ' + ($!).inspect
239
260
  end
240
-
261
+
241
262
  if raw_json then
242
-
263
+
243
264
  record = JSON.parse(raw_json)
244
265
  index = RecordxSqlite.new(File.join(topic_dir, 'index.db'),
245
266
  table: {items: record})
246
267
  index.create record
247
-
268
+
248
269
  update_index_xml(index, topic_dir)
249
-
270
+
250
271
  end
251
-
272
+
252
273
  @sps.notice "%s/add: %s/status/%s" % [@timeline, topic, id] if @timeline
253
-
274
+
254
275
  sleep 0.3
255
-
276
+
256
277
  end
257
-
278
+
258
279
  def delete_notice(topic, msg)
259
-
280
+
260
281
  topic_dir = File.join(@filepath, topic)
261
-
282
+
262
283
  id = msg.to_i
263
-
264
- feed = DailyNotices.new topic_dir, log: @log
284
+
285
+ feed = DailyNotices.new topic_dir, log: @log
265
286
  feed.delete id
266
-
267
- notices = RecordxSqlite.new(File.join(topic_dir, 'notices.db'),
287
+
288
+ notices = RecordxSqlite.new(File.join(topic_dir, 'notices.db'),
268
289
  table: 'notices')
269
290
  notices.delete id
270
-
291
+
271
292
  indexdb = File.join(topic_dir, 'index.db')
272
-
293
+
273
294
  if File.exists? indexdb then
274
-
295
+
275
296
  index = RecordxSqlite.new(indexdb, table: 'items')
276
297
  index.delete id
277
298
  update_index_xml(index, topic_dir)
278
299
  end
279
-
300
+
280
301
  end
281
-
302
+
282
303
  # builds the HTML for a given card
283
304
  #
284
305
  def build(card)
306
+ puts 'inside build'
285
307
 
286
308
  #return card.inspect
287
- #next unless card.is_a? Hash
309
+ #next unless card.is_a? Hash
288
310
 
289
311
  if card.is_a? Hash then
290
-
291
- card2 = case card.keys.first
312
+
313
+ card2 = case card.keys.first
292
314
  when :images
293
-
315
+
294
316
  card[:images].map.with_index do |img, i|
295
-
317
+
296
318
  "<img class='img1' src='%s'/>" % img[:url]
297
-
319
+
298
320
  end.join
299
-
321
+
300
322
  when :summary_large_image
301
323
 
302
324
  h2 = card[:summary_large_image]
@@ -304,64 +326,106 @@ module MNSSubscriber
304
326
  rawdesc = h2[:desc]
305
327
 
306
328
  desc = rawdesc.length > 147 ? rawdesc[0..147] + '...' : rawdesc
307
- site = h2[:url][/https?:\/\/([^\/]+)(?=\/)/,1].sub(/^www\./,'')
329
+ site = h2[:url][/https?:\/\/([^\/]+)/,1].sub(/^www\./,'')
308
330
  title = h2[:title]
309
331
  img = h2[:img]
310
332
  url = h2[:url]
311
333
 
312
- # The following Card HTML template is for use with the RSS feed.
313
- # The actual HTML template for the card is rendered dynamically
314
- # from the web server.
334
+ # The following Card HTML template is for use with the RSS feed.
335
+ # The actual HTML template for the card is rendered dynamically
336
+ # from the web server.
315
337
  "
316
- <div class='card'><a href='#{url}' target='_blank'>" +
317
- "<div class='top-crop center'><img src='#{img}'/></div>" +
318
- "<span class='title'>#{title}</span></a><p>#{desc}</p>" +
338
+ <div class='card'><a href='#{url}' target='_blank'>" +
339
+ "<div class='top-crop center'><img src='#{img}'/></div>" +
340
+ "<span class='title'>#{title}</span></a><p>#{desc}</p>" +
319
341
  "<span class='link'><span class='linkurl'>#{site}</span></span></div>"
320
342
 
343
+ when :summary
344
+
345
+ h2 = card[:summary]
346
+ puts 'h2: ' + h2.inspect
347
+ rawdesc = h2[:desc]
348
+
349
+ desc = rawdesc.length > 120 ? rawdesc[0..120] + '...' : rawdesc
350
+ site = h2[:url][/https?:\/\/([^\/]+)(?=\/)/,1].sub(/^www\./,'')
351
+ title = h2[:title]
352
+ img = h2[:img]
353
+ url = h2[:url]
354
+
355
+ # The following Card HTML template is for use with the RSS feed.
356
+ # The actual HTML template for the card is rendered dynamically
357
+ # from the web server.
358
+
359
+ "<div class='card2'><a href='#{url}' target='_blank'><div><div id='col1'>" +
360
+ "<img src='#{img}'></div><div id='col2'><div id='content'>" +
361
+ "<span class='title'>#{title}</span><span class='desc'>#{desc}</span>" +
362
+ "<span class='link'><span class='linkurl'>#{site}</span></span></div>" +
363
+ "</div></div></a></div>"
364
+
321
365
  end
322
366
  end
323
367
 
324
- end
325
-
368
+ end
369
+
326
370
  def update_index_xml(index, topic_dir)
327
-
371
+
328
372
  # create the index.xml file
329
-
373
+
330
374
  a = index.order(:desc).first(15)
331
375
  a2 = a.map {|x| h = x.to_h; id = h.delete(:id); {item_id: id}.merge(h)}
332
376
 
333
- dx = Dynarex.new
377
+ dx = Dynarex.new
334
378
  dx.import a2
335
379
  dx.order ='descending'
336
-
337
- dx.save File.join(topic_dir, 'index.xml')
338
-
380
+
381
+ dx.save File.join(topic_dir, 'index.xml')
382
+
339
383
  end
340
384
 
341
385
  def update_attributes(attribute, topic, value)
342
-
386
+
343
387
  topic_dir = File.join(@filepath, topic)
344
388
  notices = DailyNotices.new topic_dir, @options.merge(identifier: topic)
345
389
  notices.method((attribute.to_s + '=').to_sym).call(value)
346
390
  notices.save
347
-
391
+
392
+ end
393
+
394
+ def update_profile(topic, h)
395
+
396
+ topic_dir = File.join(@filepath, topic)
397
+ notices = DailyNotices.new topic_dir, @options.merge(identifier: topic)
398
+
399
+ valid_keys = %i(title identifier image bio location website banner_image)
400
+
401
+ h.each_pair do |key, value|
402
+
403
+ next unless valid_keys.include? key
404
+
405
+ attribute = key.to_s
406
+ notices.method((attribute + '=').to_sym).call(value)
407
+
408
+ end
409
+
410
+ notices.save
411
+
348
412
  end
349
413
 
350
-
414
+
351
415
  end
352
416
 
353
417
  class Client < SPSSub
354
418
 
355
- def initialize(host: 'sps', port: 59000, dir: '.', options: {},
419
+ def initialize(host: 'sps', port: 59000, dir: '.', options: {},
356
420
  timeline: nil, log: nil, hashtag_url: nil)
357
-
421
+
358
422
  @log = log
359
423
  log.info 'mns_subscriber/initialize: active' if log
360
- @nm = NoticeMgr.new(self, dir: dir, options: options, timeline: timeline,
424
+ @nm = NoticeMgr.new(self, dir: dir, options: options, timeline: timeline,
361
425
  hashtag_url: hashtag_url, debug: true)
362
-
426
+
363
427
  super(host: host, port: port, log: log)
364
-
428
+
365
429
  end
366
430
 
367
431
  def subscribe(topic='notice/*')
@@ -371,13 +435,13 @@ module MNSSubscriber
371
435
  private
372
436
 
373
437
  def ontopic(topic, msg)
374
-
438
+
375
439
  @log.info 'mns_subscriber/ontopic: topic: ' + topic.inspect if @topic
376
440
 
377
441
  a = topic.split('/')
378
442
  #puts "%s: %s %s" % [topic, Time.now.to_s, msg.inspect]
379
-
380
- @nm.incoming topic, msg
443
+
444
+ @nm.incoming topic, msg
381
445
 
382
446
  end
383
447
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mns_subscriber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0
4
+ version: 0.7.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Robertson
@@ -35,7 +35,7 @@ cert_chain:
35
35
  yOAycOYzp7fxmPl3/HV5D0YOJwuy1XslA50GQJwq12A5vMyUGUCHuxH/zKZcM/zr
36
36
  0QZaKiVqq5ZeGvni0eQSavhQ
37
37
  -----END CERTIFICATE-----
38
- date: 2021-03-29 00:00:00.000000000 Z
38
+ date: 2022-01-17 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: mtlite
@@ -86,7 +86,7 @@ dependencies:
86
86
  version: '0.7'
87
87
  - - ">="
88
88
  - !ruby/object:Gem::Version
89
- version: 0.7.1
89
+ version: 0.7.2
90
90
  type: :runtime
91
91
  prerelease: false
92
92
  version_requirements: !ruby/object:Gem::Requirement
@@ -96,7 +96,7 @@ dependencies:
96
96
  version: '0.7'
97
97
  - - ">="
98
98
  - !ruby/object:Gem::Version
99
- version: 0.7.1
99
+ version: 0.7.2
100
100
  - !ruby/object:Gem::Dependency
101
101
  name: recordx_sqlite
102
102
  requirement: !ruby/object:Gem::Requirement
@@ -106,7 +106,7 @@ dependencies:
106
106
  version: '0.3'
107
107
  - - ">="
108
108
  - !ruby/object:Gem::Version
109
- version: 0.3.1
109
+ version: 0.3.3
110
110
  type: :runtime
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
@@ -116,7 +116,7 @@ dependencies:
116
116
  version: '0.3'
117
117
  - - ">="
118
118
  - !ruby/object:Gem::Version
119
- version: 0.3.1
119
+ version: 0.3.3
120
120
  - !ruby/object:Gem::Dependency
121
121
  name: easyimg_utils
122
122
  requirement: !ruby/object:Gem::Requirement
@@ -126,7 +126,7 @@ dependencies:
126
126
  version: '0.7'
127
127
  - - ">="
128
128
  - !ruby/object:Gem::Version
129
- version: 0.7.3
129
+ version: 0.7.4
130
130
  type: :runtime
131
131
  prerelease: false
132
132
  version_requirements: !ruby/object:Gem::Requirement
@@ -136,7 +136,7 @@ dependencies:
136
136
  version: '0.7'
137
137
  - - ">="
138
138
  - !ruby/object:Gem::Version
139
- version: 0.7.3
139
+ version: 0.7.4
140
140
  description:
141
141
  email: digital.robertson@gmail.com
142
142
  executables: []
metadata.gz.sig CHANGED
Binary file