示例#1
0
        private static string RetrieveRawText(BiblePlace place)
        {
            string url = string.Format(READER_URL, place.Book, place.Chapter, place.Start, place.End);
            //Console.WriteLine(url);
            HttpWebRequest  request        = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response       = (HttpWebResponse)request.GetResponse();
            StreamReader    reader         = new StreamReader(response.GetResponseStream(), Encoding.Default);
            string          responseString = reader.ReadToEnd().Replace("<br>", " ");

            return(responseString);
        }
示例#2
0
        private static List <BibleVerse> GetAllTexts()
        {
            List <BibleVerse> texts = new List <BibleVerse>();

            using (MyContext db = new MyContext())
                using (StreamReader sr = new StreamReader("d:\\texts.txt", Encoding.Default))
                {
                    string line;
                    while ((line = sr.ReadLine()) != null)
                    {
                        if (line == "break")
                        {
                            break;
                        }

                        try
                        {
                            string     cleanText = null;
                            BiblePlace place     = GetBiblePlace(line);
                            var        placeInDb = (from t in db.BiblePlaces
                                                    where t.BookBG == place.BookBG &&
                                                    t.Chapter == place.Chapter &&
                                                    t.End == place.End &&
                                                    t.FullLocation == place.FullLocation &&
                                                    t.Start == place.Start
                                                    select t).SingleOrDefault();
                            if (placeInDb != null)
                            {
                                cleanText = placeInDb.BibleVerses.Single().Text;
                            }
                            else
                            {
                                string rawText = RetrieveRawText(place);
                                cleanText = ExtractCleanText(rawText);

                                // Add a record
                                db.BiblePlaces.Add(place);
                                db.SaveChanges();
                                BibleVerse verse = new BibleVerse()
                                {
                                    BiblePlace = place,
                                    Text       = cleanText
                                };
                                db.BibleVerses.Add(verse);
                                db.SaveChanges();
                                Thread.Sleep(500);
                            }
                            texts.Add(new BibleVerse()
                            {
                                BiblePlace = place, Text = cleanText
                            });
                            string output = string.Format("Place {0} is OK", line);
                            Console.WriteLine(output);
                            fileOutput.WriteLine(output);
                        }
                        catch (Exception ex)
                        {
                            string output = "Problem with row: " + line + "\n      " + ex.Message;
                            Console.WriteLine(output);
                            fileOutput.WriteLine(output);
                        }
                    }
                }
            return(texts);
        }
示例#3
0
        public static void CreateDoc(List <BibleVerse> verseList, StreamWriter fileOutput)
        {
#if DEBUG
            string fileName = "D:\\test.docx";
            Console.WriteLine("Filename is {0}\n", fileName);
#else
            Console.Write("\nPlease enter the destination docx file: ");
            string fileName = Console.ReadLine();
#endif
            bool            isNewFile   = true;
            WordApplication application = new WordApplication();
            // Delete the file before writing
            if (!ShouldAppend)
            {
                File.Delete(fileName);
            }

            if (File.Exists(fileName))
            {
                application.Documents.Open(fileName);
                isNewFile = false;
            }
            else
            {
                application.Documents.Add();
            }

            object    pageBreak = WdBreakType.wdPageBreak;
            Paragraph paragraph;
            for (int i = 0; i < verseList.Count; i++)
            {
                // The number of text
                paragraph            = application.ActiveDocument.Content.Paragraphs.Add();
                paragraph.Range.Text = (i + startTextIndex).ToString();
                paragraph.set_Style(application.ActiveDocument.Styles["Heading 1"]);
                paragraph.Range.InsertParagraphAfter();
                // Verse
                paragraph            = application.ActiveDocument.Content.Paragraphs.Add();
                paragraph.Range.Text = verseList[i].Text;
                paragraph.Range.InsertParagraphAfter();
                // Place
                paragraph            = application.ActiveDocument.Content.Paragraphs.Add();
                paragraph.Range.Text = verseList[i].BiblePlace.FullLocation;
                paragraph.set_Style(application.ActiveDocument.Styles["Heading 2"]);
                paragraph.Range.InsertParagraphAfter();

                // Page Break
                if (i < verseList.Count - 1)
                {
                    paragraph.Range.InsertBreak(ref pageBreak);
                }
                System.Runtime.InteropServices.Marshal.ReleaseComObject(paragraph);

                // Check for overlapping
                BiblePlace currentPlace = verseList[i].BiblePlace;
                var        q            = verseList.GetRange(0, i).Where(a => a.BiblePlace.IsOverlaping(currentPlace));
                if (q.Any())
                {
                    string output = string.Format("Row {0}: Place {1} is overlapping the following places: {2}\n",
                                                  i + 1, currentPlace.ToString(), string.Join("; ", q.Select(a => a.BiblePlace)));
                    Console.WriteLine(output);
                    fileOutput.WriteLine(output);
                }
            }

            try
            {
                if (isNewFile)
                {
                    application.ActiveDocument.SaveAs(fileName);
                }
                else
                {
                    application.ActiveDocument.Save();
                }
                application.ActiveDocument.Close();
                application.Quit();
                System.Runtime.InteropServices.Marshal.ReleaseComObject(application);
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
            catch
            {
                paragraph   = null;
                application = null;
            }
        }