Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Net;
- using System.IO;
- using System.Threading;
- using System.Net.NetworkInformation;
- using System.Diagnostics;
- namespace WindowsFormsApplication1 {
- public partial class Viewer : Form {
- string[] IMAGE_FORMATS = new string[] { ".JPG", ".JPEG", ".PNG", ".BMP", ".GIF", ".TIFF" };
- const string hostMain = "http://rghost.ru/main";
- const string hostRu = "http://rghost.ru";
- const string proxyConfPath = "proxy.conf";
- public Viewer() {
- InitializeComponent();
- panel1.Dock = DockStyle.Fill;
- flowLayoutPanel1.Dock = DockStyle.Top;
- flowLayoutPanel1.Height = 2000;
- LoadProxy();
- }
- void UpdateConnectedState() {
- PingHost(hostRu);
- }
- WebProxy _proxy;
- bool _connected = false;
- bool Connected {
- get { return _connected; }
- set {
- if(!value) {
- toolStripStatusLabel1.Text = "Disconnected";
- }
- else {
- toolStripStatusLabel1.Text = "Connected";
- }
- toolStripButton1.Enabled = true;
- _connected = value;
- }
- }
- void DoRefresh() {
- if(!Connected) {
- UpdateConnectedState();
- return;
- }
- List<string> urlsToLoad = new List<string>();
- try {
- toolStripStatusLabel1.Text = "Getting images from host...";
- toolStripButton1.Enabled = false;
- Application.DoEvents();
- WebRequest request = WebRequest.Create(hostMain);
- using(var response = request.GetResponse()) {
- using(var stream = response.GetResponseStream()) {
- string html;
- using(StreamReader sr = new StreamReader(stream)) {
- html = sr.ReadToEnd();
- int startInd = html.IndexOf(@"class=""main-column""");
- int endInd = html.IndexOf(@"<a href=""/files"">Далее");
- if(endInd <= startInd) return;
- html = html.Substring(startInd, endInd - startInd);
- List<string> lis = html.Split(new string[] { "<li>" }, StringSplitOptions.RemoveEmptyEntries).ToList();
- LeaveOnlyImages(lis);
- foreach(string li in lis) {
- List<string> splits = li.Split(new string[] { @"""" }, StringSplitOptions.RemoveEmptyEntries).ToList();
- if(splits.Count > 1) urlsToLoad.Add(splits[1]);
- }
- }
- }
- }
- }
- catch { Connected = false; }
- finally {
- ClearPix();
- urlsToLoad = urlsToLoad.Distinct().ToList();
- ProceedURLs(urlsToLoad);
- }
- }
- void LeaveOnlyImages(List<string> lis) {
- List<string> lisToRemove = new List<string>();
- foreach(string li in lis) {
- string upperli = li.ToUpper();
- string[] splits = upperli.Split(IMAGE_FORMATS, StringSplitOptions.RemoveEmptyEntries);
- if(splits.Length <= 1) lisToRemove.Add(li);
- }
- foreach(string liToRemove in lisToRemove) {
- lis.Remove(liToRemove);
- }
- }
- void ClearPix() {
- while(flowLayoutPanel1.Controls.Count > 0) {
- Control c = flowLayoutPanel1.Controls[0];
- flowLayoutPanel1.Controls.Remove(c);
- c.MouseDoubleClick -= new MouseEventHandler(pic_MouseDoubleClick);
- c.Dispose();
- c = null;
- }
- }
- private void ProceedURLs(List<string> urlsToLoad) {
- if(urlsToLoad.Count == 0) { toolStripButton1.Enabled = true; return; }
- foreach(string url in urlsToLoad) {
- bool last = urlsToLoad.IndexOf(url) == urlsToLoad.Count - 1;
- Thread t = new Thread(() => {
- LoadImage(url, last);
- });
- t.Start();
- }
- }
- void LoadImage(string url, bool last) {
- WebRequest request = WebRequest.Create(hostRu + url + "/thumb.png");
- try {
- using(var response = request.GetResponse()) {
- using(var stream = response.GetResponseStream()) {
- Image img = Bitmap.FromStream(stream);
- if(img == null) return;
- PictureBox pic = new PictureBox() { Size = img.Size, Image = img, Tag = hostRu + url };
- pic.MouseDoubleClick += new MouseEventHandler(pic_MouseDoubleClick);
- flowLayoutPanel1.Invoke(new Action(() => {
- flowLayoutPanel1.Controls.Add(pic);
- }));
- }
- }
- }
- catch { }
- finally {
- if(last) {
- toolStrip1.Invoke(new Action(() => {
- toolStripButton1.Enabled = true;
- toolStripStatusLabel1.Text = "Done!";
- }));
- }
- }
- }
- void pic_MouseDoubleClick(object sender, MouseEventArgs e) {
- Control c = sender as Control;
- string url = (string) c.Tag;
- Process.Start(url);
- }
- public void PingHost(string nameOrAddress) {
- toolStripStatusLabel1.Text = "Ping host...";
- Thread tr = new Thread(() => { RequestPing(nameOrAddress, this); });
- tr.Start();
- }
- void RequestPing(string nameOrAddress, Viewer control) {
- try {
- WebRequest request = WebRequest.Create(nameOrAddress);
- using(var response = request.GetResponse()) {
- control.Invoke(new Action(() => {
- control.Connected = true;
- }));
- }
- }
- catch {
- control.Invoke(new Action(() => {
- control.Connected = false;
- }));
- }
- }
- private void toolStripButton1_Click(object sender, EventArgs e) {
- DoRefresh();
- }
- private void checkBox1_CheckedChanged(object sender, EventArgs e) {
- if(checkBox1.Checked)
- WebRequest.DefaultWebProxy = _proxy;
- else
- WebRequest.DefaultWebProxy = null;
- }
- private void textBox1_TextChanged(object sender, EventArgs e) {
- try {
- string str = textBox1.Text.Trim();
- _proxy = new WebProxy(@"http://" + str);
- if(checkBox1.Checked) {
- WebRequest.DefaultWebProxy = _proxy;
- this.Connected = false;
- }
- SaveProxy();
- }
- catch { }
- }
- void SaveProxy() {
- try {
- string str = textBox1.Text.Trim();
- File.WriteAllText(proxyConfPath, str);
- }
- catch { }
- }
- void LoadProxy() {
- try {
- if(File.Exists(proxyConfPath))
- textBox1.Text = File.ReadAllText(proxyConfPath);
- }
- catch { }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment