private static async Task <int> run() { TomlSettings parseSettings = TomlSettings.Create(s => s.ConfigurePropertyMapping(m => m.UseTargetPropertySelector(new SnakeCasePropertySelector())) ); FeedSource feedSource = Toml.ReadFile <FeedSource>(settings.ConfigFilepath, parseSettings); if (feedSource == null) { Console.Error.WriteLine("Error: Somethine went wrong when parsing your settings file :-("); return(1); } if (!string.IsNullOrWhiteSpace(feedSource.Feed.Output)) { settings.OutputFilepath = feedSource.Feed.Output; } FeedBuilder feedBuilder = new FeedBuilder(); try { await feedBuilder.AddSource(feedSource); } catch (ApplicationException error) { Console.Error.WriteLine(error.Message); return(2); } await Console.Error.WriteLineAsync($"[Output] Writing feed to {settings.OutputFilepath}"); File.WriteAllText(settings.OutputFilepath, await feedBuilder.Render()); return(0); }
public async Task AddSource(FeedSource source) { await Console.Error.WriteLineAsync("[Builder] Downloading content"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(source.Feed.Url); request.UserAgent = UserAgentHelper.UserAgent; WebResponse response = await request.GetResponseAsync(); await Console.Error.WriteLineAsync("[Builder] Generating feed header"); // Write the header await feed.WriteGenerator("Polyfeed", "https://github.com/sbrl/PolyFeed.git", Program.GetProgramVersion()); await feed.WriteId(source.Feed.Url); await feed.Write(new SyndicationLink(new Uri(source.Feed.Url), AtomLinkTypes.Self)); string lastModified = response.Headers.Get("last-modified"); if (string.IsNullOrWhiteSpace(lastModified)) { await feed.WriteUpdated(DateTimeOffset.Now); } else { await feed.WriteUpdated(DateTimeOffset.Parse(lastModified)); } string contentType = response.Headers.Get("content-type"); IParserProvider provider = GetProvider(source.Feed.SourceType); if (provider == null) { throw new ApplicationException($"Error: A provider for the source type {source.Feed.SourceType} wasn't found."); } provider.SetOutputFeed(feed, xml); await provider.ParseWebResponse(source, response); await Console.Error.WriteLineAsync("[Builder] Done!"); }