Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // xp_WebBrowser_read_amazon_com_UI.linq - rip book list from the Library page of the Amazon cloud reader
- // 2020-09-16
- //
- // see https://stackoverflow.com/a/63917496/4156577
- //<div class="book_container" id="B089DW5DYF">
- // <div class="book_cover">
- // <img title="Il misterioso caso di villa Grada (Le indagini del tenente Roversi Vol. 4) (Italian Edition)" class="book_image book_click_area" src="https://images-na.ssl-images-amazon.com/images/P/B089DW5DYF.01._SX255_SY255_TTXW_SCLZZZZZZZ_.jpg">
- // <div class="alt_title book_click_area"></div>
- // </div>
- // <div class="book_details">
- // <div class="book_title book_click_area">Il misterioso caso di villa Grada (Le indagini del tenente Roversi Vol. 4) (Italian Edition)</div>
- // <div class="book_author book_click_area">Gavino Zucca</div>
- // </div>
- //</div>
- class KindleBookListProgram
- {
- const string FILENAME_TEMPLATE = "x:\\kindle_library_{0:yyyyMMdd}.lst"; // gets DateTime.Now as parameter
- const int TIMER_INTERVAL_MS = 5000;
- const string READ_AMAZON_COM = "https://read.amazon.com/";
- const string USERAGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
- const int URLMON_OPTION_USERAGENT = 0x10000001;
- static void Main ()
- {
- // setting the user agent in the Navigate() call works only once;
- // this works for the whole session
- UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, USERAGENT, USERAGENT.Length, 0);
- using (var form = new BrowserForm())
- {
- form.ShowDialog();
- }
- }
- [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
- private static extern int UrlMkSetSessionOption (
- int dwOption, string pBuffer, int dwBufferLength, int dwReserved );
- class BrowserForm: Form
- {
- WebBrowser m_browser;
- System.Windows.Forms.Timer m_timer;
- string m_text_from_previous_try = null;
- public BrowserForm ()
- {
- Width = 800;
- Height = 600;
- m_timer = new System.Windows.Forms.Timer();
- m_timer.Interval = TIMER_INTERVAL_MS;
- m_timer.Tick += timer_Tick;
- Controls.Add(m_browser = new WebBrowser());
- m_browser.DocumentCompleted += browser_DocumentCompleted;
- m_browser.Dock = DockStyle.Fill;
- KeyPreview = true;
- KeyDown += this_KeyDown;
- m_browser.Navigate(READ_AMAZON_COM);
- }
- string find_book_list_frame_text (WebBrowser browser)
- {
- foreach (HtmlWindow frame in browser.Document.Window.Frames)
- {
- var elt = frame.Document.GetElementById("titles_inner_wrapper");
- if (elt != null)
- {
- return elt.InnerHtml;
- }
- }
- return null;
- }
- void save_book_list (string text)
- {
- var filename = string.Format(FILENAME_TEMPLATE, DateTime.Now);
- #if LINQPAD
- Console.WriteLine("\n##### {0} ######\n\n{1}\n\n", filename, text);
- #endif
- File.WriteAllText(filename, text, Encoding.Default);
- this.Text = filename + " saved!";
- }
- void timer_Tick (Object sender, EventArgs e)
- {
- m_timer.Stop();
- #if LINQPAD
- Console.WriteLine("{0} Tick event", DateTime.Now);
- #endif
- var text = find_book_list_frame_text(m_browser);
- if (!string.IsNullOrEmpty(text))
- {
- if (!string.IsNullOrEmpty(m_text_from_previous_try) && text == m_text_from_previous_try)
- {
- save_book_list(text);
- return;
- }
- m_text_from_previous_try = text;
- }
- m_timer.Start();
- }
- void browser_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
- {
- #if LINQPAD
- Console.WriteLine("{0} DocumentCompleted event", DateTime.Now);
- #endif
- m_timer.Stop();
- m_timer.Start();
- }
- void this_KeyDown (object sender, KeyEventArgs e)
- {
- if (e.Control && e.KeyValue == 17) // ^S
- {
- e.SuppressKeyPress = true;
- save_book_list(find_book_list_frame_text(m_browser) ?? "");
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment