public TorrentFile(string FileName) { try { System.IO.FileStream TorrentFile = new System.IO.FileStream(FileName, System.IO.FileMode.Open); if (TorrentFile.Length == 0) { return; } byte[] TorrentBytes = new byte[TorrentFile.Length]; TorrentFile.Read(TorrentBytes, 0, TorrentBytes.Length); TorrentFile.Close(); if ((char)TorrentBytes[0] != 'd') { if (OpenError.Length == 0) { OpenError = "错误的Torrent文件,开头第1字节不是100"; } return; } GetTorrentData(TorrentBytes); if (TorrentName.Length == 0 && TorrentFileInfo.Count > 0) { TorrentName = TorrentFileInfo[0].Path; } } catch (System.Exception ex) { //错误日志记录 H31Debug.PrintLn("TorrentFile:" + ex.StackTrace); } }
/// <summary> /// 开始读取 /// </summary> /// <param name="TorrentBytes"></param> private void GetTorrentData(byte[] TorrentBytes) { try { int StarIndex = 1; while (true) { string test = Encoding.UTF8.GetString(TorrentBytes, StarIndex, TorrentBytes.Length - StarIndex > 500 ? 500 : TorrentBytes.Length - StarIndex); object Keys = GetKeyText(TorrentBytes, ref StarIndex); if (Keys == null) { if (StarIndex >= TorrentBytes.Length) { OpenFile = true; } break; } if (GetValueText(TorrentBytes, ref StarIndex, Keys.ToString().ToUpper()) == false) { break; } } } catch (System.Exception ex) { //错误日志记录 H31Debug.PrintLn("GetTorrentData:" + ex.StackTrace); } }
//移动种子文件到子文件夹下 private int MoveTorrentFileToSubDir(string pathname) { DirectoryInfo filedir = new DirectoryInfo(pathname); foreach (DirectoryInfo NextFolder in filedir.GetDirectories()) { if (NextFolder.Name.Length == 1) { foreach (FileInfo fileChild in NextFolder.GetFiles("*.torrent")) { try { Application.DoEvents(); //让系统在百忙之中来响应其他事件 string fc = fileChild.ToString(); int finddot = fc.IndexOf('.'); string hashname = fc.Substring(0, finddot); string pathname1 = Path.Combine(pathname, hashname.Substring(hashname.Length - 2, 2).ToUpper()); if (!Directory.Exists(pathname1)) { Directory.CreateDirectory(pathname1); } string filename1 = Path.Combine(fileChild.DirectoryName, fc); string filename2 = Path.Combine(pathname1, fc); File.Move(filename1, filename2); } catch (Exception ex) { H31Debug.PrintLn(ex.Message); } } } } return(0); }
private int DownLoadFileToSaveFile(string strURL, string fileName, int timeout1) { Int32 ticktime1 = System.Environment.TickCount; try { Int32 ticktime2 = 0; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(strURL); wr.ContentType = "application/x-bittorrent"; wr.Timeout = timeout1; WebResponse response = wr.GetResponse(); int readsize = 0; { bool gzip = response.Headers["Content-Encoding"] == "gzip"; Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream(); using (MemoryStream memoryStream = new MemoryStream()) { int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); readsize += count; } while (count != 0); ticktime2 = System.Environment.TickCount; byte[] result = memoryStream.ToArray(); Thread.Sleep(10); using (BinaryWriter writer = new BinaryWriter(new FileStream(fileName, FileMode.Create))) { writer.Write(result); } } Int32 ticktime3 = System.Environment.TickCount; //H31Debug.PrintLn("下载成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); } return(1); } catch (WebException e) { Int32 ticktime3 = System.Environment.TickCount; if (e.Status == WebExceptionStatus.Timeout)//文件超时 { return(-2); } else if (e.Status == WebExceptionStatus.ProtocolError)//文件不存在 { return(-3); } else { H31Debug.PrintLn("下载失败" + strURL + ":" + (ticktime3 - ticktime1).ToString() + e.Status.ToString() + e.Message); return(-4); } } }
//读取配置文件 private int ReadSetting() { try { configDir = Path.Combine(m_localPath, CONFIG_DIR); string file = Path.Combine(configDir, "Setting.txt"); if (File.Exists(file)) { StreamReader reader = new StreamReader(file, Encoding.Default); while (true) { string str1 = reader.ReadLine(); if (str1 == null) { break; } string str2 = reader.ReadLine(); if (str2 == null) { break; } long readPos = Convert.ToInt32(str2); m_fileOKList[str1] = readPos; } } string file1 = Path.Combine(configDir, "BadList.txt"); if (File.Exists(file1)) { StreamReader reader = new StreamReader(file1, Encoding.Default); Int32 ticktime = System.Environment.TickCount / 1000; string str1 = reader.ReadLine(); while (str1 != null) { m_downBadList[str1] = ticktime; str1 = reader.ReadLine(); } reader.Close(); } string file2 = Path.Combine(configDir, "OKList.txt"); if (File.Exists(file2)) { StreamReader reader = new StreamReader(file2, Encoding.Default); Int32 ticktime = System.Environment.TickCount / 1000; string str1 = reader.ReadLine(); while (str1 != null) { m_downOKList[str1] = ticktime; str1 = reader.ReadLine(); } reader.Close(); } } catch (System.Exception ex) { H31Debug.PrintLn("ReadSetting:" + ex.StackTrace); } return(1); }
/// <summary> /// 遍历该目录下所有文件,并返回一个文件名进行读取 /// </summary> private int GetOneFileDataToMDB() { if (!Directory.Exists(resourceDir)) { Directory.CreateDirectory(resourceDir); } DirectoryInfo filedir = new DirectoryInfo(resourceDir); foreach (FileInfo fileChild in filedir.GetFiles("*.txt")) //*201*.txt //old 201x年 谁知道2020 年我发现下载了使用时 { try { Application.DoEvents(); //让系统在百忙之中来响应其他事件 string fc = fileChild.ToString(); //如果是同一个文件,则需要检测是否读取完成 if (m_fileOKList.ContainsKey(fc)) { System.IO.FileStream file1 = new System.IO.FileStream(Path.Combine(resourceDir, fc), System.IO.FileMode.Open); long len = file1.Length; file1.Close(); if (len > m_fileOKList[fc]) { m_readFilename = fc; m_readPos = m_fileOKList[fc]; m_fileTotal_Len = len; MainProgressBar.Maximum = Convert.ToInt32(m_fileTotal_Len / 100); MainProgressBar.Value = Convert.ToInt32(m_readPos / 100); this.Text = "已经读取" + m_readFilename + ":" + m_doWorkCnt.ToString() + "行"; return(1); } } else { m_doWorkCnt = 0; m_readFilename = fc; m_fileOKList[fc] = 0; m_readPos = 0; System.IO.FileStream file1 = new System.IO.FileStream(Path.Combine(resourceDir, fc), System.IO.FileMode.Open); m_fileTotal_Len = file1.Length; file1.Close(); MainProgressBar.Maximum = Convert.ToInt32(m_fileTotal_Len / 100); MainProgressBar.Value = Convert.ToInt32(m_readPos / 100); this.Text = "已经读取" + m_readFilename + ":" + m_doWorkCnt.ToString() + "行"; return(1); } } catch (Exception ex) { H31Debug.PrintLn(ex.Message); } } return(0); }
public int DownLoadFileByHashToFile(string hashname) { try { if (pathname == string.Empty) { string localfile = AppDomain.CurrentDomain.BaseDirectory; pathname = Path.Combine(localfile, "Torrent"); if (!Directory.Exists(pathname)) { Directory.CreateDirectory(pathname); string tmpFolder = Path.Combine(pathname, "BAD"); if (!Directory.Exists(tmpFolder)) { Directory.CreateDirectory(tmpFolder); } } } //检测子文件夹是否存在 string pathname1 = Path.Combine(pathname, hashname.Substring(hashname.Length - 2, 2)); if (!Directory.Exists(pathname1)) { Directory.CreateDirectory(pathname1); } string filename = string.Format("{0}\\{1}\\{2}.torrent", pathname, hashname.Substring(hashname.Length - 2, 2), hashname); if (File.Exists(filename)) { return(1); } //随机从一个网址下载 //downwebpos = (downwebpos + 1) % 2; //if (DownLoadFileToSaveFile(m_strURLList[downwebpos], filename) == 1) // return 1; //随机打乱三个网址顺序下载,防止从一个网站下载过多被封 downwebpos = (downwebpos + 1); //从三种网址一一测试下载 foreach (var torrentServer in _serverList) { if (1 == DownLoadFileToSaveFile(torrentServer.GetUrlStr(filename), filename, torrentServer.Timeout)) { return(1); } } return(0); } catch (Exception e) { H31Debug.PrintLn(e.Message); return(-2); } }
public byte[] DownLoadFileByHashToByte(string hashname) { byte[] res = null; try { //先检查本地有没有文件,如果有直接读取本地,没有再从网络上下载 string filename = string.Format("{0}//{1}.torrent", pathname, hashname); if (File.Exists(filename)) { System.IO.FileStream TorrentFile = new System.IO.FileStream(filename, System.IO.FileMode.Open); if (TorrentFile.Length > 0) { res = new byte[TorrentFile.Length]; TorrentFile.Read(res, 0, res.Length); TorrentFile.Close(); return(res); } } m_strURLList[0] = string.Format("https://zoink.it/torrent/{0}.torrent", hashname); m_strURLList[1] = string.Format("http://bt.box.n0808.com/{0}/{1}/{2}.torrent", hashname.Substring(0, 2), hashname.Substring(hashname.Length - 2, 2), hashname); m_strURLList[2] = string.Format("http://torrage.com/torrent/{0}.torrent", hashname); m_strURLList[3] = string.Format("http://torcache.net/torrent/{0}.torrent", hashname); m_timeoutList[0] = 300; m_timeoutList[1] = 300; m_timeoutList[2] = 700; m_timeoutList[3] = 700; //随机从前面两个网站中的一个下载,因为前面两个网站速度快些 downwebpos = (downwebpos + 1) % 2; //res = DownLoadFileToSaveByte(m_strURLList[downwebpos]); //随机打乱三个网址顺序下载,防止从一个网站下载过多被封 res = DownLoadFileToSaveByte(m_strURLList[downwebpos], m_timeoutList[downwebpos]); if (res == null) { //res = DownLoadFileToSaveByte(m_strURLList[(downwebpos + 1) % 2]); //if (res == null) { res = DownLoadFileToSaveByte(m_strURLList[2], m_timeoutList[2]); } } return(res); } catch (Exception e) { H31Debug.PrintLn(e.Message); return(null); } }
static void Main() { try { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); } catch (Exception ex) { H31Debug.PrintLn(ex.Message); MessageBox.Show(ex.Message); } }
//向后一页查询 private void Button_NEXTPAGE_Click(object sender, EventArgs e) { try { int searchindex = Convert.ToInt32(textBox_SearchIndex.Text); searchindex = searchindex + 1; textBox_SearchIndex.Text = searchindex.ToString(); ButtonSearch_Click(null, null); } catch (System.Exception ex) { H31Debug.PrintLn(ex.StackTrace); } }
private byte[] DownLoadFileToSaveByte(string strURL, int timeout1) { Int32 ticktime1 = System.Environment.TickCount; byte[] result = null; try { Int32 ticktime2 = 0; byte[] buffer = new byte[4096]; WebRequest wr = WebRequest.Create(strURL); wr.ContentType = "application/x-bittorrent"; wr.Timeout = timeout1; WebResponse response = wr.GetResponse(); int readsize = 0; { bool gzip = response.Headers["Content-Encoding"] == "gzip"; Stream responseStream = gzip ? new GZipStream(response.GetResponseStream(), CompressionMode.Decompress) : response.GetResponseStream(); using (MemoryStream memoryStream = new MemoryStream()) { //responseStream.ReadTimeout = timeout1*2; int count = 0; do { count = responseStream.Read(buffer, 0, buffer.Length); memoryStream.Write(buffer, 0, count); readsize += count; } while (count != 0); ticktime2 = System.Environment.TickCount; Thread.Sleep(10); result = memoryStream.ToArray(); } Int32 ticktime3 = System.Environment.TickCount; //H31Debug.PrintLn("下载成功" + strURL + ":" + readsize.ToString() + ":" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime3 - ticktime2).ToString()); } wr.Abort(); return(result); } catch (Exception e) { Int32 ticktime3 = System.Environment.TickCount; H31Debug.PrintLn("下载失败" + strURL + ":" + (ticktime3 - ticktime1).ToString() + "err:" + e.Message); return(null); } }
/// <summary> /// 开始停止读取 2013-06-16 /// </summary> private void ButtonStart_Click(object sender, EventArgs e) { try { if (ButtonStart.Text == "开始") { //将文件夹由16个变成256个 MoveTorrentFileToSubDir(Path.Combine(m_localPath, "Torrent")); MainStatusText.Text = string.Format("准备读取:{0}", DateTime.Now.ToString()); recvthreadison = false; isFirstTimeCheck = true; ReadSetting(); m_nowTableCount = 0; m_nowTableID = 0; GetDetailFileTableID(); RecvTimer.Start(); ButtonStart.Text = "停止"; } else { ButtonStart.Text = "开始"; RecvTimer.Stop(); MainStatusText.Text = string.Format("已经停止读取:{0}", DateTime.Now.ToString()); recvthreadison = false; SaveSetting(); m_downBadList.Clear(); if (m_reader != null) { m_reader.Close(); m_reader.Dispose(); m_reader = null; } } } catch (System.Exception ex) { H31Debug.PrintLn("ButtonStart:" + ex.StackTrace); } }
/// <summary> /// 去掉标题中的网址信息 /// </summary> private string GetOneGoodString(string title) { //去掉标题中的网址信息 string res = title; try { //过滤[www.*] [bbs.*]的文件名 string pattern = @"(\[|\@|\【|\s|\(|\{)(.*)([\w-]+://?|(www|bbs)[.])([^(\]|\@|\】|\)|\})]*)(\]|\@|\】|\)|\})"; Match usermatch = Regex.Match(title, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count > 1) { res = res.Replace(usermatch.Groups[0].Value.ToString(), " "); res = res.Trim(); } //过滤[*.com]等文件名 pattern = @"(\[|\@|\【|\s|\(|\{)(.*)\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|cc|me|cm)([^(\]|\@|\】|\)|\}|\s)]*)(\]|\@|\】|\)|\}|\s)"; usermatch = Regex.Match(res, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count > 1) { res = res.Replace(usermatch.Groups[0].Value.ToString(), " "); res = res.Trim(); } //过滤www.*.com bbs.*.com文件名 pattern = @"(www|bbs)(.*)(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk|cc|me|cm)"; usermatch = Regex.Match(res, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count > 1) { res = res.Replace(usermatch.Groups[0].Value.ToString(), " "); res = res.Trim(); } //如果过滤错误只有后缀留下说明有错误,直接用原来的 if (res.Length <= 5 && res.Length < title.Length) { res = title; } } catch (System.Exception ex) { H31Debug.PrintLn(ex.Message); res = title; } return(res); }
//存储目前已经处理的配置文件 private int SaveSetting() { try { //存储读取的文件列表 if (m_readFilename.Length > 1 && m_readPos > 0) { m_fileOKList[m_readFilename] = m_readPos; } string file = Path.Combine(configDir, "Setting.txt"); StreamWriter writer = new StreamWriter(file); foreach (string key in m_fileOKList.Keys) { writer.WriteLine(key); writer.WriteLine(m_fileOKList[key].ToString()); } writer.Close(); //存储不成功的HASH列表 string file1 = Path.Combine(configDir, "BadList.txt"); StreamWriter writer1 = new StreamWriter(file1); foreach (string key in m_downBadList.Keys) { writer1.WriteLine(key); } writer1.Close(); //存储成功的HASH列表 string file2 = Path.Combine(configDir, "OKList.txt"); StreamWriter writer2 = new StreamWriter(file2); foreach (string key in m_downOKList.Keys) { writer2.WriteLine(key); } writer2.Close(); } catch (System.Exception ex) { H31Debug.PrintLn("SaveSetting:" + ex.StackTrace); } return(1); }
/// <summary> /// 搜索数据库显示出来 2013-07-16 /// </summary> private void ButtonSearch_Click(object sender, EventArgs e) { try { //TorrentFile my = new TorrentFile("1.torrent"); m_data.Rows.Clear(); int selecttype = comboBox_Type.SelectedIndex + 1; int ordertype = comboBox_OrderBy.SelectedIndex + 1; string keyword = textBox_Search.Text; if (keyword == "关键字") { keyword = ""; } int searchindex = Convert.ToInt32(textBox_SearchIndex.Text); int ishanzi = comboBox_Lanague.SelectedIndex; DataSet ds = H31SQL.GetHashPageListFromDB(selecttype, ishanzi, keyword, ordertype, searchindex, MAX_PAGE_SHOW_COUN); Application.DoEvents(); //让系统在百忙之中来响应其他事件 if (ds != null) { int cnt = ds.Tables[0].Rows.Count; for (int i = cnt - 1; i >= 0; i--) { m_data.Rows.Add((cnt - i).ToString(), ds.Tables[0].Rows[i]["ID"].ToString(), ds.Tables[0].Rows[i]["hashKey"].ToString(), ds.Tables[0].Rows[i]["recvTime"].ToString(), ds.Tables[0].Rows[i]["keyContent"].ToString(), ds.Tables[0].Rows[i]["keyType"].ToString(), ds.Tables[0].Rows[i]["recvTimes"].ToString(), ds.Tables[0].Rows[i]["fileCnt"].ToString(), FormatFileSize(Convert.ToDouble(ds.Tables[0].Rows[i]["filetotalSize"].ToString())), ds.Tables[0].Rows[i]["Detail"].ToString()); } if (MAX_PAGE_SHOW_COUN > cnt) { Button_NEXTPAGE.Enabled = false; } else { Button_NEXTPAGE.Enabled = true; } } } catch (System.Exception ex) { H31Debug.PrintLn(ex.StackTrace); } }
public byte[] DownLoadFileByHashToByte(string hashname) { byte[] res = null; try { //先检查本地有没有文件,如果有直接读取本地,没有再从网络上下载 string filename = string.Format("{0}//{1}.torrent", pathname, hashname); if (File.Exists(filename)) { System.IO.FileStream torrentFile = new System.IO.FileStream(filename, System.IO.FileMode.Open); if (torrentFile.Length > 0) { res = new byte[torrentFile.Length]; torrentFile.Read(res, 0, res.Length); torrentFile.Close(); return(res); } } //随机从前面两个网站中的一个下载,因为前面两个网站速度快些 downwebpos = GetRandomServer(); //res = DownLoadFileToSaveByte(m_strURLList[downwebpos]); //随机打乱三个网址顺序下载,防止从一个网站下载过多被封 res = DownLoadFileToSaveByte(_serverList[downwebpos].GetUrlStr(hashname), _serverList[downwebpos].Timeout); if (res == null) { downwebpos = GetRandomServer(); res = DownLoadFileToSaveByte(_serverList[downwebpos].GetUrlStr(hashname), _serverList[downwebpos].Timeout); } return(res); } catch (Exception e) { H31Debug.PrintLn(e.Message); return(null); } }
/// <summary> /// 选中一行,然后Tips显示文件列表内容 /// </summary> private void m_data_MouseClick(object sender, MouseEventArgs e) { try { DataGridView.HitTestInfo hi; hi = m_data.HitTest(e.X, e.Y); if (hi.RowIndex >= 0) { this.toolTip1.Hide(this); m_data.ClearSelection(); m_data.Rows[hi.RowIndex].Selected = true; Point mousePos = PointToClient(MousePosition);//获取鼠标当前的位置 int hashid = Convert.ToInt32(m_data.Rows[hi.RowIndex].Cells["HashID"].Value.ToString()); int tableid = Convert.ToInt32(m_data.Rows[hi.RowIndex].Cells["TableID"].Value.ToString()); int ordertype = comboBox_OrderBy.SelectedIndex + 1; int ishanzi = comboBox_Lanague.SelectedIndex; DataSet ds = H31SQL.GetHashFileDetail(tableid, hashid, ordertype * 1000 + 10 + ishanzi); Application.DoEvents(); //让系统在百忙之中来响应其他事件 string tip = ""; if (ds != null) { int cnt = ds.Tables[0].Rows.Count; for (int i = 0; i < cnt; i++) { tip = tip + ds.Tables[0].Rows[i]["filename"].ToString() + "\t" + FormatFileSize(Convert.ToDouble(ds.Tables[0].Rows[i]["filesize"].ToString())) + "\r\n"; } } this.toolTip1.Show(tip, this, mousePos);//在指定位置显示提示工具 } else { } } catch (System.Exception ex) { H31Debug.PrintLn(ex.StackTrace); } }
//判断是否是中文,如果是日文等,则存储到另外一个表中 private int ISChineseAndEnglist(string title) { try { string pattern = @"[\uac00-\ud7ff]+";//判断韩语 Match usermatch = Regex.Match(title, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count >= 1 && usermatch.Groups[0].Value.Length >= 1) { return(0); } pattern = @"[\u0800-\u4e00]+";//判断日语 usermatch = Regex.Match(title, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count >= 1 && usermatch.Groups[0].Value.Length >= 1) { return(0); } pattern = @"[\u4e00-\u9fa5]+";//判断汉字 usermatch = Regex.Match(title, pattern, RegexOptions.IgnoreCase); if (usermatch.Groups.Count >= 1 && usermatch.Groups[0].Value.Length >= 1) { return(1); } //判断英文,数字 byte[] byte_len = System.Text.Encoding.Default.GetBytes(title); if (byte_len.Length == title.Length) { return(1); } } catch (System.Exception ex) { H31Debug.PrintLn(ex.Message); } return(0); }
public int DownLoadFileByHashToFile(string hashname) { try { if (pathname == string.Empty) { string localfile = AppDomain.CurrentDomain.BaseDirectory; pathname = Path.Combine(localfile, "Torrent"); if (!Directory.Exists(pathname)) { Directory.CreateDirectory(pathname); string tmpFolder = Path.Combine(pathname, "BAD"); if (!Directory.Exists(tmpFolder)) { Directory.CreateDirectory(tmpFolder); } } } //检测子文件夹是否存在 string pathname1 = Path.Combine(pathname, hashname.Substring(hashname.Length - 2, 2)); if (!Directory.Exists(pathname1)) { Directory.CreateDirectory(pathname1); } string filename = string.Format("{0}\\{1}\\{2}.torrent", pathname, hashname.Substring(hashname.Length - 2, 2), hashname); if (File.Exists(filename)) { return(1); } m_strURLList[3] = string.Format("http://torcache.net/torrent/{0}.torrent", hashname); m_strURLList[2] = string.Format("https://zoink.it/torrent/{0}.torrent", hashname); m_strURLList[1] = string.Format("http://bt.box.n0808.com/{0}/{1}/{2}.torrent", hashname.Substring(0, 2), hashname.Substring(hashname.Length - 2, 2), hashname); m_strURLList[0] = string.Format("http://torrage.com/torrent/{0}.torrent", hashname); m_timeoutList[0] = 500; m_timeoutList[1] = 500; m_timeoutList[2] = 1000; m_timeoutList[3] = 1000; //随机从一个网址下载 //downwebpos = (downwebpos + 1) % 2; //if (DownLoadFileToSaveFile(m_strURLList[downwebpos], filename) == 1) // return 1; //随机打乱三个网址顺序下载,防止从一个网站下载过多被封 downwebpos = (downwebpos + 1); //从三种网址一一测试下载 if (DownLoadFileToSaveFile(m_strURLList[(downwebpos) % 4], filename, m_timeoutList[(downwebpos) % 4]) == 1) { return(1); } if (DownLoadFileToSaveFile(m_strURLList[(downwebpos + 1) % 4], filename, m_timeoutList[(downwebpos + 1) % 4]) == 1) { return(1); } if (DownLoadFileToSaveFile(m_strURLList[(downwebpos + 2) % 4], filename, m_timeoutList[(downwebpos + 2) % 4]) == 1) { return(1); } if (DownLoadFileToSaveFile(m_strURLList[(downwebpos + 3) % 4], filename, m_timeoutList[(downwebpos + 3) % 4]) == 1) { return(1); } return(0); } catch (Exception e) { H31Debug.PrintLn(e.Message); return(-2); } }
/// <summary> /// 下载种子文件并进行解析读取 /// </summary> private int GetDownAndReaHashDetail(string hashname, ref HASHITEM item1, ref HASHFILEITEM[] filelist) { Int32 ticktime1 = System.Environment.TickCount; try { int res1 = 0; TorrentFile myFile = null; if (checkBox_Torrent.Checked) { res1 = m_downLoad.DownLoadFileByHashToFile(hashname); Application.DoEvents(); //让系统在百忙之中来响应其他事件 if (res1 == 1) { string filename = Path.Combine(Path.Combine(Path.Combine(m_localPath, "Torrent"), hashname.Substring(hashname.Length - 2, 2)), hashname + ".torrent"); myFile = new TorrentFile(filename); Int32 ticktime2 = System.Environment.TickCount; if (myFile == null || myFile.TorrentName.Length == 0) { m_downBadList[hashname] = System.Environment.TickCount / 1000; //解析不了的文件就直接备份 if (checkBox_Torrent.Checked) { File.Move(filename, Path.Combine(Path.Combine(m_localPath, "Torrent\\BAD"), hashname + ".torrent")); LogTheAction(1, 1, hashname + "下载文件不对,删除" + "-" + (ticktime2 - ticktime1).ToString()); } return(0); } } } else { //不用存储文件,直接在内存中操作 byte[] data = m_downLoad.DownLoadFileByHashToByte(hashname); if (data != null) { res1 = 1; } myFile = new TorrentFile(data); } //如果解析成功就进行赋值返回 if (res1 == 1) { if (myFile == null || myFile.TorrentName.Length == 0 || myFile.TorrentFileInfo.Count == 0 || myFile.TorrentFileInfo.Count > MAX_FILEDETAIL_COUNT) { Int32 ticktime2 = System.Environment.TickCount; LogTheAction(1, 2, hashname + ">>下载成功,但使用失败." + "-" + (ticktime2 - ticktime1).ToString()); m_downBadList[hashname] = System.Environment.TickCount / 1000; return(0); } else { //通过文件列表对文件是哪类进行分析判断 item1.keyType = (int)GetHashFileKeyType(ref myFile); item1.fileCnt = myFile.TorrentFileInfo.Count; item1.filetotalSize = myFile.TorrentPieceLength; //提取正确的文件名字,过滤一些网址信息 item1.keyContent = GetOneGoodString(myFile.TorrentName); filelist = new HASHFILEITEM[myFile.TorrentFileInfo.Count]; for (int m = 0; m < myFile.TorrentFileInfo.Count; m++) { filelist[m].filename = myFile.TorrentFileInfo[m].Path; filelist[m].filesize = myFile.TorrentFileInfo[m].Length; } Int32 ticktime2 = System.Environment.TickCount; LogTheAction(1, 3, myFile.TorrentName + ">>下载成功:" + myFile.TorrentFileInfo.Count + "-" + (ticktime2 - ticktime1).ToString()); return(1); } } else { Int32 ticktime2 = System.Environment.TickCount; m_downBadList[hashname] = System.Environment.TickCount / 1000; LogTheAction(1, 4, hashname + ">>下载失败" + "-" + (ticktime2 - ticktime1).ToString()); } } catch (System.Exception ex) { H31Debug.PrintLn("GetHashDetail:" + ex.StackTrace); } return(0); }
/// <summary> /// 读取线程主函数 2013-06-16 /// </summary> private void GetTheDataDelegate() { if (recvthreadison) { //检测网络状态 if (!isConnected()) { LogTheAction(0, 1, "网络不通,请等待."); recvthreadison = false; return; } try { MainStatusText.Text = string.Format("开始读取:{0}", DateTime.Now.ToString()); if (m_reader == null) { int res2 = GetOneFileDataToMDB(); if (m_reader != null) { m_reader.Close(); m_reader.Dispose(); } } if (string.IsNullOrEmpty(m_readFilename)) { return; } //定位到上次文件读取的地方 m_reader = new StreamReader(Path.Combine(resourceDir, m_readFilename), Encoding.Default); long pos = m_reader.BaseStream.Seek(m_readPos, SeekOrigin.Begin); Int32 ticktime1 = System.Environment.TickCount; string[] strlist = new string[100]; string str1 = m_reader.ReadLine(); int i = 0; while (i < 100 && str1 != null) { if (str1.Length > 1) { if (str1.Length == 40) { m_readPos = m_readPos + str1.Length + 1; } else { m_readPos = m_readPos + str1.Length + 4; } strlist[i] = str1; i++; } str1 = m_reader.ReadLine(); } m_reader.Close(); m_doWorkCnt = m_doWorkCnt + i; int value1 = Convert.ToInt32(m_readPos / 100); MainProgressBar.Value = value1 > MainProgressBar.Maximum ? MainProgressBar.Maximum : value1; Int32 ticktime2 = System.Environment.TickCount; if (m_doWorkCnt % 10000 == 0) { SaveSetting(); LogTheAction(0, 1, "存储到BadList" + m_downBadList.Count.ToString() + "个成功."); } if (i == 0) { LogTheAction(0, 1, m_readFilename + "读取文件完成"); m_fileOKList[m_readFilename] = m_readPos; int res2 = GetOneFileDataToMDB(); recvthreadison = false; return; } //开始处理那么多条HASH for (int k = 0; k < i && recvthreadison; k++) { HASHITEM item1 = new HASHITEM(); //通过正则表达式获取几种不同类型的HASH文件 int res11 = GetHashLineContent(strlist[k], ref item1); if (res11 == 1) { if (m_downBadList.ContainsKey(item1.hashKey)) { continue; } Int32 ticktime3 = System.Environment.TickCount; int detailTableid = 0; int keytype = 0; int ishanzi = 0; bool findexist = false; //先检查本地HASH列表里面是否有,没有就查询数据库里面是否有这一条,如果没有则需要插入,如果有,则需要直接更新次数和日志表 if (m_downOKList.ContainsKey(item1.hashKey)) { if (m_downOKList[item1.hashKey] > 0) { findexist = true; keytype = m_downOKList[item1.hashKey] / 1000; detailTableid = m_downOKList[item1.hashKey] / 10 % 100; ishanzi = m_downOKList[item1.hashKey] % 10; } } if (findexist == false) { int res2 = H31SQL.CheckHashItemExist(item1.hashKey, ref keytype, ref detailTableid, ref ishanzi); } if (detailTableid > 0) { H31SQL.UpdateHashCount(keytype, item1, detailTableid, ishanzi); m_downOKList[item1.hashKey] = keytype * 1000 + detailTableid * 10 + ishanzi; Int32 ticktime4 = System.Environment.TickCount; //LogTheAction(2, 2, ">>>>" + item1.hashKey + "更新到数据库" + item1.keyType.ToString() + "成功" + keytype.ToString() + "TIME:" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime4 - ticktime3).ToString()); } else { Int32 ticktime4 = System.Environment.TickCount; HASHFILEITEM[] filelist = null; //首先去下载种子文件并进行读取 int res = GetDownAndReaHashDetail(item1.hashKey, ref item1, ref filelist); if (res == 0 || (res == 1 && item1.keyContent.Contains("�"))) { continue; } Int32 ticktime5 = System.Environment.TickCount; if (filelist != null && res == 1 && filelist.Length <= MAX_FILEDETAIL_COUNT) { item1.keyWords = ""; ishanzi = ISChineseAndEnglist(item1.keyContent); //插入新的一条HASH数据 int hashID = H31SQL.AddNewHash((HASHTYPE)item1.keyType, item1, m_nowTableID, ishanzi); Int32 ticktime6 = System.Environment.TickCount; if (hashID > 0) { m_downOKList[item1.hashKey] = item1.keyType * 1000 + m_nowTableID * 10 + ishanzi; int real_add = 0; //插入文件列表 for (int m = 0; m < filelist.Length; m++) { filelist[m].hashID = hashID; filelist[m].recvTime = item1.recvTime; //过滤一些没有用的介绍性文件 if (filelist[m].filesize == 0 || (filelist[m].filename).ToLower().Contains("Thumbs.db")) { continue; } int dotpos = filelist[m].filename.LastIndexOf('.'); string str2 = filelist[m].filename.Substring(dotpos + 1, filelist[m].filename.Length - dotpos - 1); if (str2.ToLower() == "url") { continue; } string str3 = filelist[m].filename.Substring(filelist[m].filename.Length - 1, 1); if (str3 == "_") { continue; } //如果是视频就直接不需要一些文件 if ((item1.keyType == (int)HASHTYPE.MOVIE || filelist.Length > 30) && (str2.ToLower() == "mht" || str2.ToLower() == "html" || str2.ToLower() == "htm" || str2.ToLower() == "txt")) { continue; } //过滤文件名字中的网址 filelist[m].filename = GetOneGoodString(filelist[m].filename); int resid = H31SQL.AddNewHashDetail(item1.keyType * 1000 + 10 + ishanzi, filelist[m], m_nowTableID); real_add = real_add + 1; } m_nowTableCount = m_nowTableCount + real_add; GetDetailFileTableID(); Int32 ticktime7 = System.Environment.TickCount; LogTheAction(2, 1, ">>>>" + item1.keyContent + "插入数据库" + (item1.keyType * 1000 + 10 + ishanzi).ToString() + "成功" + filelist.Length.ToString() + "个文件 TIME:" + (ticktime2 - ticktime1).ToString() + "-" + (ticktime4 - ticktime3).ToString() + "-" + (ticktime5 - ticktime4).ToString() + "-" + (ticktime6 - ticktime5).ToString() + "-" + (ticktime7 - ticktime6).ToString()); } } } Application.DoEvents(); //让系统在百忙之中来响应其他事件 } } } catch (System.Exception ex) { H31Debug.PrintLn("GetTheDataDelegate:" + ex.StackTrace); } } else { Thread.Sleep(100); isFirstTimeCheck = true; } this.Text = "已经读取" + m_readFilename + ":" + m_doWorkCnt.ToString() + "行"; MainStatusText.Text = string.Format("读取完成:{0}", DateTime.Now.ToString()); recvthreadison = false; }