-
Notifications
You must be signed in to change notification settings - Fork 20
RSS to JSON converter #9
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
Open
Nailok
wants to merge
6
commits into
ulstu:master
Choose a base branch
from
Nailok:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d859da7
Added converter.rb
Nailok 08dd9b3
Merge remote-tracking branch 'upstream/master'
Nailok c9cc495
Reworked program structure
Nailok 7885fbd
Delete .gitignore
Nailok fa405b5
Merge remote-tracking branch 'upstream/master'
Nailok be38ac5
Reworked how parsers and converters work
Nailok File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| *.json | ||
| *.rss | ||
| *.atom | ||
| *.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| В данном репозитории находится конвертер из RSS формата в форматы ATOM, JSON и наоборот. | ||
|
|
||
| Для использования программы используются следующие обязательные аттрибуты: | ||
| -i --input=FILE //Входной файл | ||
| -o --output=FORMAT //Выходной формат | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'optparse' | ||
|
|
||
| class Parser | ||
| @options = {} | ||
| def self.parse() | ||
| OptionParser.new do |parser| | ||
| parser.on('-i', '--input=FILE', | ||
| 'File to convert') | ||
|
|
||
| parser.on('-o', '--output=FORMAT', | ||
| 'Choose one of the formats: json, atom, rss') | ||
|
|
||
| parser.on('-h', '--help', 'Prints this help') do | ||
| puts parser | ||
| exit | ||
| end | ||
| end.parse!(into: @options) | ||
| @options | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| require 'require_all' | ||
| require '~/converter/main.rb' | ||
| require 'nokogiri' | ||
| require 'feedjira' | ||
| require 'json' | ||
|
|
||
| require_all 'lib' | ||
| require_all 'bin' | ||
|
|
||
| options = Parser.parse | ||
| p options | ||
| main = Main.new(options) | ||
| main.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Check | ||
| def self.valid_json?(json) | ||
| JSON.parse(json) | ||
| true | ||
| rescue JSON::ParserError => e | ||
| false | ||
| end | ||
|
|
||
| def self.check(data) | ||
| str = data.to_s | ||
| if str.include? '<feed' | ||
| 'atom' | ||
| elsif str.include? '<rss' | ||
| 'rss' | ||
| elsif valid_json?(str) | ||
| 'json' | ||
| end | ||
| end | ||
| end |
Empty file.
5 changes: 5 additions & 0 deletions
5
lesson04/homework/Converter/lib/converters/convert_to_json.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| module ConvertToJson | ||
| def self.convert(data) | ||
| File.open('output.json', 'w+') { |f| f.write JSON.pretty_generate(data) } | ||
| end | ||
| end |
Empty file.
Binary file not shown.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module ParseAtom | ||
| def self.parseFeed(doc) | ||
| parsedString = [] | ||
| feed_title = doc.at_css('feed title').text.strip | ||
| feed_updated = doc.at_css('feed updated').text.strip | ||
| feed_author = doc.at_css('feed author name').text.strip | ||
| feed_id = doc.at_css('feed id').text.strip | ||
|
|
||
| parsedString.push( | ||
| title: feed_title, | ||
| link: feed_link, | ||
| updated: feed_updated, | ||
| author: feed_author, | ||
| id: feed_id | ||
| ) | ||
| end | ||
|
|
||
| def self.parseEntry(doc) | ||
| items = [] | ||
|
|
||
| doc.css('entry').each do |item| | ||
| entry_title = doc.at_css('entry title').text.strip | ||
| entry_id = doc.at_css('entry id') .text.strip | ||
| entry_updated = doc.at_css('entry updated').text.strip | ||
| entry_summary = doc.at_css('entry summary').text.strip | ||
|
|
||
| items.push( | ||
| title: entry_title, | ||
| id: entry_id, | ||
| updated: entry_updated, | ||
| summary: entry_summary | ||
| ) | ||
|
|
||
| end | ||
|
|
||
| items | ||
| end | ||
|
|
||
| def self.parse(doc) | ||
| doc = Nokogiri::XML(doc) | ||
| result = { feed: parseFeed(doc), | ||
| entry: parseEntry(doc) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| module ParseJson | ||
| def self.parse(input) | ||
| data = JSON.parse(input) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'nokogiri' | ||
|
|
||
| module ParseRss | ||
| def self.parseFeed(doc) | ||
| parsedString = [] | ||
| feed_image_content = {} | ||
| feed_language = doc.css('channel language').text.strip | ||
| feed_title = doc.at_css('channel title').text | ||
| feed_description = doc.at_css('channel description').text.strip | ||
| feed_link = doc.at_css('channel link').text.strip | ||
| feed_image_content = { 'url' => doc.css('channel image url').text.strip, | ||
| 'title' => doc.css('channel image title').text.strip, | ||
| 'link' => doc.css('channel image link').text.strip } | ||
|
|
||
| parsedString.push( | ||
| language: feed_language, | ||
| title: feed_title, | ||
| description: feed_description, | ||
| link: feed_link, | ||
| image: feed_image_content | ||
| ) | ||
| end | ||
|
|
||
| def self.parseItems(doc) | ||
| items = [] | ||
|
|
||
| item_tags = doc.xpath('//item/*').map(&:name).each.uniq | ||
| p item_tags | ||
| doc.css('item').each do |item| | ||
| parsed_strings = {} | ||
| attr_names = [] | ||
| item_tags.each do |tag| | ||
| attr_hash = {} | ||
| if !doc.xpath("//item/#{tag}[@*]").empty? # if tag has attribute parse them | ||
| attr_names = doc.xpath("//item/#{tag}/@*").map(&:name).each.uniq | ||
| attr_names.each do |att| | ||
| attr_hash[att] = item.css(tag)[0][att] | ||
| end | ||
| parsed_strings[tag] = attr_hash | ||
| else | ||
| item_result = item.css(tag.to_s).text.strip | ||
| parsed_strings[tag] = item_result | ||
| end | ||
| end | ||
| items.push << parsed_strings | ||
| end | ||
| items | ||
| end | ||
|
|
||
| def self.parse(doc) | ||
| doc = Nokogiri::XML(doc) | ||
| result = { feed: parseFeed(doc), | ||
| items: parseItems(doc) } | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| module File_reader | ||
| def self.read(path) | ||
| file = File.open(path) | ||
| data = file.read | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require 'open-uri' | ||
|
|
||
| module Link_reader | ||
| def self.read(url) | ||
| data = open(url).read | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| class Main | ||
| def initialize(options) | ||
| @input = options[:input] | ||
| @output_format = options[:output] | ||
| @sort = options[:sort] | ||
| end | ||
|
|
||
| def run | ||
| data = if @input.match('^https|http') | ||
| Link_reader.read(@input) | ||
| else | ||
| File_reader.read(@input) | ||
| end | ||
|
|
||
| parsed_data = if Check.check(data) == 'atom' | ||
| ParseAtom.parse(data) | ||
| elsif Check.check(data) == 'rss' | ||
| ParseRss.parse(data) | ||
| elsif Check.check(data) == 'json' | ||
| ParseJson.parse(data) | ||
| else | ||
| p 'ERROR' | ||
| end | ||
|
|
||
| if @output.to_s.downcase == 'atom' | ||
| ConvertToAtom.convert(parsed_data) | ||
| elsif @output.to_s.downcase == 'rss' | ||
| ConvertToRss.convert(parsed_data) | ||
| else | ||
| ConvertToJson.convert(parsed_data) | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| *.json | ||
| *.txt | ||
| *.swp | ||
| *.yml | ||
| *.atom | ||
| *.rss |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| В данном репозитории находится конвертер из RSS формата в форматы ATOM, JSON и наоборот. | ||
|
|
||
| Для использования программы используются следующие обязательные аттрибуты: | ||
| -i --input=FILE //Входной файл | ||
| -o --output=FORMAT //Выходной формат | ||
|
|
||
| Необязательный аттрибут для названия выходного файла: | ||
| -n --name=FILE //Имя выходного файла | ||
|
|
||
| При отсутствии данного аттрибута, данные запишутся в файл output.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'optparse' | ||
|
|
||
| # parse options | ||
| class OptionParser | ||
| @options = {} | ||
| def self.parse | ||
| OptionParser.new do |parser| | ||
| parser.on('-i', '--input=FILE', | ||
| 'File to convert') | ||
|
|
||
| parser.on('-o', '--output=FORMAT', | ||
| 'Choose one of the formats: json, atom, rss') | ||
| parser.on('-n', '--name=FILE', | ||
| 'Type converted file name') | ||
| parser.on('-h', '--help', 'Prints this help') do | ||
| exit | ||
| end | ||
| end.parse!(into: @options) | ||
| @options | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'require_all' | ||
| require '~/converter/main.rb' | ||
|
|
||
| require_all 'lib' | ||
| require_all 'bin' | ||
|
|
||
| options = OptionParser.parse | ||
| main = Main.new(options) | ||
| main.run |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Check if input data matches one of the three formats: Atom, RSS, JSON | ||
| module Check | ||
| def self.valid_json?(json) | ||
| JSON.parse(json) | ||
| true | ||
| rescue JSON::ParserError => e | ||
| false | ||
| end | ||
|
|
||
| def self.check(data) | ||
| str = data.to_s | ||
| if str.include? '<feed' | ||
| 'atom' | ||
| elsif str.include? '<rss' | ||
| 'rss' | ||
| elsif valid_json?(str) | ||
| 'json' | ||
| else 'error' | ||
| end | ||
| end | ||
| end |
14 changes: 14 additions & 0 deletions
14
lesson04/homework/converter/lib/converters/base_converter.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module BaseConverter | ||
| def self.convert(data, option) | ||
| converters = ClassList.names('lib/converters/modules/*rb') | ||
| converters.each do |converter| | ||
| class_name = Module.const_get(converter) | ||
| # puts class_name.to_s.include?(option.capitalize) | ||
| if class_name.to_s.include?(option.capitalize) | ||
| return class_name.convert(data) | ||
| end | ||
| end | ||
| end | ||
| end |
21 changes: 21 additions & 0 deletions
21
lesson04/homework/converter/lib/converters/modules/convert_to_atom.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'nokogiri' | ||
|
|
||
| # Class which converts hash data to RSS format | ||
| module ConvertToAtom | ||
| def self.convert(data) | ||
| builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml| | ||
| xml.atom('version' => '2.0', 'xmlns:atom' => 'http://www.w3.org/2005/Atom') do | ||
| data.values[0].each do |v| | ||
| xml.entry do | ||
| xml.title v[:title] | ||
| xml.summary v[:description] | ||
| xml.published v[:pubdate] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| builder.to_xml | ||
| end | ||
| end |
10 changes: 10 additions & 0 deletions
10
lesson04/homework/converter/lib/converters/modules/convert_to_json.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'json' | ||
|
|
||
| # Class which converts hash data to Json format | ||
| module ConvertToJson | ||
| def self.convert(data) | ||
| JSON.pretty_generate(data) | ||
| end | ||
| end |
22 changes: 22 additions & 0 deletions
22
lesson04/homework/converter/lib/converters/modules/convert_to_rss.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'nokogiri' | ||
|
|
||
| # Class which converts hash data to RSS format | ||
|
|
||
| module ConvertToRss | ||
| 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 | ||
| data.values[0].each do |v| | ||
| xml.item do | ||
| xml.title v[:title] | ||
| xml.description v[:description] | ||
| xml.pubDate v[:pubdate] | ||
| end | ||
| end | ||
| end | ||
| end | ||
| builder.to_xml | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
FileReader