Pythorian

Proxy Scraper

Mar 7th, 2012
1,236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.65 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.Specialized;
  5. using System.Linq;
  6. using System.Text;
  7.  
  8. using System.Net;
  9. using System.IO;
  10. using System.Data;
  11. using System.Data.SqlClient;
  12. using System.Text.RegularExpressions;
  13.  
  14. namespace Proxies
  15. {
  16.     class Program
  17.     {
  18.         static CookieAwareWebClient webClient = new CookieAwareWebClient();
  19.         static void Main(string[] args)
  20.         {
  21.             GetProxiesfromProxyList(10000);
  22.             GetProxiesfromXRoxy(115);
  23.         }
  24.         private static void drawTextProgressBar(int progress, int total)
  25.         {
  26.             //draw empty progress bar
  27.             Console.CursorLeft = 0;
  28.             Console.Write("["); //start
  29.             Console.CursorLeft = 32;
  30.             Console.Write("]"); //end
  31.             Console.CursorLeft = 1;
  32.             float onechunk = 30.0f / total;
  33.  
  34.             //draw filled part
  35.             int position = 1;
  36.             for (int i = 0; i < onechunk * progress; i++)
  37.             {
  38.                 Console.BackgroundColor = ConsoleColor.Gray;
  39.                 Console.CursorLeft = position++;
  40.                 Console.Write(" ");
  41.             }
  42.  
  43.             //draw unfilled part
  44.             for (int i = position; i <= 31; i++)
  45.             {
  46.                 Console.BackgroundColor = ConsoleColor.Black;
  47.                 Console.CursorLeft = position++;
  48.                 Console.Write(" ");
  49.             }
  50.  
  51.             //draw totals
  52.             Console.CursorLeft = 35;
  53.             Console.BackgroundColor = ConsoleColor.Black;
  54.             Console.Write(progress.ToString() + " of " + total.ToString() + "    "); //blanks at the end remove any excess
  55.         }
  56.         private static void GetProxiesfromProxyList(int pages)
  57.         {
  58.             ArrayList listArray = new ArrayList();
  59.            
  60.             for (int inc = 0; inc < pages; inc++)
  61.             {
  62.                 try
  63.                 {
  64.                     string resp = webClient.DownloadString("http://www.proxylist.net/list/0/0/1/0/" + inc.ToString());
  65.                     if (resp.Contains("No proxies found!"))
  66.                         break;
  67.                     MatchCollection listingMatches = Regex.Matches(resp, "<tr title=\"(.+)\"><td><a href=\"/proxy/(.+):(.+)\">(.+):(.+)</a>");
  68.                     foreach (Match m in listingMatches)
  69.                     {
  70.                         string proxy = m.Groups[2].Value + ":" + m.Groups[3].Value;
  71.                         listArray.Add(proxy);
  72.                     }
  73.                 }
  74.                 catch
  75.                 { }
  76.             }
  77.             int i = 0;
  78.             foreach (string proxy in listArray)
  79.             {
  80.                 try
  81.                 {
  82.                     i++;
  83.                     Console.Clear();
  84.                     drawTextProgressBar(i, listArray.Count);
  85.                     if (proxyBasicTest(proxy))
  86.                     {
  87.                         SqlConnection Conn = new SqlConnection("Data Source=localhost;Initial Catalog=Scraping;Integrated Security=SSPI;");
  88.                         SqlCommand Comm = new SqlCommand("insertProxy", Conn);
  89.                         Comm.CommandType = System.Data.CommandType.StoredProcedure;
  90.                         Comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ip", proxy.Split(':')[0]));
  91.                         Comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Port", proxy.Split(':')[1]));
  92.                         Conn.Open();
  93.                         Comm.ExecuteNonQuery();
  94.                         Conn.Close();
  95.                         Console.WriteLine("\n\nAdded: " + proxy);
  96.                     }
  97.                 }
  98.                 catch
  99.                 { }
  100.             }
  101.         }
  102.         private static void GetProxiesfromXRoxy(int pages)
  103.         {
  104.             ArrayList listArray = new ArrayList();
  105.             for (int inc = 0; inc < pages; inc++)
  106.             {
  107.                 try
  108.                 {
  109.                     string resp = webClient.DownloadString("http://www.xroxy.com/proxylist.php?port=&type=&ssl=&country=&latency=&reliability=&sort=reliability&desc=true&pnum=" + inc.ToString());
  110.                     drawTextProgressBar(inc, pages);
  111.  
  112.                     MatchCollection listingMatches = Regex.Matches(resp, "&host=(?<host>.*)&port=(?<port>.*)&");
  113.                     foreach (Match m in listingMatches)
  114.                     {
  115.                         string proxy = m.Groups["host"].Value + ":" + m.Groups["port"].Value.Split('&')[0];
  116.                         listArray.Add(proxy);
  117.                     }
  118.                 }
  119.                 catch
  120.                 { }
  121.             }
  122.             int i = 0;
  123.             foreach (string proxy in listArray)
  124.             {
  125.                 try
  126.                 {
  127.                     i++;
  128.                     Console.Clear();
  129.                     drawTextProgressBar(i, listArray.Count);
  130.                     double ratio = (double)listArray.Count / (double)i;
  131.                     Console.WriteLine(" " + Convert.ToInt32((ratio * 100)).ToString() + "%");
  132.                     if (proxyBasicTest(proxy))
  133.                     {
  134.                         SqlConnection Conn = new SqlConnection("Data Source=localhost;Initial Catalog=Scraping;Integrated Security=SSPI;");
  135.                         SqlCommand Comm = new SqlCommand("insertProxy", Conn);
  136.                         Comm.CommandType = System.Data.CommandType.StoredProcedure;
  137.                         Comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ip", proxy.Split(':')[0]));
  138.                         Comm.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Port", proxy.Split(':')[1]));
  139.                         Conn.Open();
  140.                         Comm.ExecuteNonQuery();
  141.                         Conn.Close();
  142.                     }
  143.                 }
  144.                 catch
  145.                 { }
  146.             }
  147.         }
  148.  
  149.         private static bool proxyBasicTest(string proxy)
  150.         {
  151.             try
  152.             {
  153.                 HttpWebRequest wrWebRequest = WebRequest.Create("http://manta.com") as HttpWebRequest;
  154.                 WebProxy Proxy = new WebProxy(proxy.Split(':')[0], int.Parse(proxy.Split(':')[1]));
  155.                 wrWebRequest.Proxy = Proxy;
  156.                 wrWebRequest.Timeout = 5000;
  157.                 HttpWebResponse hwrWebResponse = (HttpWebResponse)wrWebRequest.GetResponse();
  158.                 System.IO.StreamReader srResponseReader = new System.IO.StreamReader(hwrWebResponse.GetResponseStream());
  159.                 string strResponseData = srResponseReader.ReadToEnd();
  160.                 srResponseReader.Close();
  161.                 if (strResponseData.Contains("codeen"))
  162.                     return false;
  163.                 if (strResponseData.Contains("manta"))
  164.                     return true;
  165.                 return false;
  166.             }
  167.             catch (Exception ex)
  168.             {
  169.                 return false;
  170.             }
  171.         }
  172.     }
  173.  
  174.     public class CookieAwareWebClient : WebClient
  175.     {
  176.  
  177.         public CookieContainer m_container = new CookieContainer();
  178.  
  179.         protected override WebRequest GetWebRequest(Uri address)
  180.         {
  181.             WebRequest request = base.GetWebRequest(address);
  182.             if (request is HttpWebRequest)
  183.             {
  184.                 (request as HttpWebRequest).CookieContainer = m_container;
  185.                 (request as HttpWebRequest).Timeout = 5000;
  186.                 (request as HttpWebRequest).UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; SLCC1;";
  187.                 (request as HttpWebRequest).KeepAlive = true;
  188.             }
  189.             return request;
  190.         }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment