Skip to content
This repository has been archived by the owner on Mar 3, 2023. It is now read-only.

Commit

Permalink
Added new commands, implemented CLI RSS Feed Display Manager, Minor b…
Browse files Browse the repository at this point in the history
…ug fixes
  • Loading branch information
dimankiev committed Jul 28, 2021
1 parent 38362fd commit de1429e
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 18 deletions.
59 changes: 51 additions & 8 deletions RSSReader/Program.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using RSSReader.core;
using RSSReader.cli;

namespace RSSReader
{
Expand All @@ -11,12 +12,12 @@ class Program
// First array is for checking that entered command actually exists
new List<string>() {"add", "read", "edit", "remove", "help", "exit"},
// Next arrays are for commands description. Used in "help" command
new List<string>() {"add [feed name]", "Add new RSS Feed to the app. Name is required"},
new List<string>() {"read [feed name]", "Read one RSS Feed. Leave name empty to read all feeds."},
new List<string>() {"edit [feed name]", "Edit a RSS Feed in your config."},
new List<string>() {"remove [feed name]", "Remove one RSS Feed from the app."},
new List<string>() {"help", "Display available commands."},
new List<string>() {"exit", "Save the configuration and exit the program."}
new List<string>() {"add [feed name]\t\t", "\tAdd new RSS Feed to the app. Name is required"},
new List<string>() {"read [feed name]\t", "\tRead one RSS Feed. Leave name empty to read all feeds."},
new List<string>() {"edit [feed name]\t", "\tEdit a RSS Feed in your config."},
new List<string>() {"remove [feed name]\t", "\tRemove one RSS Feed from the app."},
new List<string>() {"help\t\t\t", "\tDisplay available commands."},
new List<string>() {"exit\t\t\t", "\tSave the configuration and exit the program."}
};

private static void GetHelp(string command)
Expand All @@ -32,10 +33,10 @@ private static void GetHelp()
}
static void Main(string[] args)
{
Console.WriteLine("RSS Feed Reader v1.1.2");
Console.WriteLine("RSS Feed Reader v1.2.3 by dimankiev");
Console.WriteLine("Enter 'help' command to get help\n");
ConfigManager config = new ConfigManager();
RssManager manager = new RssManager();
RSSManager manager = new RSSManager();
while (true)
{
Console.Write("RSSReader >> ");
Expand Down Expand Up @@ -100,6 +101,48 @@ static void Main(string[] args)
Console.WriteLine($"Editing name: {newNameSetResult}\nEditing URL: {newUrlSetResult}");
}
}
else if (parsedCommand[0] == "read")
{
try
{
DisplayManager dm;
if (parsedCommand.Length < 2)
{
var allFeeds = config.GetAllFeeds();

if (allFeeds.Count < 1)
{ Console.WriteLine("There is no feeds added!"); continue; }

var fetchedFeeds = manager.FetchMany(allFeeds);
dm = new DisplayManager(fetchedFeeds);
dm.Display();
}
else
{
if (!config.FeedExists(parsedCommand[1]))
{
Console.WriteLine("Feed does not exist!");
continue;
}

var feedUrl = config.GetFeedLink(parsedCommand[1]);
var fetchedFeed = manager.Fetch(parsedCommand[1], feedUrl);
dm = new DisplayManager(fetchedFeed);
dm.Display();
}
}
catch (Exception error)
{
Console.Clear();
Console.WriteLine($"Following error has been occured while reading feed:\n{error}");
}
}
else if (parsedCommand[0] == "help")
{
Console.WriteLine("Help for RSSReader");
for (int i = 1; i < commands.Count; i++)
Console.WriteLine($"{commands[i][0]}-{commands[i][1]}");
}
else if (parsedCommand[0] == "exit")
{
Console.WriteLine("Config is saved!\nBye-bye!");
Expand Down
91 changes: 91 additions & 0 deletions RSSReader/cli/display.manager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel.Syndication;
using RSSReader.core;

namespace RSSReader.cli
{
public class DisplayManager
{
private bool isSingle { get; set; }
private RSSReadResult currentFeed { get; set; }
private Dictionary<string, RSSReadResult> allFeeds { get; set; }
private List<string> allFeedsNames { get; set; }
public DisplayManager(RSSReadResult feed)
{
isSingle = true;
currentFeed = feed;
}

public DisplayManager(Dictionary<string, RSSReadResult> feeds)
{
isSingle = false;
allFeeds = feeds;
allFeedsNames = new List<string>(feeds.Keys);
}

private void SetCurrentFeed()
{
if (isSingle) return;
currentFeed = allFeeds.First().Value;
}

public void Display()
{
if (!isSingle) SetCurrentFeed();
int articleNo = 0;
int feedNo = 0;
List<SyndicationItem> articles = new List<SyndicationItem>(currentFeed.Feed.Items);
while (true)
{
Console.Clear();
Console.WriteLine("Up/Down Arrow Keys to navigate through feed articles list");
if (!isSingle) Console.WriteLine("Left/Right Arrow Keys to navigate through feeds list");

Console.WriteLine("Press Q to quit feed viewer...\n\n");
Console.WriteLine($"Current feed: {currentFeed.Name} ({currentFeed.Feed.Title.Text.Trim()})");
Console.WriteLine($"Description:\n{currentFeed.Feed.Description.Text.Trim()}\n");
if (articles.Count > 0)
{
Console.WriteLine($"Article No. {articleNo + 1} of {articles.Count}:");
Console.WriteLine($"{articles[articleNo].Title.Text.Trim()}\n");
Console.WriteLine($"Date: {articles[articleNo].PublishDate.ToString()}");
Console.WriteLine($"\n{articles[articleNo].Summary.Text.Trim()}\n");
new List<SyndicationLink>(articles[articleNo].Links).ForEach(link =>
{
Console.WriteLine($"{link.Uri.ToString().Trim()}");
});
}
else
{
Console.WriteLine("No articles found!");
}

ConsoleKeyInfo pressed = Console.ReadKey();
if (pressed.Key == ConsoleKey.UpArrow && articleNo + 1 < articles.Count)
articleNo += 1;
if (pressed.Key == ConsoleKey.DownArrow && articleNo - 1 >= 0)
articleNo -= 1;
if (!isSingle)
{
int previousFeedNo = feedNo;
if (pressed.Key == ConsoleKey.LeftArrow && !isSingle && feedNo - 1 >= 0)
feedNo -= 1;
if (pressed.Key == ConsoleKey.RightArrow && !isSingle && feedNo + 1 < allFeedsNames.Count)
feedNo += 1;
if (previousFeedNo != feedNo)
{
currentFeed = allFeeds[allFeedsNames[feedNo]];
articles = new List<SyndicationItem>(currentFeed.Feed.Items);
articleNo = 0;
}
}

if (pressed.Key == ConsoleKey.Q)
break;
}
Console.Clear();
}
}
}
35 changes: 26 additions & 9 deletions RSSReader/core/rss.manager.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.ServiceModel.Syndication;
using System.Threading.Tasks;
using System.Xml;

namespace RSSReader.core
{
public class RSSReadResult
{
public RSSReadResult(string name, string url)
{ Name = name; Url = url; }
public bool Success { get; set; }
public string Name { get; set; }
public string Url { get; set; }
public SyndicationFeed Feed { get; set; }
public Exception Message { get; set; }
public void StatusSet(bool isSuccess, Exception message)
{
Success = isSuccess;
Message = message;
}
{ Success = isSuccess; Message = message; }
}

public class RssManager
public class RSSManager
{
public RssManager() {}
public RSSManager() {}

public RSSReadResult Fetch(string url)
public RSSReadResult Fetch(string feedName, string feedUrl)
{
RSSReadResult result = new RSSReadResult();
RSSReadResult result = new RSSReadResult(feedName, feedUrl);
try
{
using (var reader = XmlReader.Create(url)) result.Feed = SyndicationFeed.Load(reader);
using (var reader = XmlReader.Create(feedUrl)) result.Feed = SyndicationFeed.Load(reader);
result.StatusSet(true, null);
}
catch (Exception err)
Expand All @@ -36,5 +40,18 @@ public RSSReadResult Fetch(string url)

return result;
}

public Dictionary<string, RSSReadResult> FetchMany(List<string[]> feedParams)
{
Dictionary<string, RSSReadResult> fetched = new Dictionary<string, RSSReadResult>();
Parallel.ForEach(feedParams, currentFeed =>
{
RSSReadResult feed = Fetch(currentFeed[0], currentFeed[1]);
if (!feed.Success)
{ throw new Exception($"{feed.Name} ({feed.Url}) {feed.Message}"); }
fetched.Add(currentFeed[0], feed);
});
return fetched;
}
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ab0d11c216b2f3958aa1c365ab673d66b63f796e
6f479842cd559e7a61dc2abef8b7ef8b627d38a5
Binary file modified RSSReader/obj/Debug/net5.0/RSSReader.dll
Binary file not shown.
Binary file modified RSSReader/obj/Debug/net5.0/RSSReader.pdb
Binary file not shown.
Binary file modified RSSReader/obj/Debug/net5.0/ref/RSSReader.dll
Binary file not shown.

0 comments on commit de1429e

Please sign in to comment.