Fighting Ant

Ant can be great while elephant can be chickenshit

导航

自己偶尔会去replays.net上下载一些rep来看(比较喜欢打魔兽)所以希望有一个工具可以把replays.net上的比较精彩的rep给down下来,于是乎做了一个小小的东西。设计思想比较简单,写起来也不麻烦。
用webRequest对replays.net发出请求,用webResponse获得网站服务器发回的回复,转换成为stream,用一个StreamReader将steam逐行读出,从中找到存放我们感兴趣的rep的网页的url存到ArrayList中,然后分别对ArrayList中的url发送请求,用一个简单的WebClient.DownLoadFile(string url, string filepath)就可以将其下载到本地的filepath中。看到网上有将网站发回的数据转换成stream然后将stream写成一个文件的做法,个人认为也是可取的。只不过有了现成的函数就偷了个懒了(希望不要拍砖@#¥)。建议重新启一个线程来下载文件,因为考虑到网络的不稳定性,当一个url上的文件响应时间超时可能会造成该线程的长时间阻塞,从而大大降低了下载的速度。

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.IO;
using System.Net;
using System.Collections;
using System.Threading;

namespace RepalysDownload_Beta
{
    
class Program
    
{
        
static void Main(string[] args)
        
{
            
int[] nums = 2345689101216 };
            
foreach (int num in nums)
            
{

                
try
                
{
                    
//Creat a webrequest
                    WebRequest wRequest = WebRequest.Create(LocalParameters.repwebUrl + num.ToString());
                    
//Creat a webresponse to get the resonponse from the remote server
                    WebResponse wResponse = wRequest.GetResponse();
                    
//Convert the response to stream.
                    Stream stream = wResponse.GetResponseStream();
                    StreamReader sr 
= new StreamReader(stream);

                    ArrayList AL 
= new ArrayList();
                    Replays rTemp 
= new Replays();
                    
string strLine = null;
                    
//The following cycle function is to analyzing the html and got the url that contains the reps we are interested in
                    
//while ((strLine = sr.ReadLine()) != null)
                    
//{
                    
//    if (strLine.StartsWith("<ul class=\"datarow3\""))
                    
//    //if(strLine.StartsWith("<li class=\"c_p\">"))
                    
//    {
                    
//        getUrlFromHtml getUrl = new getUrlFromHtml(strLine, "/doc", "html", 4);
                    
//        rTemp.url = getUrl.url();
                    
//    }
                    
//    if (strLine.StartsWith("<li class=\"c_i\">"))
                    
//    {
                    
//        if (strLine.Length > 21)
                    
//        {
                    
//            rTemp.hasCrown = true;
                    
//            AL.Add(rTemp);
                    
//        }
                    
//    }
                    
//}
                    while ((strLine = sr.ReadLine()) != null)
                    
{
                        
if (strLine.StartsWith("<li class=\"c_p\">"))
                        
{
                            getUrlFromHtml getUrl 
= new getUrlFromHtml(strLine, "/doc""html"4);
                            rTemp.url 
= getUrl.url();
                            
if (strLine.Contains("http://w3g.replays.net/images/"))
                            
{
                                rTemp.hasCrown 
= true;
                                AL.Add(rTemp);
                            }

                        }

                    }

                    
foreach (Replays rep in AL)
                    
{
                        WebRequest wbRequest 
= WebRequest.Create(LocalParameters.webUrl + rep.url);
                        WebResponse wbResponse 
= wbRequest.GetResponse();
                        Stream sstream 
= wbResponse.GetResponseStream();
                        StreamReader sReader 
= new StreamReader(sstream);
                        
//The following cycle function is to analyzing the url to get the rep's url.
                        while ((strLine = sReader.ReadLine()) != null)
                        
{
                            
string year = DateTime.Now.Year.ToString();
                            
string month = DateTime.Now.Month.ToString();
                            
string date = Convert.ToString((Convert.ToInt32(DateTime.Now.Day.ToString()) - 1));
                            
if (strLine.Contains("Download REP"))
                            
{
                                getUrlFromHtml gUrl 
= new getUrlFromHtml(strLine, "/Download.aspx?"".w3g"4);
                                
string htmlUrl = gUrl.url();
                                
string downLoadUrl = LocalParameters.webUrl + HttpUtility.UrlDecode(htmlUrl);
                                
string[] downLoadUrlParts = downLoadUrl.Split('/');
                                
int len = downLoadUrlParts.Length;
                                
string repName = downLoadUrlParts[len - 1];
                                
string dDate = year + "-" + month + "\\" + date + "\\";
                                
if (downLoadUrl.Contains(year) && downLoadUrl.Contains(month) && downLoadUrl.Contains(date))
                                
{
                                    
if (!Directory.Exists(LocalParameters.repDownLoadTo + dDate))
                                    
{
                                        Directory.CreateDirectory(LocalParameters.repDownLoadTo 
+ dDate);
                                    }

                                    WebClient wc 
= new WebClient();
                                    DownLoadFile DLfile 
= new DownLoadFile(downLoadUrl, LocalParameters.repDownLoadTo + dDate + repName);
                                    Thread tDownload 
= new Thread(new ThreadStart(DLfile.Download));
                                    tDownload.Start();
                                }

                            }

                        }

                    }

                }

                
catch (Exception ex)
                
{
                    Console.WriteLine(ex.ToString());
                }

            }

        }

    }

    
/**/
    
/// <summary>
    
/// This class is to Download the file from url to the path 'name'
    
/// </summary>

    class DownLoadFile
    
{
        
private string url;
        
private string name;
        
public DownLoadFile(string Url, string Name)
        
{
            
this.url = Url;
            
this.name = Name;
        }

        
public void Download()
        
{
            WebClient wc 
= new WebClient();
            
try
            
{
                wc.DownloadFile(url, name);
                Console.WriteLine(
"One rep has been download");
            }

            
catch (Exception ex)
            
{
                Console.WriteLine(
"Error:{0}", ex.ToString());
            }

        }

    }

    
/**/
    
/// <summary>
    
/// Define the local parameters
    
/// </summary>

    static class LocalParameters
    
{
        
public static string webUrl = "http://w3g.replays.net";
        
public static string repwebUrl = "http://w3g.replays.net/replaylist.aspx?Gamerace=";
        
public static string repDownLoadTo = @"E:\game\WarIII\魔兽争霸3冰封王座V1.21版\Warcraft III\replay\";
    }

    
/**/
    
/// <summary>
    
/// This struct is to store the url we're interested in
    
/// if the url is what we want the hasCrown equals 'true'
    
/// </summary>

    struct Replays
    
{
        
public string url;
        
public bool hasCrown;
    }

    
/**/
    
/// <summary>
    
/// The class is to get the url from replays list. the tip is to correct the index of end char
    
/// for example end with 'html' will set the tip as 4 while 'htm' as 3
    
/// </summary>

    class getUrlFromHtml
    
{
        
private string urlInHtml;
        
private string begin;
        
private string end;
        
private int tip;

        
public getUrlFromHtml(string html, string beginWith, string endWith, int tip)
        
{
            
this.urlInHtml = html;
            
this.begin = beginWith;
            
this.end = endWith;
            
this.tip = tip;
        }

        
public string url()
        
{
            
int b = urlInHtml.IndexOf(begin);
            
int e = urlInHtml.IndexOf(end);
            
string url = urlInHtml.Substring(b, e - b + tip);
            
return url;
        }

    }

}

 

附件已经编译好的文件
https://files.cnblogs.com/ppchouyou/ReplaysDownLoad.zip