SHARE
TWEET

GetFromN CLASSES/CODE

Feverbird Mar 15th, 2016 79 Never
  1. // MIND THAT I'M USING AN OPEN SOURCE SOLUTION TO ACCESS AND ANALYZE THE WEBCONTENT (HTMLAGILITYPACK)
  2. // I AM NOT GOING TO PASTE IT'S CODE IN HERE, GOOGLE IT YOURSELF
  3.  
  4. //CLASS CONTENTACCESS
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Net;
  11. using HtmlAgilityPack;
  12. using System.IO;
  13. using System.Text.RegularExpressions;
  14. using System.Diagnostics;
  15. using System.Threading;
  16. namespace GetFromN
  17. {
  18.     static class ContentAccess
  19.     {
  20.  
  21.        static List<ImageInfo> iiList = new List<ImageInfo>();
  22.         private static string LocalPath;
  23.         public static double Progress = 0;
  24.         public static string Status = "";
  25.         private static double OnePercent = 0;
  26.  
  27.         //public static string RemoveSpecialCharacters(string str)
  28.         //{
  29.         //    StringBuilder sb = new StringBuilder();
  30.         //    foreach (char c in str)
  31.         //    {
  32.         //        if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '.' || c == '_' || c == '[' || c ==']')
  33.         //        {
  34.         //            sb.Append(c);
  35.         //        }
  36.         //    }
  37.         //    return sb.ToString();
  38.         //}
  39.  
  40.         public static void Initialize(string ComicURI, string LocalePath)
  41.         {
  42.  
  43.             //using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GetFromN.HtmlAgilityPack.dll"))
  44.             //{
  45.             //    byte[] assemblyData = new byte[stream.Length];
  46.             //    stream.Read(assemblyData, 0, assemblyData.Length);
  47.             //    System.Reflection.Assembly.Load(assemblyData);
  48.             //}
  49.  
  50.             Status = "Initializing...";
  51.             LocalPath = LocalePath;
  52.                 HtmlWeb hatweb = new HtmlWeb();
  53.  
  54.                 HtmlDocument htdocs = hatweb.Load(ComicURI);
  55.             Status = "loaded baseUri";
  56.             //INFO
  57.             HtmlNode n1 = htdocs.DocumentNode.SelectSingleNode("//h1");
  58.             Status = "FILE " + n1.InnerText;
  59.             Thread.Sleep(500);
  60.             LocalPath += @"\" + CleanInput(n1.InnerText);
  61.             if (!Directory.Exists(LocalPath))
  62.             {
  63.                 Directory.CreateDirectory(LocalPath);
  64.             }
  65.             Status = "checking Nodes";
  66.             //
  67.             HtmlNode n = htdocs.DocumentNode.SelectSingleNode("//div[@class='ncontainer']");
  68.                 string ThumbnailContainer = n.InnerHtml;
  69.                 htdocs.LoadHtml(ThumbnailContainer);
  70.                 HtmlNodeCollection HatNodes = htdocs.DocumentNode.SelectNodes(".//img");
  71.             double onePerTempt = (double)50 / (double)HatNodes.Count;
  72.                 bool toggle = false;
  73.                 foreach (HtmlNode hn in HatNodes)
  74.                 {
  75.                 Thread.Sleep(50);
  76.                 Progress += onePerTempt;
  77.                 Status = "Creating virtual File ";
  78.                     if (toggle == true)
  79.                     {
  80.                         HtmlAttribute attr = hn.Attributes["src"];
  81.                     ImageInfo ii = new ImageInfo(attr.Value, LocalPath);
  82.                     Status += ii.FileName;
  83.                     iiList.Add(ii);
  84.                    
  85.                         toggle = false;
  86.                  
  87.                         continue;
  88.                     }
  89.                     toggle = true;
  90.  
  91.                 }
  92.             OnePercent = 50 / iiList.Count;
  93.            
  94.          
  95.  
  96.         }
  97.  
  98.         public static void Clear()
  99.         {
  100.             Progress = 0;
  101.             Status = "";
  102.             LocalPath = "";
  103.             iiList.Clear();
  104.         }
  105.  
  106.         static string CleanInput(string inp)
  107.         {
  108.             inp = inp.Replace(@"\","|");
  109.             inp = inp.Replace(@"/", "|");
  110.             inp = inp.Replace(@"[", "-");
  111.             inp = inp.Replace(@"]", "-");
  112.             inp = inp.Replace(@"|", "#");
  113.             return inp;
  114.         }
  115.  
  116.         //static string CleanInput(string strIn)
  117.         //{
  118.         //    // Replace invalid characters with empty strings.
  119.         //    try
  120.         //    {
  121.         //        return Regex.Replace(strIn, @"[^\w\.@-]", "",//  return Regex.Replace(strIn, @"[^\w\.@-]", "",
  122.         //                             RegexOptions.None, TimeSpan.FromSeconds(1.5));
  123.         //    }
  124.         //    // If we timeout when replacing invalid characters,
  125.         //    // we should return Empty.
  126.         //    catch (RegexMatchTimeoutException)
  127.         //    {
  128.         //        return String.Empty;
  129.         //    }
  130.         //}
  131.  
  132.         public static void ProcessImages()
  133.         {
  134.            
  135.            foreach (ImageInfo ii in iiList)
  136.             {
  137.                 Status = "Writing File " + ii.FileName;
  138.  
  139.                 ii.SaveFile();
  140.                 Progress += OnePercent;
  141.             }
  142.             Status = "Finished!";
  143.         }
  144.  
  145.  
  146.      
  147.  
  148.  
  149.     }
  150.  
  151. }
  152.  
  153.  
  154. // CLASS IMAGEINFO
  155. using System;
  156. using System.Collections.Generic;
  157. using System.Linq;
  158. using System.Text;
  159. using System.Threading.Tasks;
  160. using System.IO;
  161. using HtmlAgilityPack;
  162. using System.Net;
  163. using System.Text.RegularExpressions;
  164.  
  165. namespace GetFromN
  166. {
  167.     class ImageInfo
  168.     {
  169.         public ImageInfo(string ThumbNailPath, string LocalPath)
  170.         {
  171.             this.ThumbNailPath = ThumbNailPath;
  172.             this.LocalPath = LocalPath;
  173.             string Ext = Path.GetExtension(ThumbNailPath);
  174.             string FNL = Path.GetFileNameWithoutExtension(ThumbNailPath);
  175.             int RemovableNameLength = Path.GetFileName(ThumbNailPath).Length;
  176.             OverDir = Path.GetDirectoryName(ThumbNailPath).Remove(0,2);
  177.             OverDirNum = Path.GetFileName(OverDir);
  178.             Number = Convert.ToInt32(FNL.Remove(FNL.Length - 1,1));
  179.             FileName = Number + Ext;
  180.             Level1Path = @"http://i.nhentai.net/galleries/" + OverDirNum +@"/"+ FileName;
  181.          
  182.         }
  183.  
  184.        
  185.  
  186.  
  187.         public void SaveFile()
  188.         {
  189.             using (WebClient client = new WebClient())
  190.             {
  191.                 try
  192.                 {
  193.                     client.DownloadFile(Level1Path, LocalPath + @"\" + FileName);
  194.                 }
  195.                 catch (Exception)
  196.                 {
  197.  
  198.                 }
  199.             }
  200.         }
  201.  
  202.         public string ThumbNailPath { get; set; }
  203.         public string LocalPath { get; set; }
  204.         public string Level1Path { get; set; }
  205.         public string FileName { get; set; }
  206.         private string OverDir { get; set; }
  207.         private string OverDirNum { get; set; }
  208.         public int Number { get; set; }
  209.     }
  210. }
  211.  
  212.  
  213. //CLASS MAINWINDOW
  214. using System;
  215. using System.Collections.Generic;
  216. using System.Linq;
  217. using System.Text;
  218. using System.Threading.Tasks;
  219. using System.Windows;
  220. using System.Windows.Controls;
  221. using System.Windows.Data;
  222. using System.Windows.Documents;
  223. using System.Windows.Input;
  224. using System.Windows.Media;
  225. using System.Windows.Media.Imaging;
  226. using System.Windows.Navigation;
  227. using System.Windows.Shapes;
  228. using System.Text.RegularExpressions;
  229. using HtmlAgilityPack;
  230. using System.Net;
  231. using System.IO;
  232. using System.ComponentModel;
  233.  
  234. namespace GetFromN
  235. {
  236.     /// <summary>
  237.     /// Interaktionslogik für MainWindow.xaml
  238.     /// </summary>
  239.     public partial class MainWindow : Window
  240.     {
  241.         BackgroundWorker bw = new BackgroundWorker();
  242.         BackgroundWorker bw2 = new BackgroundWorker();
  243.         public MainWindow()
  244.         {
  245.             InitializeComponent();
  246.            
  247.  
  248.             //using (var stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("GetFromN.HtmlAgilityPack.dll"))
  249.             //{
  250.             //    byte[] assemblyData = new byte[stream.Length];
  251.             //    stream.Read(assemblyData, 0, assemblyData.Length);
  252.             //    //System.Reflection.Assembly.Load(assemblyData);
  253.             //    AppDomain.CurrentDomain.Load(assemblyData);
  254.             //}
  255.             bw.WorkerSupportsCancellation = true;
  256.             bw.DoWork += new DoWorkEventHandler(bw_DoWork);
  257.             bw2.WorkerSupportsCancellation = true;
  258.             bw2.DoWork += new DoWorkEventHandler(bw2_DoWork);
  259.             //   bw.WorkerReportsProgress = true;
  260.         }
  261.  
  262.        
  263.  
  264.         public string DestDir;
  265.         public string DestURI;
  266.         private void brwsBtn_Click(object sender, RoutedEventArgs e)
  267.         {
  268.             using (System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog())
  269.             {
  270.                 DestURI = srcBox.Text;
  271.                 fbd.Description = "Select the Directory to Save your Images in, a subdirectory will be created automagically.";
  272.                System.Windows.Forms.DialogResult objResult = fbd.ShowDialog();
  273.                 if (objResult == System.Windows.Forms.DialogResult.OK)
  274.                 {
  275.                     DestDir = fbd.SelectedPath;
  276.                     pthBox.Text = System.IO.Path.GetFileName(DestDir);
  277.                 }
  278.                 else
  279.                 {
  280.                     pthBox.Text = "cancelled";
  281.                 }
  282.  
  283.             }
  284.         }
  285.  
  286.         private void dwnButton_Click(object sender, RoutedEventArgs e)
  287.         {
  288.            
  289.  
  290.                 if (pthBox.Text != "cancelled")
  291.                 {
  292.                     bw2.RunWorkerAsync();
  293.                     bw.RunWorkerAsync();
  294.                 }
  295.  
  296.            
  297.            
  298.             if (dwnButton.Content.ToString() =="Start over")
  299.             {
  300.                 System.Diagnostics.Process.Start(Application.ResourceAssembly.Location);
  301.                 Application.Current.Shutdown();
  302.             }
  303.            
  304.  
  305.             }
  306.  
  307.         private void bw_DoWork(object sender, DoWorkEventArgs e)
  308.         {
  309.             BackgroundWorker worker = sender as BackgroundWorker;
  310.             if (uribox.Items.Count > 0)
  311.             {
  312.                 foreach (string s in uribox.Items)
  313.                 {
  314.                     ContentAccess.Initialize(s, DestDir);
  315.                     ContentAccess.ProcessImages();
  316.                     ContentAccess.Clear();
  317.                     pbStatus.Value = 100;
  318.                     StatusLabel.Content = "Finished";
  319.                     dwnButton.Content = "Start over";
  320.                     bw.CancelAsync();
  321.                 }
  322.              
  323.             }
  324.             if (uribox.Items.Count == 0)
  325.             {
  326.                 ContentAccess.Initialize(DestURI, DestDir);
  327.                 ContentAccess.ProcessImages();
  328.                 pbStatus.Value = 100;
  329.                 StatusLabel.Content = "Finished";
  330.                 dwnButton.Content = "Start over";
  331.                 bw.CancelAsync();
  332.             }
  333.  
  334.          
  335.         }
  336.  
  337.         private void bw2_DoWork(object sender, DoWorkEventArgs e)
  338.         {
  339.             while (ContentAccess.Progress <= 100)
  340.             {
  341.                 double Progress = 0.0;
  342.                 string Status ="";
  343.                 Dispatcher.Invoke(new Action(() => Progress = ContentAccess.Progress));
  344.                 Dispatcher.Invoke(new Action(() => Status = ContentAccess.Status));
  345.                 Dispatcher.Invoke(new Action(() => pbStatus.Value = Progress));
  346.                 Dispatcher.Invoke(new Action(() => StatusLabel.Content = Status));
  347.             }
  348.             Dispatcher.Invoke(new Action(() => dwnButton.Content = "Start over"));
  349.             bw2.CancelAsync();
  350.         }
  351.  
  352.         private void button_Click(object sender, RoutedEventArgs e)
  353.         {
  354.             if (srcBox.Text != "")
  355.             {
  356.                 uribox.Items.Add(srcBox.Text);
  357.                 srcBox.Text = "";
  358.             }
  359.         }
  360.  
  361.         private void pstBTN_Click(object sender, RoutedEventArgs e)
  362.         {
  363.             srcBox.Text = Clipboard.GetText();
  364.         }
  365.  
  366.         private void pstaddBTN_Click(object sender, RoutedEventArgs e)
  367.         {
  368.             srcBox.Text = "";
  369.            uribox.Items.Add(Clipboard.GetText());
  370.         }
  371.  
  372.         private void rmbtn_Click(object sender, RoutedEventArgs e)
  373.         {
  374.             uribox.Items.RemoveAt(uribox.SelectedIndex);
  375.         }
  376.     }
  377. }
RAW Paste Data
Top