Repository navigation
A simple and quick way to have feed groups in fever clients. #244
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
Changes from all commits
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 |
|---|---|---|
| @@ -1,16 +1,35 @@ | ||
| require_relative "../../models/feed" | ||
| require_relative "../../models/group" | ||
| require_relative "../../utils/opml_parser" | ||
|
|
||
| class ImportFromOpml | ||
| ONE_DAY = 24 * 60 * 60 | ||
|
|
||
| def self.import(opml_contents) | ||
| feeds = OpmlParser.new.parse_feeds(opml_contents) | ||
| class << self | ||
| def import(opml_contents) | ||
| feeds_with_groups = OpmlParser.new.parse_feeds(opml_contents) | ||
|
|
||
| feeds.each do |feed| | ||
| Feed.create(name: feed[:name], | ||
| url: feed[:url], | ||
| last_fetched: Time.now - ONE_DAY) | ||
| # It considers a situation when feeds are already imported without groups, | ||
| # so it's possible to re-import the same subscriptions.xml just to set group_id | ||
| # for existing feeds. Feeds without groups are in 'Ungrouped' group, we don't | ||
| # create such group and create such feeds with group_id = nil. | ||
| # | ||
| feeds_with_groups.each do |group_name, parsed_feeds| | ||
| if parsed_feeds.size > 0 | ||
| group = Group.where(name: group_name).first_or_create unless group_name == 'Ungrouped' | ||
|
|
||
| parsed_feeds.each { |parsed_feed| create_feed(parsed_feed, group) } | ||
| end | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_feed(parsed_feed, group) | ||
| feed = Feed.where(name: parsed_feed[:name], url: parsed_feed[:url]).first_or_initialize | ||
| feed.last_fetched = Time.now - ONE_DAY if feed.new_record? | ||
| feed.group_id = group.id if group | ||
| feed.save | ||
| end | ||
| end | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| class Group < ActiveRecord::Base | ||
| has_many :feeds | ||
|
|
||
| def as_fever_json | ||
| { id: self.id, title: self.name } | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| require_relative "../models/group" | ||
|
|
||
| class GroupRepository | ||
| def self.list | ||
| Group.order('LOWER(name)') | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,23 +4,29 @@ class OpmlParser | |
| def parse_feeds(contents) | ||
| doc = Nokogiri.XML(contents) | ||
|
|
||
| doc.xpath("//body//outline").inject([]) do |feeds, outline| | ||
| next feeds if missing_fields? outline.attributes | ||
| feeds_with_groups = Hash.new { |h,k| h[k] = [] } | ||
|
|
||
| feeds << { | ||
| name: extract_name(outline.attributes).value, | ||
| url: outline.attributes["xmlUrl"].value | ||
| } | ||
| doc.xpath('//body/outline').each do |outline| | ||
|
|
||
| if outline.attributes['xmlUrl'].nil? # it's a group! | ||
| group_name = extract_name(outline.attributes).value | ||
| feeds = outline.xpath('./outline') | ||
| else # it's a top-level feed, which means it's a feed without group | ||
| group_name = 'Ungrouped' | ||
| feeds = [outline] | ||
| end | ||
|
|
||
| feeds.each do |feed| | ||
| feeds_with_groups[group_name] << { name: extract_name(feed.attributes).value, | ||
| url: feed.attributes['xmlUrl'].value } | ||
|
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. If the group names are unique, lines 17-21 could be simplified as follows. feeds_with_groups[group_name] = feeds.map do |feed|
{ name: extract_name(feed.attributes).value, url: feed.attributes['xmlUrl'].value }
end
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. Such assumption makes sense to me, changed code according to your suggestion.
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. Reverted this because when
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. OK, that's unfortunate. Thanks for trying! |
||
| end | ||
| end | ||
| feeds_with_groups | ||
| end | ||
|
|
||
| private | ||
| def missing_fields?(attributes) | ||
| attributes["xmlUrl"].nil? || | ||
| (attributes["title"].nil? && attributes["text"].nil?) | ||
| end | ||
|
|
||
| def extract_name(attributes) | ||
| attributes["title"] || attributes["text"] | ||
| attributes['title'] || attributes['text'] | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| class AddGroupsTableAndForeignKeysToFeeds < ActiveRecord::Migration | ||
| def up | ||
| create_table :groups do |t| | ||
| t.string :name, null: false | ||
| t.timestamps null: false | ||
| end | ||
| add_column :feeds, :group_id, :integer | ||
| end | ||
|
|
||
| def down | ||
| drop_table :groups | ||
| remove_column :feeds, :group_id | ||
| end | ||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| require 'spec_helper' | ||
|
|
||
| app_require "commands/feeds/import_from_opml" | ||
|
|
||
| describe ImportFromOpml do | ||
| let(:subscriptions) { File.open(File.expand_path('../../../support/files/subscriptions.xml', __FILE__)) } | ||
|
|
||
| def import | ||
| described_class.import(subscriptions) | ||
| end | ||
|
|
||
| after do | ||
| Feed.delete_all | ||
| Group.delete_all | ||
| end | ||
|
|
||
| let(:group_1 ) { Group.find_by_name('Football News') } | ||
| let(:group_2 ) { Group.find_by_name('RoR') } | ||
|
|
||
| context 'adding group_id for existing feeds' do | ||
| let!(:feed_1) { Feed.create(name: 'TMW Football Transfer News', | ||
| url: 'http://www.transfermarketweb.com/rss') } | ||
| let!(:feed_2) { Feed.create(name: 'GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS - Home', | ||
| url: 'http://feeds.feedburner.com/GiantRobotsSmashingIntoOtherGiantRobots') } | ||
| before { import } | ||
|
|
||
| it 'retains exising feeds' do | ||
| feed_1.should be_valid | ||
| feed_2.should be_valid | ||
| end | ||
|
|
||
| it 'creates new groups' do | ||
| group_1.should be | ||
| group_2.should be | ||
| end | ||
|
|
||
| it 'sets group_id for existing feeds' do | ||
| feed_1.reload.group.should eq group_1 | ||
| feed_2.reload.group.should eq group_2 | ||
| end | ||
| end | ||
|
|
||
| context 'creates new feeds with groups' do | ||
| let(:feed_1) { Feed.where(name: 'TMW Football Transfer News', | ||
| url: 'http://www.transfermarketweb.com/rss') } | ||
|
|
||
| let(:feed_2) { Feed.where(name: 'GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS - Home', | ||
| url: 'http://feeds.feedburner.com/GiantRobotsSmashingIntoOtherGiantRobots') } | ||
| before { import } | ||
|
|
||
| it 'creates groups' do | ||
| group_1.should be | ||
| group_1.should be | ||
| end | ||
|
|
||
| it 'creates feeds' do | ||
| feed_1.should exist | ||
| feed_2.should exist | ||
| end | ||
|
|
||
| it 'sets group' do | ||
| feed_1.first.group.should eq group_1 | ||
| feed_2.first.group.should eq group_2 | ||
| end | ||
| end | ||
|
|
||
| context 'creates new feeds without group' do | ||
| let(:feed_1) { Feed.where(name: 'Autoblog', url: 'http://feeds.autoblog.com/weblogsinc/autoblog/').first } | ||
| let(:feed_2) { Feed.where(name: 'City Guide News', url: 'http://www.probki.net/news/RSS_news_feed.asp').first } | ||
|
|
||
| before { import } | ||
|
|
||
| it 'does not create any new group for feeds without group' do | ||
| Group.where('id NOT IN (?)', [group_1.id, group_2.id]).count.should eq 0 | ||
| end | ||
|
|
||
| it 'creates feeds without group_id' do | ||
| feed_1.group_id.should be_nil | ||
| feed_2.group_id.should be_nil | ||
| end | ||
| 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.
I'm not sure about these constants. Are they needed?
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.
See the Fever API docs, headings "Groups" and "Write". It turns out these constants are wrong,
KINDLING_GROUP_IDshould be 0 andSPARKS_GROUP_IDshould be -1. Feel free to correct this.As to your question if these constants are needed, I'd say that yes they are. Even though we don't have a notion of sparks for feeds, we should still handle requests using these IDs. My suggestion for handling these requests is to simply mark all feeds as read (which was the previous behavior).
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.
Ok, so I changed constants and left the same behaviour for requests with such group_id.