|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Name:: Automatic::Plugin::Subscription::StatDb |
| 3 | +# Author:: 774 <http://id774.net> |
| 4 | +# Created:: Jul 12, 2013 |
| 5 | +# Updated:: Jul 12, 2013 |
| 6 | +# Copyright:: Copyright (c) 2012-2013 Automatic Ruby Developers. |
| 7 | +# License:: Licensed under the GNU GENERAL PUBLIC LICENSE, Version 3.0. |
| 8 | + |
| 9 | +module Automatic::Plugin |
| 10 | + class SubscriptionStatDb |
| 11 | + require 'open-uri' |
| 12 | + require 'active_support' |
| 13 | + require 'active_support/core_ext' |
| 14 | + require 'rss' |
| 15 | + |
| 16 | + def initialize(config, pipeline=[]) |
| 17 | + @config = config |
| 18 | + @pipeline = pipeline |
| 19 | + end |
| 20 | + |
| 21 | + def run |
| 22 | + @return_feeds = [] |
| 23 | + @config['params'].each {|param| |
| 24 | + url = "http://statdb.nstac.go.jp/api/1.0b/app/getStatsData?appId=#{@config['appid']}&#{param}" |
| 25 | + retries = 0 |
| 26 | + begin |
| 27 | + create_rss(URI.encode(url)) |
| 28 | + rescue |
| 29 | + retries += 1 |
| 30 | + Automatic::Log.puts("error", "ErrorCount: #{retries}, Fault in parsing: #{url}") |
| 31 | + sleep @config['interval'].to_i unless @config['interval'].nil? |
| 32 | + retry if retries <= @config['retry'].to_i unless @config['retry'].nil? |
| 33 | + end |
| 34 | + } |
| 35 | + @return_feeds |
| 36 | + end |
| 37 | + |
| 38 | + private |
| 39 | + def create_rss(url) |
| 40 | + Automatic::Log.puts("info", "Parsing: #{url}") |
| 41 | + hash = Hash.from_xml open(url).read |
| 42 | + json = hash.to_json |
| 43 | + data = ActiveSupport::JSON.decode(json) |
| 44 | + unless data.nil? |
| 45 | + rss = create(data, url) |
| 46 | + sleep @config['interval'].to_i unless @config['interval'].nil? |
| 47 | + @return_feeds << rss |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + def create(data, url) |
| 52 | + RSS::Maker.make("2.0") {|maker| |
| 53 | + xss = maker.xml_stylesheets.new_xml_stylesheet |
| 54 | + maker.channel.title = "Automatic Ruby" |
| 55 | + maker.channel.description = "Automatic Ruby" |
| 56 | + maker.channel.link = "https://github.com/automaticruby/automaticruby" |
| 57 | + maker.items.do_sort = true |
| 58 | + item = maker.items.new_item |
| 59 | + item.title = "StatDB Feed by Automatic Ruby" |
| 60 | + item.link = url |
| 61 | + item.content_encoded = data |
| 62 | + item.date = Time.now |
| 63 | + } |
| 64 | + end |
| 65 | + end |
| 66 | +end |
0 commit comments