コード例 #1
0
ファイル: Program.cs プロジェクト: Gravicode/Al-Hadith
        static void GetIndoContent()
        {
            List<HadithContent> IndoContent = new List<HadithContent>();
            HadithDBEntities ctx = new HadithDBEntities();
            var hadist = (from c in ctx.Hadiths
                          where c.Name == "bukhari"
                          select c).ToList();

            for (int i = 0; i < hadist.Count; i++)
            {
                var selHadith = hadist[i];
                var hadistIndex = (from c in ctx.HadithIndexes
                                   where c.HadithID == selHadith.HadithID
                                   orderby c.No
                                   select c).ToList();
                for (int j = 0; j < hadistIndex.Count; j++)
                {
                    var selIndex = hadistIndex[j];
                    var selURL = string.Format("http://sunnah.com/{0}/{1}", selHadith.Name, selIndex.No);

                    try
                    {
                        //CookieAwareWebClient client = new CookieAwareWebClient ();
                        //var Webget = new HtmlWeb();
                        MyWebClient client = new MyWebClient();
                        var doc = client.GetPage(selURL); //Webget.Load(selURL);
                        HadithChapter selChapter = null;
                        int ContentCounter = 0;

                        HadithPage selPage = new HadithPage();
                        selPage.PageNo = selIndex.No;
                        selPage.HadithID = selHadith.HadithID;
                        //get title
                        foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div"))
                        {
                            if (node.Attributes["class"] != null && !string.IsNullOrEmpty(node.Attributes["class"].Value))
                            {
                                switch (node.Attributes["class"].Value)
                                {
                                    case "book_page_english_name":
                                        selPage.Title = node.InnerHtml;
                                        break;
                                    case "book_page_arabic_name arabic":
                                        selPage.TitleArabic = node.InnerHtml;
                                        //ctx.HadithPages.Add(selPage);
                                        break;

                                    case "chapter":
                                        selChapter = new HadithChapter();
                                        selChapter.HadithID = selHadith.HadithID;
                                        selChapter.PageNo = selPage.PageNo;
                                        //iterate every chapter
                                        var chapterNode = node;
                                        {
                                            var subnode = chapterNode.SelectSingleNode(".//div[@class='echapno']");
                                            if (subnode != null)
                                            {
                                                selChapter.ChapterNo = Convert.ToInt32(subnode.InnerText.Replace("(", "").Replace(")", ""));
                                            }
                                        }
                                        {
                                            var subnode = chapterNode.SelectSingleNode(".//div[@class='englishchapter']");
                                            if (subnode != null)
                                            {
                                                selChapter.Title = subnode.InnerText;
                                            }
                                        }
                                        {
                                            var subnode = chapterNode.SelectSingleNode(".//div[@class='arabicchapter arabic']");
                                            if (subnode != null)
                                            {
                                                selChapter.TitleArabic = subnode.InnerText;
                                            }
                                        }
                                        //ctx.HadithChapters.Add(selChapter);
                                        break;
                                    case "arabic achapintro":
                                        {
                                            if (selChapter != null)
                                            {
                                                selChapter.Intro = node.InnerText;
                                            }
                                            //ctx.SaveChanges();
                                        }
                                        break;
                                    case "actualHadithContainer":
                                        HadithContent selContent = new HadithContent();
                                        selContent.HadithID = selHadith.HadithID;
                                        selContent.PageNo = selPage.PageNo;
                                        //selContent.ChapterNo = selPage.PageNo;
                                        if (selChapter != null)
                                            selContent.ChapterNo = selChapter.ChapterNo;
                                        {
                                            var subnode = node.SelectSingleNode(".//div[@class='hadith_narrated']");
                                            if (subnode != null)
                                            {
                                                selContent.Narated = subnode.InnerHtml;
                                            }
                                        }
                                        {
                                            var subnode = node.SelectSingleNode(".//div[@class='text_details']");
                                            if (subnode != null)
                                            {
                                                selContent.ContentEnglish = subnode.InnerHtml;
                                            }
                                        }
                                        {
                                            var subnode = node.SelectSingleNode(".//div[@class='indonesian_hadith_full']");
                                            if (subnode != null)
                                            {
                                                selContent.ContentIndonesia = subnode.InnerHtml;
                                            }
                                        }
                                        {
                                            var subnode = node.SelectSingleNode(".//table[@class='gradetable']");
                                            if (subnode != null)
                                            {
                                                selContent.Grade = subnode.InnerText;
                                            }
                                        }
                                        {
                                            var subnode = node.SelectSingleNode(".//table[@class='hadith_reference']");

                                            selContent.Reference = subnode.InnerHtml;
                                        }
                                        {
                                            var subnode = node.SelectNodes(".//span[@class='arabic_sanad arabic']");
                                            selContent.SanadTop = subnode[0].InnerHtml;
                                            selContent.SanadBottom = subnode[1].InnerHtml;

                                        }
                                        {
                                            var subnode = node.SelectSingleNode(".//span[@class='arabic_text_details arabic']");
                                            selContent.ContentArabic = subnode.InnerHtml;
                                        }
                                        IndoContent.Add(selContent);
                                        //ctx.HadithContents.Add(selContent);
                                        ContentCounter++;
                                        if (ContentCounter > 100)
                                        {
                                            //ctx.SaveChanges();
                                            ContentCounter = 0;
                                        }
                                        break;
                                    default: break;
                                }
                            }


                        }

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                    Console.WriteLine(IndoContent.Count);
                    Console.ReadLine();
                    //ctx.SaveChanges();
                    break;
                }
            }
        }