批量下载魔兽replays录像文件
生活随笔
收集整理的這篇文章主要介紹了
批量下载魔兽replays录像文件
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
?????自己是個war3愛好者,也經(jīng)常去浩方或者VS平臺去打打,技術(shù)不怎么樣但也偶爾去http://w3g.replays.net上去下載些replays學(xué)習(xí)學(xué)習(xí),不過Replays_Net上的錄像下載確實麻煩,每個鏈接都得單個點(diǎn)擊進(jìn)去才能下載,看到有人開發(fā)過mp3的批量下載器,所以想想就自己開發(fā)個類似這樣的下載魔獸replays錄像文件的東西,方便自己使用,也方便和我一樣的war3愛好者。實現(xiàn)思路很簡單,參考一些批量下載的程序,技術(shù)沒什么新的,實用就行了。
?
?? 已經(jīng)修改為Form版本(有需要的留言):
?
???? 主要代碼如下:
???? War3Replays.java:
?
package com.moonights.war3replays;/*** * @author moonights**/ public class War3Replays { /**錄像文件名稱*/ private String fileName; /**錄像文件大小*/ private String fileSize; /**錄像文件格式*/ private String fileFormat=".w3g"; /**下載地址*/ private String fileUrl;private String savePath; public String getFileFormat() {return fileFormat;}public void setFileFormat(String fileFormat) {this.fileFormat = fileFormat;}public String getFileName() {return fileName;}public void setFileName(String fileName) {this.fileName = fileName;}public String getFileSize() {return fileSize;}public void setFileSize(String fileSize) {this.fileSize = fileSize;}public String getFileUrl() {return fileUrl;}public void setFileUrl(String fileUrl) {this.fileUrl = fileUrl;}public String getSavePath() {return savePath;}public void setSavePath(String savePath) {this.savePath = savePath;} }?
War3ReplaysDowner.java:
?
package com.moonights.war3replays;import java.io.*; import java.net.*; import java.util.*; import java.util.regex.*;import com.moonights.utils.Configuration; import com.moonights.utils.HttpGet;/*** http://w3g.replays.net 批量下載war3Replays文件的工具,方便喜愛下載魔獸錄像的人員使用.* 目前只實現(xiàn)到解析到二級目錄下的頁面,可增加遞歸實現(xiàn)多級頁面解析....* * @author moonights* */ public class War3ReplaysDowner {private static Configuration config = new Configuration("config/war3.properties");private static String DOWNLOAD_INDEX_URL = config.getValue("DOWNLOAD_INDEX_URL");private static String DOWNLOAD_MAIN_URL = config.getValue("DOWNLOAD_MAIN_URL");// "http://w3g.replays.net/Default.aspx?PageNo=2";private static String FILE_SAVE_PATH = config.getValue("FILE_SAVE_PATH");// "E:\\temp";private static String FILE_SAVE_TYPE = config.getValue("FILE_SAVE_TYPE");// ".w3g";/*** * 只針對這種類型的url:<a* href="http://w3g.replays.net/doc/cn/2010-1-25/12644174774532572561.html"* target="_blank">**/private static final String url_regexp_resources = "<a href=\"(\\w+://[^/:]+[^#\\s]*)\" target=\"_blank\">";private static final String url_regexp_downloadFile = "<a href=\"(.*?)\">Download REP</a>";private static final String url_regexp_downloadFileName = "<h3><span id=\"ctl00_Content_labTitle\">(.*?)</span></h3>";/*** 根據(jù) URL 讀取應(yīng)對頁面的HTML源碼* * @param url* 文件的URL* @return String URL應(yīng)對頁面的HTML源碼, 如果連接到指定URL, 則返回一個空字符串("")*/public String getHtmlCode(String url) {try {URL u = new URL(url);URLConnection urlConnection = u.openConnection();urlConnection.setAllowUserInteraction(false);// 使用openStream得到一輸入流并由此構(gòu)造一個BufferedReader對象BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream()));String inputLine;StringBuffer tempHtml = new StringBuffer();while ((inputLine = in.readLine()) != null) { // 從輸入流不斷的讀數(shù)據(jù),直到讀完為止tempHtml.append(inputLine).append("\n");}return tempHtml.toString();} catch (IOException e) {return "";}}/*** 根據(jù) URL 讀取應(yīng)對頁面的HTML源碼* * @param url* 文件的URL* @return String URL應(yīng)對頁面的HTML源碼, 如果連接到指定URL, 則返回一個空字符串("")*/public static String getHtml(String urlString) {try {StringBuffer html = new StringBuffer();URL url = new URL(urlString);HttpURLConnection conn = (HttpURLConnection) url.openConnection();InputStreamReader isr = new InputStreamReader(conn.getInputStream());BufferedReader br = new BufferedReader(isr);String temp;while ((temp = br.readLine()) != null) {html.append(temp).append("\n");}br.close();isr.close();return html.toString();} catch (Exception e) {e.printStackTrace();return null;}}/*** 根據(jù)url獲取該url頁面中的所有包含下載資源的url鏈接() 只針對這種類型的url:<a* href="http://w3g.replays.net/doc/cn/2010-1-25/12644174774532572561.html"* target="_blank">* * @param 文件的URL* @return List* URL應(yīng)對頁面中的所有指定URL:http://w3g.replays.net/doc/cn/2010-1-25/12644174774532572561.html*/public List<String> getUrlByURLPage(String url) {List<String> list = new ArrayList<String>();String htmlCode = getHtmlCode(url);Pattern p = Pattern.compile(url_regexp_resources,Pattern.CASE_INSENSITIVE);Matcher matcher = p.matcher(htmlCode);String id = null;while (matcher.find()) {id = matcher.group(1);if (!list.contains(id)) {list.add(id);}}return list;}/*** 根據(jù)獲取的包含下載資源的url查詢下載資源url連接以及文件名稱 (文件名如下:[SW]GaB.RohJinWook vs ieS.Check* #1.w3g,解析頁面中的標(biāo)題獲取,中文有亂碼,和手動下載的名稱不一致但類似)* * @param List* @return Vector* {("http://w3g.replays.net/Download.aspx?ReplayID=41162&File=%2fReplayFile%2f2010-1-26%2f100126_%5bUD%5dfantafiction_VS_%5bORC%5dmmmgbp_TwistedMeadows_RN.w3g",* "e:\\temp\\dmmmgbp_TwistedMeadows_RN.w3g")}*/public Vector getWar3ReplaysByTitle(List url_list) {Vector vector = new Vector();for (int i = 0; i < url_list.size(); i++) {String temp_url = url_list.get(i).toString();String temp_htmlCode = this.getHtmlCode(temp_url);String fileUrl = getMatcher(url_regexp_downloadFile, temp_htmlCode,1);String fileName = getMatcher(url_regexp_downloadFileName,temp_htmlCode, 1);if (!fileUrl.equals("") && !fileName.equals("")) {War3Replays war3Replays = new War3Replays();fileUrl = DOWNLOAD_INDEX_URL + fileUrl;// 將相對URL修改為絕對URL// fileName = FILE_SAVE_PATH+"\\"+fileName+FILE_SAVE_TYPE;//war3Replays.setFileName(fileName);war3Replays.setFileUrl(fileUrl);war3Replays.setSavePath(this.FILE_SAVE_PATH);war3Replays.setFileFormat(this.FILE_SAVE_TYPE);vector.add(war3Replays);}if (i > 0 && i % 10 == 0) {// 每循環(huán)10次后休息2秒再進(jìn)行請求, 否則可能被當(dāng)作網(wǎng)絡(luò)攻擊try {Thread.sleep(2000);System.out.println(">>>>>>>>>>>>>>>>>>>暫停頁面抓取,2秒后繼續(xù)<<<<<<<<<<<<<<<<<<<<<<");} catch (InterruptedException e) {e.printStackTrace();}}}return vector;}/*** 根據(jù)獲取的包含下載資源的url鏈接查詢下載War3Replays文件* (文件名如下:_ReplayFile_2010-1-24_100124__NE_mTw.DIDI8_VS__HM_VeryB1gman._AncientIsles_RN.w3g,解析url獲取的)* 待優(yōu)化:待簡略一些..* * @param url_list* @return*/public Vector getWar3ReplaysByUrl(List url_list) {Vector vector = new Vector();for (int i = 0; i < url_list.size(); i++) {String temp_url = url_list.get(i).toString();String temp_htmlCode = this.getHtmlCode(temp_url);String fileUrl = getMatcher(url_regexp_downloadFile, temp_htmlCode,1);if (!fileUrl.equals("")) {War3Replays war3Replays = new War3Replays();fileUrl = DOWNLOAD_INDEX_URL + fileUrl;// 將相對URL修改為絕對URLwar3Replays.setFileUrl(fileUrl);String regex = "&File=(.*?).w3g";String fileName = getMatcher(regex, fileUrl, 1);fileName = fileName.replaceAll("(%2f|%5b|%5d)", "_");war3Replays.setFileName(fileName);war3Replays.setSavePath(this.FILE_SAVE_PATH);war3Replays.setFileFormat(this.FILE_SAVE_TYPE);vector.add(war3Replays);}if (i > 0 && i % 10 == 0) {// 每循環(huán)10次后休息2秒再進(jìn)行請求, 否則可能被當(dāng)作網(wǎng)絡(luò)攻擊try {Thread.sleep(2000);System.out.println(">>>>>>>>>>>>>>>>>>>暫停頁面抓取,2秒后繼續(xù)<<<<<<<<<<<<<<<<<<<<<<");} catch (InterruptedException e) {e.printStackTrace();}}}return vector;}public static String getMatcher(String regex, String source, int group) {String result = "";Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);Matcher matcher = pattern.matcher(source);while (matcher.find()) {result = matcher.group(group);}return result;}/*** 下載獲取到的war3replays錄像文件 方法一*/public void downWar3Replays_WAY1() {List url_list = this.getUrlByURLPage(DOWNLOAD_MAIN_URL);Vector vector = this.getWar3ReplaysByUrl(url_list);HttpGet downer = new HttpGet();if (vector.size() > 0) {for (int i = 0; i < vector.size(); i++) {War3Replays war3Replays = (War3Replays) vector.get(i);try {// 增加下載列表(此處用戶可以寫入自己代碼來增加下載列表)downer.addItem(war3Replays.getFileUrl(), war3Replays.getSavePath()+ "\\"+ war3Replays.getFileName()+ war3Replays.getFileFormat());// 開始下載} catch (Exception err) {System.out.println(err.getMessage());}}System.out.println("開始下載.");downer.downLoadByList();System.out.println("下載完畢.");}}/*** 下載獲取到的war3replays錄像文件 方法二*/public void downWar3Replays_WAY2() {List url_list = this.getUrlByURLPage(DOWNLOAD_MAIN_URL);Vector vector = this.getWar3ReplaysByTitle(url_list);HttpGet downer = new HttpGet();if (vector.size() > 0) {for (int i = 0; i < vector.size(); i++) {War3Replays war3Replays = (War3Replays) vector.get(i);try {// 增加下載列表(此處用戶可以寫入自己代碼來增加下載列表)downer.addItem(war3Replays.getFileUrl(), war3Replays.getSavePath()+ "\\"+ war3Replays.getFileName()+ war3Replays.getFileFormat());// 開始下載} catch (Exception err) {System.out.println(err.getMessage());}}System.out.println("開始下載.");downer.downLoadByList();System.out.println("下載完畢.");}} }?
HttpGet.java:
?
package com.moonights.utils;import java.io.*; import java.net.*; import java.util.*;/*** Description: 將指定的HTTP網(wǎng)絡(luò)資源在本地以文件形式存放*/ public class HttpGet {public final static boolean DEBUG = true;// 調(diào)試用private static int BUFFER_SIZE = 8096;// 緩沖區(qū)大小private Vector vDownLoad = new Vector();// URL列表private Vector vFileList = new Vector();// 下載后的保存文件名列表/*** 構(gòu)造方法*/public HttpGet() {}/*** 清除下載列表*/public void resetList() {vDownLoad.clear();vFileList.clear();}/*** 增加下載列表項* * @param url* String* @param filename* String*/public void addItem(String url, String filename) {vDownLoad.add(url);vFileList.add(filename);}/*** 根據(jù)列表下載資源*/public void downLoadByList() {String url = null;String filename = null;// 按列表順序保存資源for (int i = 0; i < vDownLoad.size(); i++) {url = (String) vDownLoad.get(i);filename = (String) vFileList.get(i);try {saveToFile(url, filename);} catch (IOException err) {if (DEBUG) {System.out.println("資源[" + url + "]下載失敗!!!");}}/*if(i>0 && i%5==0){ // 每循環(huán)5次后休息2秒再進(jìn)行請求, 否則可能被當(dāng)作網(wǎng)絡(luò)攻擊 try { //Thread.sleep(2000); //System.out.println(">>>>>>>>>>>>>>>>>>>暫停下載,2秒后繼續(xù)<<<<<<<<<<<<<<<<<<<<<<");}catch (InterruptedException e) { e.printStackTrace(); }}*/}if (DEBUG) {System.out.println("下載完成!!!");}}/*** 將HTTP資源另存為文件* * @param destUrl* String* @param fileName* String* @throws Exception*/public void saveToFile(String destUrl, String fileName) throws IOException {FileOutputStream fos = null;BufferedInputStream bis = null;HttpURLConnection httpUrl = null;URL url = null;byte[] buf = new byte[BUFFER_SIZE];int size = 0;// 建立鏈接url = new URL(destUrl);httpUrl = (HttpURLConnection) url.openConnection();// 連接指定的資源httpUrl.connect();// 獲取網(wǎng)絡(luò)輸入流bis = new BufferedInputStream(httpUrl.getInputStream());// 建立文件fos = new FileOutputStream(fileName);//判斷是否存在該文件,如果存在將文件名稱修改為另外一個。。。。if (this.DEBUG)System.out.println("正在獲取鏈接[" + destUrl + "]的內(nèi)容...\n將其保存為文件["+ fileName + "]");// 保存文件while ((size = bis.read(buf)) != -1)fos.write(buf, 0, size);fos.close();bis.close();httpUrl.disconnect();}/*** 設(shè)置代理服務(wù)器* * @param proxy* String* @param proxyPort* String*/public void setProxyServer(String proxy, String proxyPort) {// 設(shè)置代理服務(wù)器System.getProperties().put("proxySet", "true");System.getProperties().put("proxyHost", proxy);System.getProperties().put("proxyPort", proxyPort);}/*** 設(shè)置認(rèn)證用戶名與密碼* * @param uid* String* @param pwd* String*//*public void setAuthenticator(String uid, String pwd) {Authenticator.setDefault(new MyAuthenticator(uid, pwd));}*//*** 主方法(用于測試)* * @param argv* String[]*/public static void main(String argv[]) {HttpGet oInstance = new HttpGet();try {// 增加下載列表(此處用戶可以寫入自己代碼來增加下載列表)oInstance.addItem("http://xiazai.xiazaiba.com/0905/0504/2k3IIS6_XiaZaiBa.rar","e:\\temp\\iis6_2.rar");// 開始下載oInstance.downLoadByList();} catch (Exception err) {System.out.println(err.getMessage());}}}?
?? Configuration.java:?
?
package com.moonights.utils;import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties;/** * 讀取properties文件* @author moonights**/ public class Configuration {private Properties propertie;private InputStream inputFile;private FileOutputStream outputFile;/** * 初始化Configuration類*/public Configuration(){propertie = new Properties();}/** * 初始化Configuration類* @param filePath 要讀取的配置文件的路徑+名稱*/public Configuration(String filePath){propertie = new Properties();try {//inputFile = (InputStream) this.getClass().getResourceAsStream(filePath);inputFile = this.getClass().getClassLoader().getResourceAsStream(filePath); propertie.load(inputFile);inputFile.close();} catch (FileNotFoundException ex) {System.out.println("讀取屬性文件--->失敗!- 原因:文件路徑錯誤或者文件不存在");ex.printStackTrace();} catch (IOException ex) {System.out.println("裝載文件--->失敗!");ex.printStackTrace();}}//end ReadConfigInfo()/** * 重載函數(shù),得到key的值* @param key 取得其值的鍵* @return key的值*/public String getValue(String key){if(propertie.containsKey(key)){String value = propertie.getProperty(key);//得到某一屬性的值return value;}else return "";}/** * 重載函數(shù),得到key的值* @param fileName properties文件的路徑+文件名* @param key 取得其值的鍵* @return key的值*/public String getValue(String fileName, String key){try {String value = "";inputFile = new FileInputStream(fileName);propertie.load(inputFile);inputFile.close();if(propertie.containsKey(key)){value = propertie.getProperty(key);return value;}elsereturn value;} catch (FileNotFoundException e) {e.printStackTrace();return "";} catch (IOException e) {e.printStackTrace();return "";} catch (Exception ex) {ex.printStackTrace();return "";}}//end getValue()/** * 清除properties文件中所有的key和其值*/public void clear(){propertie.clear();}//end clear();/** * 改變或添加一個key的值,當(dāng)key存在于properties文件中時該key的值被value所代替,* 當(dāng)key不存在時,該key的值是value* @param key 要存入的鍵* @param value 要存入的值*/public void setValue(String key, String value){propertie.setProperty(key, value);}//end setValue()/** * 將更改后的文件數(shù)據(jù)存入指定的文件中,該文件可以事先不存在。* @param fileName 文件路徑+文件名稱* @param description 對該文件的描述*/public void saveFile(String fileName, String description){try {outputFile = new FileOutputStream(fileName);propertie.store(outputFile, description);outputFile.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException ioe){ioe.printStackTrace();}}//end saveFile()/*** 測試* @param args*/ }?
?Main.java:程序入口
package com.moonights.war3replays;/*** 程序入口。* @author moonights**/ public class Main {public static void main(String[] args) {War3ReplaysDowner war3_Downer = new War3ReplaysDowner();//war3_Downer.downWar3Replays_WAY1();war3_Downer.downWar3Replays_WAY2();} }?
war3.properties:配置文件
##下載首頁面 DOWNLOAD_INDEX_URL = http://w3g.replays.net ##下載主頁面(修改該變量即可) DOWNLOAD_MAIN_URL = http://w3g.replays.net/ReplayList.aspx?GameRace=3&PageNo=6 ##靈活下載多個頁面中的資源 (暫時未實現(xiàn)) PageNo=5 MAX_PageNo=50 ##文件保存路徑 FILE_SAVE_PATH=F:\\1.22\\hum ##文件格式 FILE_SAVE_TYPE=.w3g???
??? 程序還有挺多需要改進(jìn)的地方,文件路徑必須先有,時間問題沒有做過多的考慮,希望有哪位提提意見,優(yōu)化一下。如果有人修改成界面的,那就更好了。
?
總結(jié)
以上是生活随笔為你收集整理的批量下载魔兽replays录像文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Anaconda依赖包的安装
- 下一篇: 无法卸载mysql server 200