public static void insertTorrent(HisTorrent his) { string sql = string.Format(insertTorrentSql, his.File.Replace("'", "''"), his.Size, his.Path.Replace("'", "''"), his.Ext, his.Md5); using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand sc = new SqlCommand(sql, conn); sc.ExecuteNonQuery(); } }
bool check(HisTorrent trt, bool ifCheckHis) { bool flag = true; if (trt.Size > 15 * 1024 * 1024 && !trt.File.ToLower().Contains("sample")) { if (fileDic.ContainsKey(Tool.filterName(trt.File))) { Tool.moveFile("duplicate", trt.Path); return(false); } if (ifCheckHis) { HisTorrent t; try { t = dic[Tool.filterName(trt.File)]; } catch (KeyNotFoundException e) { try { t = torrentNameDic[trt.FilteredFileName]; } catch (KeyNotFoundException ex) { t = null; } } if (t != null) { if (t.Size >= trt.Size || t.CreateTime < startTime) { Tool.moveFile("duplicate", trt.Path); flag = false; } else { Tool.moveFile("duplicate", t.Path); } } return(flag); } else { return(true); } } else { return(true); } }
public static int checkTorrent(HisTorrent his) { int res = 0; string sql = string.Format(checkTorrentSql, his.File.Replace("'", "''").ToLower()); using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand sc = new SqlCommand(sql, conn); res = Convert.ToInt32(sc.ExecuteScalar()); } return(res); }
public static int checkFiles(HisTorrent his, string filter) { int res = 0; string sql = string.Format(checkFilesSql, his.File.Replace("'", "''") + his.Ext); using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand sc = new SqlCommand(sql, conn); res = Convert.ToInt32(sc.ExecuteScalar()); } if (res > 0) { Console.WriteLine(his.Path + " already downloaded"); } return(res); }
public static Dictionary <string, HisTorrent> getList(string filterStr) { string[] strs = filterStr.Split(','); string filterStr1 = " REPLACE(LOWER([file]),'" + strs[0] + "','') "; for (int i = 1; i < strs.Length; i++) { filterStr1 = "REPLACE(" + filterStr1 + ",'" + strs[i] + "','') "; } Dictionary <string, HisTorrent> dic = new Dictionary <string, HisTorrent>(); string sql = "select " + filterStr1 + " as [file] ,createtime,[path],[size],ext,md5 from his where size>100*1024*1024 and DATEDIFF(M,createtime,GETDATE())<=500 order by createtime"; using (SqlConnection conn = new SqlConnection(connstr)) { conn.Open(); SqlCommand sc = new SqlCommand(sql, conn); sc.CommandTimeout = 1800; SqlDataReader reader = sc.ExecuteReader(); while (reader.Read()) { HisTorrent t = new HisTorrent(); t.File = reader["file"].ToString(); t.CreateTime = Convert.ToDateTime(reader["createtime"]); t.Ext = reader["ext"].ToString(); t.Path = reader["path"].ToString(); t.Size = Convert.ToInt64(reader["size"].ToString()); t.Md5 = reader["md5"].ToString(); try { dic.Add(t.File, t); } catch (ArgumentException e) { dic.Remove(t.File); dic.Add(t.File, t); } } return(dic); } }
public void process(string directoryStr, bool ifCheckHis) { string md5; if (md5Set.Count == 0) { getList(); } startTime = DateTime.Now; String[] path = Directory.GetFiles(directoryStr, "*", SearchOption.TopDirectoryOnly); foreach (String p in path) { if (p.EndsWith(".torrent")) { if (Path.GetFileNameWithoutExtension(p).ToLower().Contains(".sd.")) { Tool.moveFile("SD", p); continue; } md5 = GetMd5(p); if (md5Set.Contains(md5)) { Tool.moveFile("Md5Duplicate", p); continue; } if (!Path.GetFileNameWithoutExtension(p).Replace("rarbg.to", "").Contains(".")) { Tool.moveFile("invalid", p); } BDict torrentFile = null; bool hasBigFile = false; try { torrentFile = BencodingUtils.DecodeFile(p) as BDict; } catch (Exception e) { Tool.moveFile("decodeErr", p); } if (torrentFile != null) { bool flag = true; BList b; List <HisTorrent> listTorrent = new List <HisTorrent>(); if ((torrentFile["info"] as BDict).ContainsKey("files")) { b = (BList)(torrentFile["info"] as BDict)["files"]; for (int i = 0; i < b.Count; i++) { BDict bd = (BDict)b[i]; long length = ((BInt)bd["length"]).Value; if (length > 25 * 1024 * 1024) { hasBigFile = true; } BList list = (BList)bd["path"]; string s = ((BString)list[list.Count - 1]).Value; HisTorrent trt = new HisTorrent(); trt.CreateTime = DateTime.Now; trt.Path = p; trt.Size = length; if (s.LastIndexOf('.') > 0) { trt.File = s.Substring(0, s.LastIndexOf('.')); trt.Ext = s.Substring(s.LastIndexOf('.')); } else { trt.File = s; } listTorrent.Add(trt); if (!check(trt, ifCheckHis)) { flag = false; break; } } } else { hasBigFile = true; HisTorrent trt = new HisTorrent(); trt.CreateTime = DateTime.Now; trt.Path = p; trt.Size = ((BInt)(torrentFile["info"] as BDict)["length"]).Value; string name = ((BString)(torrentFile["info"] as BDict)["name"]).Value; if (name.LastIndexOf('.') > 0) { trt.File = name.Substring(0, name.LastIndexOf('.')); trt.Ext = name.Substring(name.LastIndexOf('.')); } else { trt.File = name; } listTorrent.Add(trt); if (!check(trt, ifCheckHis)) { flag = false; } } if (!hasBigFile) { Tool.moveFile("noBigFile", p); } if (flag && hasBigFile) { foreach (HisTorrent his in listTorrent) { if (his.Size > 60 * 1024 * 1024) { if (ifCheckHis) { his.Md5 = md5; DBHelper.insertTorrent(his); } try { md5Set.Add(md5); dic.Add(Tool.filterName(his.File), his); torrentNameDic.Add(his.FilteredFileName, his); }catch (ArgumentException e) { dic.Remove(Tool.filterName(his.File)); dic.Add(Tool.filterName(his.File), his); torrentNameDic.Remove(his.FilteredFileName); torrentNameDic.Add(his.FilteredFileName, his); } } } } } else { Tool.moveFile("decodeErr", p); } } } }