All pastes #994174 Raw Edit

Anonym

public text v1 · immutable
#994174 ·published 2008-04-23 05:10 UTC
rendered paste body
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Net;
using System.IO;

namespace MensaPlan
{
    class Program
    {

        static void Main(string[] args)
        {
            const string foodExp = "\\<td class=\"borderb\"\\>(?<FOOD>[^\\>]+) &nbsp;\\<";
            const string URL = "http://www.studierendenwerk-koblenz.de/speiseplan.php";
            string page = null;

            try
            {
                page = GetPage(URL);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
                Environment.Exit(1);
            }

            page = page.Replace("ß", "ss");
            page = page.Replace("ä", "ae");
            page = page.Replace("ö", "oe");
            page = page.Replace("ü", "ue");
            page = page.Replace("Ä", "Ae");
            page = page.Replace("Ö", "Oe");
            page = page.Replace("Ü", "Ue");

            //DateTime now = DateTime.Now;

            Regex regex = new Regex(foodExp);
            MatchCollection matches = regex.Matches(page);

            int dow = 0;

            if (DateTime.Now.Hour > 14)
                dow++;

            if ((int)DateTime.Now.DayOfWeek + dow > (int)DayOfWeek.Friday)
            {
                dow = 0;
            }
            else
            {
                dow += (int)DateTime.Now.DayOfWeek - (int)DayOfWeek.Monday;
            }

            if (matches.Count < dow + 6)
            {
                Console.WriteLine("Karpott. Keine Daten.");
                Environment.Exit(1);
            }

            if (matches[dow].Success && matches[dow + 5].Success)
            {
                Console.WriteLine("Ausgabe 1: " + matches[dow].Groups["FOOD"].Value);
                Console.WriteLine("Ausgabe 2: " + matches[dow + 5].Groups["FOOD"].Value);
            }
            else
            {
                Console.WriteLine("Matches karpott. Komisch!");
            }


            //Console.WriteLine(DateTime.Now - now);
            //Console.WriteLine(System.Globalization.DateTimeFormatInfo.CurrentInfo.FirstDayOfWeek.ToString());
            Console.ReadKey();
        }


        public static string GetPage(string url)
        {
            HttpWebRequest webRequest = null;
            HttpWebResponse response = null;
            Stream dataStream = null;

            try
            {
                webRequest = (HttpWebRequest)WebRequest.Create(url);
                response = (HttpWebResponse)webRequest.GetResponse();
                dataStream = response.GetResponseStream();
                using (StreamReader sr = new StreamReader(dataStream, System.Text.Encoding.UTF7))
                {
                    return sr.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                throw;
            }
            finally
            {
                if (dataStream != null)
                    dataStream.Close();
                if (response != null)
                    response.Close();
            }
        }

    }
}