public async Task Process(WallabagClient client, WallabagItem item) { using (var db = new WallabagContext()) { // Filter out HN articles that have already been processed var exists = db.ProcessedHNArticles.Any(a => a.SourceWBId == item.Id || a.ContentWBId == item.Id); if (!exists) { var hn = await Extract_HN_article(client, item); if (hn != null) { db.ProcessedHNArticles.Add(hn); var count = await db.SaveChangesAsync(); Console.WriteLine("{0} HN records saved to database", count); } } } }
static int Main(string[] args) { var appConfig = new AppConfig { url = Environment.GetEnvironmentVariable("WALLABAG_URL"), client_id = Environment.GetEnvironmentVariable("WALLABAG_CLIENT_ID"), client_secret = Environment.GetEnvironmentVariable("WALLABAG_CLIENT_SECRET"), username = Environment.GetEnvironmentVariable("WALLABAG_USERNAME"), password = Environment.GetEnvironmentVariable("WALLABAG_PASSWORD"), poll_duration = int.Parse(Environment.GetEnvironmentVariable("WALLABAG_POLL_DURATION_SECONDS") ?? "60") * 1000, data_dir = Environment.GetEnvironmentVariable("DATA_DIR") ?? "/config", database_file = "wallabag_reducer.sqlite3", config_file = "config.json" }; WallabagContext.database_file = Path.Combine(appConfig.data_dir, appConfig.database_file); var config_path = Path.Combine(appConfig.data_dir, appConfig.config_file); if (!File.Exists(config_path)) { Console.WriteLine($"No config file, exiting (expected at {config_path}"); return(-1); } try { appConfig.Validate(); } catch (Exception e) { Console.WriteLine($"Invalid environment configuration: {e.Message}"); return(-1); } // Load processor config file var processorConfig = JObject.Parse(File.ReadAllText(config_path)); // Create DB & run any necessary migrations using (var db = new WallabagContext()) { db.Database.Migrate(); } // Create & auth with Wallabag instance WallabagClient client = new WallabagClient(new System.Uri(appConfig.url), appConfig.client_id, appConfig.client_secret); var tok = client.RequestTokenAsync(appConfig.username, appConfig.password); tok.Wait(); // Prep processors var processors = new IProcessor[] { new HNProcessor(), new GenericTagProcessor(processorConfig), new YoutubeDownloader(processorConfig), }; var ex = run(client, processors, appConfig.poll_duration); ex.Wait(); return(0); }