All pastes #993343 Raw Edit

Vermischtes

public text v1 · immutable
#993343 ·published 2008-04-22 15:38 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 = string.Empty;

            page = LoadURL(URL);
            if (page.Equals(string.Empty))
            {
                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 (DateTime.Now.DayOfWeek > DayOfWeek.Friday || (DateTime.Now.DayOfWeek == DayOfWeek.Friday && dow == 1))
            {
                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);
            }


            //Console.WriteLine(DateTime.Now - now);
            //Console.ReadKey();
        }


        public static string LoadURL(string url)
        {
            HttpWebRequest webRequest = null;
            HttpWebResponse response = null;
            Stream dataStream = null;
            StreamReader reader = null;
            string resp = string.Empty;

            try
            {
                webRequest = (HttpWebRequest)WebRequest.Create(url);
                response = (HttpWebResponse)webRequest.GetResponse();
                dataStream = response.GetResponseStream();
                reader = new StreamReader(dataStream, System.Text.Encoding.UTF7);
                resp = reader.ReadToEnd();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception: " + ex.Message);
                Console.WriteLine("URL      : " + url);
                System.Diagnostics.StackTrace st = new System.Diagnostics.StackTrace();
                System.Diagnostics.StackFrame sf = st.GetFrame(1);
                Console.WriteLine("Methode  : " + sf.GetMethod().Name + "()");
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (dataStream != null)
                    dataStream.Close();
                if (response != null)
                    response.Close();
            }

            return resp;
        }
    }
}