-
Notifications
You must be signed in to change notification settings - Fork 20
Convert rss to json #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
7c5f99b
21e1985
1bcca8d
608d283
5fbc5aa
2d9e91e
89566e2
61992c9
0dc6212
129009c
19c30e0
4b73393
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| *.json | ||
| *.rss | ||
| *.atom | ||
| *.txt |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require './options.rb' | ||
| require '../main.rb' | ||
|
|
||
| options = OptionsParser.options | ||
|
|
||
| main_programm = Main.new(options) | ||
| main_programm.run |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'optparse' | ||
| require 'ostruct' | ||
| require 'uri' | ||
|
|
||
| module OptionsParser | ||
| def self.options | ||
| options = {} | ||
| parser = OptionParser.new do |opt| | ||
| opt.banner = 'Usage converter.rb [options]' | ||
| opt.separator '' | ||
| opt.on('-i', '--input [url | file]', 'The input link | file') do |input| | ||
| options[:input] = input | ||
| end | ||
| opt.on('-o', '--output-format [format]', | ||
| 'The output format(rss | json | atom)') do |output| | ||
| options[:output_format] = output | ||
| end | ||
| opt.on('-s', '--sort [asc | desc]', 'Sorting method') do |sort| | ||
| options[:sort] = sort | ||
| end | ||
|
|
||
| opt.separator '' | ||
|
|
||
| opt.on('-h', '--help', 'Show this message') do |_help| | ||
| puts opt | ||
| exit | ||
| end | ||
| end | ||
|
|
||
| parser.parse! | ||
| options | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| module BaseConverter | ||
| def self.convert(data, option) | ||
| Main::CONVERTES.each do |parser_name| | ||
| class_name = Module.const_get(parser_name) | ||
| return class_name.convert(data) if class_name.to_s.include?(option.to_s.capitalize) | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'nokogiri' | ||
|
|
||
| # Class which converts hash data to RSS format | ||
| module AtomConvert | ||
| def self.convert(data) | ||
| builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| | ||
| xml.rss('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom') do | ||
| xml.title data[:feed][:title] | ||
| xml.link data[:feed][:link] | ||
| data[:items].each do |v| | ||
| xml.entry do | ||
| xml.title v[:title] | ||
| xml.summary v[:description] | ||
| xml.link v[:link] | ||
| xml.published v[:date] | ||
| xml.category v[:category] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| builder.to_xml | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module JsonConvert | ||
| def self.convert(data) | ||
| JSON.pretty_generate(data) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module RssConvert | ||
| def self.convert(data) | ||
| builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| | ||
| xml.rss('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom') do | ||
| xml.title data[:feed][:title] | ||
| xml.link data[:feed][:link] | ||
| data[:items].each do |v| | ||
| xml.item do | ||
| xml.title v[:title] | ||
| xml.description v[:description] | ||
| xml.link v[:link] | ||
| xml.published v[:date] | ||
| xml.category v[:category] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| builder.to_xml | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module BaseParsers | ||
| def self.parse(data) | ||
| Main::PARSERS.each do |parser_name| | ||
| class_name = Module.const_get(parser_name) | ||
| return class_name.parse(data) if class_name.can_parse?(data) | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| # frozen_string_literal: true | ||
| require 'nokogiri' | ||
|
|
||
| module AtomParser | ||
| def self.parse(data) | ||
| doc = Nokogiri::XML(data) | ||
| {feed: parse_feed(doc), | ||
| items: parse_items(doc)} | ||
| end | ||
|
|
||
| def self.parse_feed(doc) | ||
| { | ||
| title: doc.at_css('feed title').text.strip, | ||
| link: doc.at_css('feed link').text.strip | ||
| } | ||
| end | ||
|
|
||
| def self.parse_items(doc) | ||
| arr = [] | ||
| doc.remove_namespaces! | ||
| doc = doc.xpath('//entry') | ||
| doc.each do |item| | ||
| hash = { | ||
| title: item.at('title').text, | ||
| description: item.at('summary').text, | ||
| link: item.at('link').text, | ||
| date: item.at('published').text, | ||
| category: item.at('category').text | ||
| } | ||
| arr << hash | ||
| end | ||
| arr | ||
| end | ||
|
|
||
| def self.can_parse?(data) | ||
| data.include?('<feed') | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module JsonParser | ||
| def self.parse(data) | ||
| res = JSON.parse(data) | ||
| end | ||
|
|
||
| def self.can_parse?(data) | ||
| JSON.parse(data) | ||
| true | ||
| rescue JSON::ParserError => e | ||
| false | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'nokogiri' | ||
|
|
||
| module RssParser | ||
| def self.parse(data) | ||
| doc = Nokogiri::XML(data) | ||
|
|
||
| {feed: parse_feed(doc), | ||
| items: parse_items(doc) } | ||
| end | ||
|
|
||
| def self.parse_feed(doc) | ||
| { | ||
| title: doc.at_css('channel title').text.strip, | ||
| link: doc.at_css('channel link').text.strip | ||
| } | ||
| end | ||
|
|
||
| def self.parse_items(doc) | ||
| doc = doc.xpath('//item') | ||
| arr = [] | ||
| doc.each do |item| | ||
| hash = { | ||
| title: item.at('title').text, | ||
| description: item.at('description').text, | ||
| link: item.at('link').text, | ||
| date: item.at('pubDate').text, | ||
| category: item.at('category').text | ||
| } | ||
| arr << hash | ||
| end | ||
| arr | ||
| end | ||
|
|
||
|
|
||
| def self.can_parse?(data) | ||
| data.include?('<channel>') | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module FileReader | ||
| def self.read(input) | ||
| File.read(input) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'open-uri' | ||
|
|
||
| module LinkReader | ||
| def self.read(input) | ||
| page = open(input) | ||
| page.read | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module AscSorter | ||
| def self.sort(data) | ||
| data.sort_by { |h| h[:pubDate].split(/\/|:|-|T|Z/).reverse } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module DescSorter | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Не доделал (
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. да, AscSorter тоже не работает. Возник вопрос сортировка должна быть только по дате?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Да, только по дате.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. хорошо, спасибо |
||
|
|
||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| module FileWriter | ||
| def self.write(data, link) | ||
| file_name = link.split('/') | ||
| file_name = file_name[-1].split('.') | ||
| File.write("../#{file_name[0]}", data) | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'require_all' | ||
| require 'uri' | ||
| require_all '../lib/**/*.rb' | ||
|
|
||
| class Main | ||
| PARSERS = %w[JsonParser RssParser AtomParser].freeze | ||
| CONVERTES = %w[JsonConvert RssConvert AtomConvert].freeze | ||
|
|
||
| def initialize(options) | ||
| @input = options[:input] | ||
| @output_format = options[:output_format] | ||
| @sort = options[:sort] | ||
| end | ||
|
|
||
| def run | ||
| data = if @input =~ URI::DEFAULT_PARSER.make_regexp | ||
| LinkReader.read(@input) | ||
| elsif File.file?(@input) | ||
| FileReader.read(@input) | ||
| else | ||
| puts 'ERROR: Not found file' | ||
| exit | ||
| end | ||
| parsed_data = BaseParsers.parse(data) | ||
| converted_data = BaseConverter.convert(parsed_data, @output_format) | ||
|
|
||
| FileWriter.write(converted_data, @input) | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BaseParser - в единственном числе.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
хорошо, исправлю