DarthGizka

xp_WebBrowser_read_amazon_com_UI.linq

Sep 16th, 2020 (edited)
615
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. // xp_WebBrowser_read_amazon_com_UI.linq - rip book list from the Library page of the Amazon cloud reader
  2. // 2020-09-16
  3. //
  4. // see https://stackoverflow.com/a/63917496/4156577
  5.  
  6. //<div class="book_container" id="B089DW5DYF">
  7. //    <div class="book_cover">
  8. //        <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">
  9. //        <div class="alt_title book_click_area"></div>
  10. //    </div>
  11. //    <div class="book_details">
  12. //        <div class="book_title book_click_area">Il misterioso caso di villa Grada (Le indagini del tenente Roversi Vol. 4) (Italian Edition)</div>
  13. //        <div class="book_author book_click_area">Gavino Zucca</div>
  14. //    </div>
  15. //</div>
  16.  
  17. class KindleBookListProgram
  18. {
  19.     const string FILENAME_TEMPLATE = "x:\\kindle_library_{0:yyyyMMdd}.lst";  // gets DateTime.Now as parameter
  20.     const int TIMER_INTERVAL_MS = 5000;
  21.     const string READ_AMAZON_COM = "https://read.amazon.com/";
  22.     const string USERAGENT = "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko";
  23.     const int URLMON_OPTION_USERAGENT = 0x10000001;
  24.  
  25.     static void Main ()
  26.     {
  27.         // setting the user agent in the Navigate() call works only once;
  28.         // this works for the whole session
  29.         UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, USERAGENT, USERAGENT.Length, 0);
  30.    
  31.         using (var form = new BrowserForm())
  32.         {
  33.             form.ShowDialog();
  34.         }
  35.     }
  36.  
  37.     [DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
  38.     private static extern int UrlMkSetSessionOption (
  39.         int dwOption, string pBuffer, int dwBufferLength, int dwReserved );
  40.    
  41.     class BrowserForm: Form
  42.     {
  43.         WebBrowser m_browser;
  44.         System.Windows.Forms.Timer m_timer;
  45.         string m_text_from_previous_try = null;
  46.    
  47.         public BrowserForm ()
  48.         {  
  49.             Width = 800;
  50.             Height = 600;
  51.  
  52.             m_timer = new System.Windows.Forms.Timer();
  53.             m_timer.Interval = TIMER_INTERVAL_MS;
  54.             m_timer.Tick += timer_Tick;
  55.            
  56.             Controls.Add(m_browser = new WebBrowser());
  57.             m_browser.DocumentCompleted += browser_DocumentCompleted;
  58.             m_browser.Dock = DockStyle.Fill;
  59.    
  60.             KeyPreview = true;
  61.             KeyDown += this_KeyDown;
  62.    
  63.             m_browser.Navigate(READ_AMAZON_COM);
  64.         }
  65.  
  66.         string find_book_list_frame_text (WebBrowser browser)
  67.         {
  68.             foreach (HtmlWindow frame in browser.Document.Window.Frames)
  69.             {
  70.                 var elt = frame.Document.GetElementById("titles_inner_wrapper");
  71.            
  72.                 if (elt != null)
  73.                 {
  74.                     return elt.InnerHtml;
  75.                 }
  76.             }
  77.            
  78.             return null;
  79.         }
  80.  
  81.         void save_book_list (string text)
  82.         {
  83.             var filename = string.Format(FILENAME_TEMPLATE, DateTime.Now);
  84. #if LINQPAD
  85.             Console.WriteLine("\n##### {0} ######\n\n{1}\n\n", filename, text);
  86. #endif
  87.             File.WriteAllText(filename, text, Encoding.Default);
  88.             this.Text = filename + " saved!";      
  89.         }
  90.  
  91.         void timer_Tick (Object sender, EventArgs e)
  92.         {
  93.             m_timer.Stop();
  94.  
  95. #if LINQPAD
  96.             Console.WriteLine("{0} Tick event", DateTime.Now);
  97. #endif
  98.             var text = find_book_list_frame_text(m_browser);
  99.  
  100.             if (!string.IsNullOrEmpty(text))
  101.             {
  102.                 if (!string.IsNullOrEmpty(m_text_from_previous_try) && text == m_text_from_previous_try)
  103.                 {
  104.                     save_book_list(text);
  105.                     return;
  106.                 }
  107.            
  108.                 m_text_from_previous_try = text;
  109.             }
  110.            
  111.             m_timer.Start();
  112.         }
  113.  
  114.         void browser_DocumentCompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
  115.         {
  116. #if LINQPAD
  117.             Console.WriteLine("{0} DocumentCompleted event", DateTime.Now);
  118. #endif
  119.             m_timer.Stop();
  120.             m_timer.Start();
  121.         }
  122.  
  123.         void this_KeyDown (object sender, KeyEventArgs e)
  124.         {
  125.             if (e.Control && e.KeyValue == 17)  // ^S
  126.             {
  127.                 e.SuppressKeyPress = true;
  128.  
  129.                 save_book_list(find_book_list_frame_text(m_browser) ?? "");
  130.             }  
  131.         }
  132.     }
  133. }
  134.  
Add Comment
Please, Sign In to add comment