File: plagger.rb

package info (click to toggle)
yapra 0.1.2-7.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 620 kB
  • ctags: 449
  • sloc: ruby: 3,572; makefile: 9
file content (94 lines) | stat: -rw-r--r-- 2,485 bytes parent folder | download | duplicates (3)
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
## Interleave Plagger processing -- Soutaro Matsumoto
##
## Invokes Plagger and process the input with your Plagger.
## The input must be an Array of RSS::RDF::Item.
## The output is an Array of RSS::RDF::Item.
## If "debug" is a value evaluated to true, tempolary files for/from Plagger won't be deleted.
## If "input" is not "feed", the input will be ignored.
## If you omit "dir", the default is /var.
## Make sure your system have /var directory and pragger/Plagger can write.
##
## - module: plagger
##   config:
##     input: feed
##     debug: false
##     dir: /var
##     plugins: 
##       - module: Publish::CSV
##         config: 
##           dir: /var
##           filename: a.csv

require 'rbconfig'
require 'open-uri'
require 'rss/1.0'
require 'rss/2.0'
require 'pathname'

raise LoadError unless ENV['PATH'].split(Config::CONFIG['PATH_SEPARATOR']).find {|dir|
  File.executable?(File.join(dir, 'plagger')) || File.executable?(File.join(dir, 'plagger.bat'))
}

def plagger(config, data)
  pla_con = config["plugins"]
  
  if input_option(config) == :feed
    pla_con = [{"module" => "Subscription::Feed", "config"=>{ "url" => "file:#{pla_input(config)}" }}] + pla_con
    eval_pragger([{"module" => "save_rss", "config" => { "filename" => pla_input(config).to_s }}], data)
  end
  
  pla_con.push({"module" => "Publish::Feed",
                 "config" =>
                 {"dir" => "/var",
                   "format" => "RSS",
                   "filename" => pla_output(config).basename.to_s}})
  
  pla_config(config).open("w") {|io| io.write to_plagger_yaml(YAML.dump({ "plugins" => pla_con })) }
  
  system "plagger -c #{pla_config(config)}"
  
  begin
    RSS::Parser.parse(pla_output(config).read).items
  rescue
    []
  ensure
    unless config.has_key?("debug")
      pla_config(config).delete
      pla_input(config).delete
      pla_output(config).delete
    end
  end
end

def pla_dir(config)
  Pathname.new(config.has_key?("dir") ? config["dir"] : "/var")
end

def pla_config(config)
  pla_dir(config) + "pla.yaml"
end

def pla_output(config)
  pla_dir(config) + "pla_output"
end

def pla_input(config)
  pla_dir(config) + "pla_input"
end

def input_option(config)
  opt = config.has_key?("input") ? config["input"] : "nothing"
  case opt
  when "feed": :feed
  when "nothing": :nothing
  else
    :nothing
  end
end

def to_plagger_yaml(yaml)
  a = yaml.split(/\n/)
  ret = a[1]+"\n"
  ret + a[2..(a.size-1)].collect {|x| "  "+x }.join("\n")+"\n"
end