コード例 #1
0
        public async Task FindPhrase()
        {
            IScraper scraper = new OpenGov.Scrapers.OpenGov("STAVANGER");

            var meetings = await scraper.FindMeetings("sykkel", new HashSet <string>());

            Assert.IsNotNull(meetings);

            var meetingsList = new List <Meeting>(meetings);

            Assert.IsTrue(meetingsList.Count > 0);

            var meeting = meetingsList[0];

            var documents = new List <Document>(await scraper.GetDocuments(meeting));

            Assert.IsTrue(documents.Count > 0);
        }
コード例 #2
0
        static async Task Main(string[] args)
        {
            var config = JsonConvert.DeserializeObject <Config>(await File.ReadAllTextAsync("config.json"));

            foreach (Search search in config.Searches)
            {
                var clientIds = search.ClientId.Split(',');
                var clients   = config.Clients.Where(c => search.ClientId == "*" || c.Id == search.ClientId || clientIds.Contains(c.Id));

                if (!clients.Any())
                {
                    Console.WriteLine(search.ClientId + " is invalid client for search " + search.Name);
                    continue;
                }

                foreach (var client in clients)
                {
                    List <string> loadedMeetings = new List <string>();

                    if (!Directory.Exists(Path.Combine(client.Name, search.Phrase)))
                    {
                        Directory.CreateDirectory(Path.Combine(client.Name, search.Phrase));
                    }

                    string seenMeetingsPath = Path.Combine(client.Name, search.Phrase, "meetings.txt");

                    if (File.Exists(seenMeetingsPath))
                    {
                        loadedMeetings = (await File.ReadAllTextAsync(seenMeetingsPath)).Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries).ToList();
                    }

                    HashSet <string> seenMeetings = new HashSet <string>(loadedMeetings);

                    IScraper scraper = null;

                    if (!string.IsNullOrEmpty(client.OpenGovId))
                    {
                        scraper = new OpenGov.Scrapers.OpenGov(client.OpenGovId);
                    }
                    else if (!string.IsNullOrEmpty(client.JupiterUrl))
                    {
                        scraper = new Jupiter(client.JupiterUrl);
                    }
                    else if (!string.IsNullOrEmpty(client.SRUUrl))
                    {
                        scraper = new SRU(new Uri(client.SRUUrl));
                    }
                    else if (!string.IsNullOrEmpty(client.ACOSUrl))
                    {
                        scraper = new ACOS(new Uri(client.ACOSUrl));
                    }
                    else if (!string.IsNullOrEmpty(client.ElementsUrl))
                    {
                        scraper = new Elements(new Uri(client.ElementsUrl));
                    }

                    try
                    {
                        IEnumerable <Meeting> newMeetings = await scraper.FindMeetings(search.Phrase.ToLower(), seenMeetings);

                        if (newMeetings.Any())
                        {
                            seenMeetings.UnionWith(newMeetings.Select(meeting => meeting.Url.ToString()));

                            MailMessage email = new MailMessage(config.Smtp.Sender, client.NotifyEmail);
                            email.Subject = "Nye møter for " + search.Name + " i " + client.Name;

                            StringBuilder body = new StringBuilder();
                            body.AppendFormat("<h3>Nye møter har dukket opp på kalenderen for {0} i {1}:</h3>\r\n\r\n<table>", search.Name, client.Name);

                            foreach (var meeting in newMeetings.OrderByDescending(m => m.Date))
                            {
                                body.AppendFormat("<tr><td><a href=\"{1}\">{2}</a></td><td><a href=\"{1}\">{0}</a></td><td><a href=\"{1}\">{3}</a></td></tr>\r\n", meeting.BoardName, meeting.Url, meeting.Date.ToString("dd.MM.yyyy"), meeting.Title);
                            }

                            body.Append("</table>");

                            email.Body                 = body.ToString();
                            email.IsBodyHtml           = true;
                            email.BodyEncoding         = Encoding.UTF8;
                            email.BodyTransferEncoding = TransferEncoding.Base64;

                            SmtpClient smtp = new SmtpClient(config.Smtp.Server, config.Smtp.Port);
                            smtp.UseDefaultCredentials = false;
                            smtp.Credentials           = new NetworkCredential(config.Smtp.Sender, config.Smtp.Password);
                            smtp.EnableSsl             = true;

                            NEVER_EAT_POISON_Disable_CertificateValidation();

                            await smtp.SendMailAsync(email);

                            using (var file = new StreamWriter(new FileStream(seenMeetingsPath, FileMode.Create, FileAccess.Write)))
                            {
                                foreach (string meetingUrl in seenMeetings)
                                {
                                    await file.WriteLineAsync(meetingUrl);
                                }
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }