static void CollectData(HtmlNode nodeToPrint, PanchangData panchangData, PatternToScan[] scanPatterns) { if (nodeToPrint.HasChildNodes) { foreach (HtmlNode subNode in nodeToPrint.ChildNodes) { CollectData(subNode, panchangData, scanPatterns); } } else { // If we have looked at all the patterns we are interested in skip. if (currentIndex >= scanPatterns.Length) { return; } PatternToScan scanPattern = scanPatterns[currentIndex]; //Console.WriteLine("currentIndex {0}: {1} \n skip {2} pattern {3} fieldType {4}", // currentIndex, nodeToPrint.InnerText, scanPattern._Skip, scanPattern._RegExp, scanPattern._FieldType.ToString()); if (scanPattern._Skip == false) { String input = nodeToPrint.InnerText; String pattern = scanPattern._RegExp; String output; output = null; if (pattern == null) { output = input; } else { Match match = Regex.Match(input, pattern); if (match.Success) { output = match.Groups[1].Value; } } if (output != null) { //Console.WriteLine(output); panchangData._fieldValues[(int)scanPattern._FieldType] = output; currentIndex++; } //Console.WriteLine("========================================================================"); // if we get an invalid pattern. Skip and go to the next node and try for the same match } else { currentIndex++; } } }
static void GetCalendarDataPerCityAndYear(int Year, String City, String UrlToken, TimeZoneValues timeZone) { HtmlAgilityPack.HtmlWeb web = new HtmlWeb(); YearlyPanchangData yearPanchangData = new YearlyPanchangData(); PanchangData[] panchangData = new PanchangData[12 * 31]; yearPanchangData._panchangData = panchangData; int day = 0; String fileName = String.Format("IndianCalendar-{0}-{1}.dat", UrlToken, Year); Console.WriteLine("Filename is {0}", fileName); if (File.Exists(fileName)) { Console.WriteLine(fileName + " Exists"); return; } YearlyPanchangData yearPanchangDataCopy; try { FileStream fs = new FileStream(fileName, FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(YearlyPanchangData)); // Get the calendar data for every month for (int month = 1; month <= 12; month++) { String url = String.Format("http://www.mypanchang.com/phppanchang.php?yr={0}&cityhead=&cityname={2}&monthtype=0&mn={1}", Year, month - 1, UrlToken); HtmlDocument document = null; try { document = web.Load(url); } catch (Exception exp) { Console.WriteLine("Load Failed " + url + "\n" + exp.Message); return; } day = 0; PanchangData pData = null; String log = null; foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table")) { HtmlNodeCollection coll = node.ChildNodes; foreach (HtmlNode data in coll) { log += data.InnerText.Trim(); log += "\n"; log += "+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; log += "\n"; //Console.WriteLine(log); String input = data.InnerText; String pattern1 = @"Panchang for"; Match match = Regex.Match(input, pattern1); if (match.Success) { if (pData != null) { // Console.WriteLine("Year {0} Month {1} Day {2}", Year, month, day); for (int j = 0; j < (int)(FieldType.MaxFields); j++) { if ((j != (int)FieldType.None) && (j != (int)FieldType.TamilYear) && (j != (int)FieldType.Festival)) { if (pData._fieldValues[j] == null) { Console.WriteLine("Field Value {0} is null", ((FieldType)j).ToString()); Console.WriteLine(log); } } //Console.WriteLine("{0}: {1}", (FieldType)j, pData._fieldValues[j]); } // Stash away the old data panchangData[(month - 1) * 31 + (day - 1)] = pData; } ; // A new day got started. pData = new PanchangData(Year, month, day + 1); day++; log = null; //if (day > 30) break; } pattern1 = @"Shalivahan Shaka: (\d\d\d\d) \((\w+) Samvatsara\),\ (\w+) Year \(North India\) (\d\d\d\d),\ (\w+ \w+) \(Gujarat\) (\d\d\d\d),\ Ayana:(\w+) \ Ritu:(\w+), Vedic Ritu:(\w+), Amavasyant (\w+) (\w+) Paksha,\ Tamil Month: (\w+)"; match = Regex.Match(input, pattern1); currentIndex = 0; if (match.Success) { pData._fieldValues[(int)FieldType.TamilYear] = match.Groups[2].Value + " " + match.Groups[1].Value; pData._fieldValues[(int)FieldType.NorthYear] = match.Groups[3].Value + " " + match.Groups[4].Value; pData._fieldValues[(int)FieldType.GujaratYear] = match.Groups[5].Value + " " + match.Groups[6].Value; pData._fieldValues[(int)FieldType.Ayana] = match.Groups[7].Value; pData._fieldValues[(int)FieldType.Ritu] = match.Groups[8].Value; pData._fieldValues[(int)FieldType.VedicRitu] = match.Groups[9].Value; pData._fieldValues[(int)FieldType.SanskritMonth] = match.Groups[10].Value; pData._fieldValues[(int)FieldType.Paksha] = match.Groups[11].Value; pData._fieldValues[(int)FieldType.TamilMonth] = match.Groups[12].Value; pData._fieldValues[(int)FieldType.Festival] = null; } // @"Sunrise:07:36:10Sunset:17:08:38Moonrise:23:54:46"; String pattern2 = @"Sunrise:([\d:]+)Sunset:([\d:]+)Moonrise:([\d:]+)"; match = Regex.Match(input, pattern2); if (match.Success) { pData._fieldValues[(int)FieldType.Sunrise] = match.Groups[1].Value; pData._fieldValues[(int)FieldType.Sunset] = match.Groups[2].Value; pData._fieldValues[(int)FieldType.Moonrise] = match.Groups[3].Value; } pattern2 = @"Sunrise:([\d:]+)Sunset:([\d:]+)Moonrise:None"; match = Regex.Match(input, pattern2); if (match.Success) { pData._fieldValues[(int)FieldType.Sunrise] = match.Groups[1].Value; pData._fieldValues[(int)FieldType.Sunset] = match.Groups[2].Value; pData._fieldValues[(int)FieldType.Moonrise] = "None"; } String pattern3 = @"Sun:(\w+)Entering"; match = Regex.Match(input, pattern3); if (match.Success) { pData._fieldValues[(int)FieldType.SunRasi] = match.Groups[1].Value; } String pattern4 = @"Chandra:(\w+)Entering"; match = Regex.Match(input, pattern4); if (match.Success) { pData._fieldValues[(int)FieldType.MoonRasi] = match.Groups[1].Value; } String pattern5 = @"Chandra:(\w+)Entering"; match = Regex.Match(input, pattern5); if (match.Success) { pData._fieldValues[(int)FieldType.MoonRasi] = match.Groups[1].Value; } pattern1 = @"Tithi:([\w ]+)End time:[\w \d:+]+Nakshatra:([\w\.]+) "; match = Regex.Match(input, pattern1); if (match.Success) { pData._fieldValues[(int)FieldType.Tithi] = match.Groups[1].Value; pData._fieldValues[(int)FieldType.Nakshatra] = match.Groups[2].Value; } //Rahukalam:11:10:50-12:22:24Yamagandam:14:45:31-15:57:05Gulikai:08:47:43-09:59:17Abhijit Muhurta:12:03:19-12:41:29 pattern1 = @"Rahukalam:([\d:-]+)Yamagandam:([\d:-]+)Gulikai:([\d:-]+)"; match = Regex.Match(input, pattern1); if (match.Success) { pData._fieldValues[(int)FieldType.RahuKalam] = match.Groups[1].Value; pData._fieldValues[(int)FieldType.YamaGandam] = match.Groups[2].Value; pData._fieldValues[(int)FieldType.Gulikai] = match.Groups[3].Value; } pattern1 = @"Yoga:(\w+)End"; match = Regex.Match(input, pattern1); if (match.Success) { pData._fieldValues[(int)FieldType.Yoga] = match.Groups[1].Value; } pattern1 = @"Karana:(\w+)End"; match = Regex.Match(input, pattern1); if (match.Success) { pData._fieldValues[(int)FieldType.Karana] = match.Groups[1].Value; } } } //Console.WriteLine("Final day Year {0} Month {1} Day {2}", Year, month, day); panchangData[(month - 1) * 31 + (day - 1)] = pData; for (int j = 0; j < (int)(FieldType.MaxFields); j++) { if ((j != (int)FieldType.None) && (j != (int)FieldType.TamilYear) && (j != (int)FieldType.Festival)) { if (pData._fieldValues[j] == null) { Console.WriteLine("Field Value {0} is null", ((FieldType)j).ToString()); Console.WriteLine(log); } } //Console.WriteLine("{0}: {1}", (FieldType)j, pData._fieldValues[j]); } } ser.WriteObject(fs, yearPanchangData); fs.Close(); FileStream fs1 = new FileStream(fileName, FileMode.Open); DataContractSerializer ser1 = new DataContractSerializer(typeof(YearlyPanchangData)); yearPanchangDataCopy = (YearlyPanchangData)ser1.ReadObject(fs1); } catch (Exception e) { Console.Write("Web load problem " + e.Message); } // PrintPanchangData(panchangData); }
static void OldGetCalendarDataPerCityAndYear(int Year, String City, String UrlToken, TimeZoneValues timeZone) { HtmlAgilityPack.HtmlWeb web = new HtmlWeb(); YearlyPanchangData yearPanchangData = new YearlyPanchangData(); PanchangData[] panchangData = new PanchangData[12 * 31]; yearPanchangData._panchangData = panchangData; int day = 0; String fileName = String.Format("IndianCalendar-{0}-{1}.dat", UrlToken, Year); Console.WriteLine("Filename is {0}", fileName); if (File.Exists(fileName)) { Console.WriteLine(fileName + " Exists"); return; } try { FileStream fs = new FileStream(fileName, FileMode.Create); DataContractSerializer ser = new DataContractSerializer(typeof(YearlyPanchangData)); // Get the calendar data for every month for (int month = 1; month <= 12; month++) { String url = String.Format("http://www.mypanchang.com/vcalformat.php?cityname={2}&yr={0}&mn={1}&monthtype=0", Year, month, UrlToken); HtmlDocument document = null; try { document = web.Load(url); } catch (Exception exp) { Console.WriteLine("Load Failed " + url + "\n" + exp.Message); return; } day = 0; foreach (HtmlNode node in document.DocumentNode.SelectNodes("//table")) { HtmlNodeCollection coll = node.ChildNodes; foreach (HtmlNode data in coll) { Console.WriteLine(data.InnerText.Trim()); Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); String input = data.InnerText; String pattern = @"panData\[(\d\d)\] = ""([^""]+)"";"; foreach (Match match in Regex.Matches(input, pattern, RegexOptions.IgnoreCase)) { Console.WriteLine("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"); Console.WriteLine(match.Groups[0].Value); Console.WriteLine(match.Groups[1].Value); HtmlDocument doc = new HtmlDocument(); String dayData = match.Groups[2].Value; // remove the bold elements to make it easier to parse String dayData1 = Regex.Replace(dayData, "<b>", " "); String dayData2 = Regex.Replace(dayData1, "</b>", " "); Console.WriteLine(dayData2); doc.LoadHtml(dayData2); PanchangData pData = new PanchangData(Year, month, day + 1); currentIndex = 0; CollectData(doc.DocumentNode, pData, scanPatterns); panchangData[(month - 1) * 31 + day] = pData; day++; pData._fieldValues[(int)FieldType.Festival] = GetFestivalData(Year, month, day, timeZone); if (day > 30) { break; } } if (day > 30) { break; } } if (day > 30) { break; } } } //ser.WriteObject(fs, yearPanchangData); //writer.Close(); //fs.Close(); //FileStream fs1 = new FileStream(fileName, FileMode.Open); //DataContractSerializer ser1 = new DataContractSerializer(typeof(YearlyPanchangData)); //YearlyPanchangData yearPanchangDataCopy; //yearPanchangDataCopy = (YearlyPanchangData)ser1.ReadObject(fs1); //Console.WriteLine("Done"); } catch (Exception e) { Console.Write("Web load problem " + e.Message); } // PrintPanchangData(panchangData); }