Guest User

Untitled

a guest
Jan 4th, 2015
541
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Net;
  10. using System.IO;
  11. using System.Threading;
  12. using System.Net.NetworkInformation;
  13. using System.Diagnostics;
  14.  
  15. namespace WindowsFormsApplication1 {
  16. public partial class Viewer : Form {
  17. string[] IMAGE_FORMATS = new string[] { ".JPG", ".JPEG", ".PNG", ".BMP", ".GIF", ".TIFF" };
  18. const string hostMain = "http://rghost.ru/main";
  19. const string hostRu = "http://rghost.ru";
  20. const string proxyConfPath = "proxy.conf";
  21.  
  22. public Viewer() {
  23. InitializeComponent();
  24. panel1.Dock = DockStyle.Fill;
  25. flowLayoutPanel1.Dock = DockStyle.Top;
  26. flowLayoutPanel1.Height = 2000;
  27. LoadProxy();
  28. }
  29.  
  30. void UpdateConnectedState() {
  31. PingHost(hostRu);
  32. }
  33.  
  34. WebProxy _proxy;
  35. bool _connected = false;
  36. bool Connected {
  37. get { return _connected; }
  38. set {
  39. if(!value) {
  40. toolStripStatusLabel1.Text = "Disconnected";
  41. }
  42. else {
  43. toolStripStatusLabel1.Text = "Connected";
  44. }
  45. toolStripButton1.Enabled = true;
  46. _connected = value;
  47. }
  48. }
  49.  
  50. void DoRefresh() {
  51. if(!Connected) {
  52. UpdateConnectedState();
  53. return;
  54. }
  55. List<string> urlsToLoad = new List<string>();
  56. try {
  57. toolStripStatusLabel1.Text = "Getting images from host...";
  58. toolStripButton1.Enabled = false;
  59. Application.DoEvents();
  60. WebRequest request = WebRequest.Create(hostMain);
  61. using(var response = request.GetResponse()) {
  62. using(var stream = response.GetResponseStream()) {
  63. string html;
  64. using(StreamReader sr = new StreamReader(stream)) {
  65. html = sr.ReadToEnd();
  66. int startInd = html.IndexOf(@"class=""main-column""");
  67. int endInd = html.IndexOf(@"<a href=""/files"">Далее");
  68. if(endInd <= startInd) return;
  69. html = html.Substring(startInd, endInd - startInd);
  70. List<string> lis = html.Split(new string[] { "<li>" }, StringSplitOptions.RemoveEmptyEntries).ToList();
  71. LeaveOnlyImages(lis);
  72. foreach(string li in lis) {
  73. List<string> splits = li.Split(new string[] { @"""" }, StringSplitOptions.RemoveEmptyEntries).ToList();
  74. if(splits.Count > 1) urlsToLoad.Add(splits[1]);
  75. }
  76. }
  77. }
  78. }
  79. }
  80. catch { Connected = false; }
  81. finally {
  82. ClearPix();
  83. urlsToLoad = urlsToLoad.Distinct().ToList();
  84. ProceedURLs(urlsToLoad);
  85. }
  86. }
  87.  
  88. void LeaveOnlyImages(List<string> lis) {
  89. List<string> lisToRemove = new List<string>();
  90. foreach(string li in lis) {
  91. string upperli = li.ToUpper();
  92. string[] splits = upperli.Split(IMAGE_FORMATS, StringSplitOptions.RemoveEmptyEntries);
  93. if(splits.Length <= 1) lisToRemove.Add(li);
  94. }
  95. foreach(string liToRemove in lisToRemove) {
  96. lis.Remove(liToRemove);
  97. }
  98. }
  99.  
  100. void ClearPix() {
  101. while(flowLayoutPanel1.Controls.Count > 0) {
  102. Control c = flowLayoutPanel1.Controls[0];
  103. flowLayoutPanel1.Controls.Remove(c);
  104. c.MouseDoubleClick -= new MouseEventHandler(pic_MouseDoubleClick);
  105. c.Dispose();
  106. c = null;
  107. }
  108. }
  109.  
  110. private void ProceedURLs(List<string> urlsToLoad) {
  111. if(urlsToLoad.Count == 0) { toolStripButton1.Enabled = true; return; }
  112. foreach(string url in urlsToLoad) {
  113. bool last = urlsToLoad.IndexOf(url) == urlsToLoad.Count - 1;
  114. Thread t = new Thread(() => {
  115. LoadImage(url, last);
  116. });
  117. t.Start();
  118. }
  119. }
  120.  
  121. void LoadImage(string url, bool last) {
  122. WebRequest request = WebRequest.Create(hostRu + url + "/thumb.png");
  123. try {
  124. using(var response = request.GetResponse()) {
  125. using(var stream = response.GetResponseStream()) {
  126. Image img = Bitmap.FromStream(stream);
  127. if(img == null) return;
  128. PictureBox pic = new PictureBox() { Size = img.Size, Image = img, Tag = hostRu + url };
  129. pic.MouseDoubleClick += new MouseEventHandler(pic_MouseDoubleClick);
  130. flowLayoutPanel1.Invoke(new Action(() => {
  131. flowLayoutPanel1.Controls.Add(pic);
  132. }));
  133. }
  134. }
  135. }
  136. catch { }
  137. finally {
  138. if(last) {
  139. toolStrip1.Invoke(new Action(() => {
  140. toolStripButton1.Enabled = true;
  141. toolStripStatusLabel1.Text = "Done!";
  142. }));
  143. }
  144. }
  145. }
  146.  
  147. void pic_MouseDoubleClick(object sender, MouseEventArgs e) {
  148. Control c = sender as Control;
  149. string url = (string) c.Tag;
  150. Process.Start(url);
  151. }
  152.  
  153. public void PingHost(string nameOrAddress) {
  154. toolStripStatusLabel1.Text = "Ping host...";
  155. Thread tr = new Thread(() => { RequestPing(nameOrAddress, this); });
  156. tr.Start();
  157. }
  158.  
  159. void RequestPing(string nameOrAddress, Viewer control) {
  160. try {
  161. WebRequest request = WebRequest.Create(nameOrAddress);
  162. using(var response = request.GetResponse()) {
  163. control.Invoke(new Action(() => {
  164. control.Connected = true;
  165. }));
  166. }
  167. }
  168. catch {
  169. control.Invoke(new Action(() => {
  170. control.Connected = false;
  171. }));
  172. }
  173. }
  174.  
  175. private void toolStripButton1_Click(object sender, EventArgs e) {
  176. DoRefresh();
  177. }
  178.  
  179. private void checkBox1_CheckedChanged(object sender, EventArgs e) {
  180. if(checkBox1.Checked)
  181. WebRequest.DefaultWebProxy = _proxy;
  182. else
  183. WebRequest.DefaultWebProxy = null;
  184. }
  185.  
  186. private void textBox1_TextChanged(object sender, EventArgs e) {
  187. try {
  188. string str = textBox1.Text.Trim();
  189. _proxy = new WebProxy(@"http://" + str);
  190. if(checkBox1.Checked) {
  191. WebRequest.DefaultWebProxy = _proxy;
  192. this.Connected = false;
  193. }
  194. SaveProxy();
  195. }
  196. catch { }
  197. }
  198.  
  199. void SaveProxy() {
  200. try {
  201. string str = textBox1.Text.Trim();
  202. File.WriteAllText(proxyConfPath, str);
  203. }
  204. catch { }
  205. }
  206. void LoadProxy() {
  207. try {
  208. if(File.Exists(proxyConfPath))
  209. textBox1.Text = File.ReadAllText(proxyConfPath);
  210. }
  211. catch { }
  212. }
  213. }
  214. }
Advertisement
Add Comment
Please, Sign In to add comment