/// <summary> ///根据列表页的地址采集页面,并返回提示数据 /// </summary> /// <param name="project"></param> /// <param name="listPageUri"></param> /// <returns></returns> public virtual string Invoke_ListPage(Project project, string listPageUri) { string returnData; int i = 0; object obj = String.Empty; project.UseMultiThread = true; project.InvokeList(listPageUri, dp => { lock (obj) { ++i; #if DEBUG saveLog(String.Format("采集到第{0}条->{1}", i, dp["title"])); #endif } }); returnData = String.Format("任务总数:{0},成功:{1},失败:{2}", project.State.TotalCount, project.State.SuccessCount, project.State.FailCount); //重置计数 project.ResetState(); return returnData; }
public override string Invoke_ListPage(Project project, string listPageUri) { project.UseMultiThread = true; //重置计数 project.ResetState(); int categoryId = int.Parse(request.Form["category"]); if (categoryId == -1) return "<strong>错误,请先选择采集目标栏目!<strong><br />"; project.InvokeList(listPageUri, GetDataPackHandler(categoryId)); return String.Format("任务总数:{0},成功:{1},失败:{2}", project.State.TotalCount, project.State.SuccessCount, project.State.FailCount); }
/// <summary> /// ������Ŀ /// </summary> /// <param name="project"></param> /// <returns></returns> public bool CreateProject(Project project) { XmlNode newPrj, propertyNode; XmlDocument xd = new XmlDocument(); xd.Load(configFilePath); XmlNodeList xnodelist = xd.SelectNodes(String.Format("/config/projects/project[@id=\"{0}\"]", project.Id)); if (xnodelist.Count != 0) return false; XmlNode projectsNode = xd.SelectSingleNode("/config/projects"); XmlAttribute xa; newPrj = xd.CreateElement("project"); xa = xd.CreateAttribute("id"); xa.Value = project.Id; newPrj.Attributes.Append(xa); xa = xd.CreateAttribute("name"); xa.Value = project.Name; newPrj.Attributes.Append(xa); xa = xd.CreateAttribute("encoding"); xa.Value = project.RequestEncoding; newPrj.Attributes.Append(xa); //�б��ַ���� propertyNode = xd.CreateElement("listUriRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.ListUriRule)); newPrj.AppendChild(propertyNode); //�б����� propertyNode = xd.CreateElement("listBlockRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.ListBlockRule)); newPrj.AppendChild(propertyNode); //ҳ����� propertyNode = xd.CreateElement("pageUriRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.PageUriRule)); newPrj.AppendChild(propertyNode); //���˴ʹ��� propertyNode = xd.CreateElement("filterWordsRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.FilterWordsRule)); newPrj.AppendChild(propertyNode); //�������Թ��� propertyNode = xd.CreateElement("propertyRules"); XmlNode pn; foreach (string pname in project.Rules) { pn = xd.CreateElement("add"); //����������� xa = xd.CreateAttribute("name"); xa.Value = pname; pn.Attributes.Append(xa); pn.AppendChild(xd.CreateCDataSection(project.Rules[pname])); propertyNode.AppendChild(pn); } newPrj.AppendChild(propertyNode); projectsNode.AppendChild(newPrj); xd.Save(configFilePath); return true; }
/// <summary> /// ������Ŀ /// </summary> /// <param name="projectId"></param> /// <param name="project"></param> /// <returns></returns> public bool SaveProject(string projectId, Project project) { XmlDocument xd = new XmlDocument(); xd.Load(configFilePath); XmlNode projectNode, propertyNode; //��ȡ��Ŀ�ڵ� projectNode = xd.SelectSingleNode(String.Format(String.Intern("/config/projects/project[@id=\"{0}\"]"), projectId)); //�ڵ�Ϊ�գ���false if (projectNode == null) return false; //�µı���Ѿ������� if (projectId != project.Id && xd.SelectSingleNode(String.Format(String.Intern("/config/projects/project[@id=\"{0}\"]"), project.Id)) != null) return false; projectNode.Attributes["id"].Value = project.Id; projectNode.Attributes["name"].Value = project.Name; projectNode.Attributes["encoding"].Value = project.RequestEncoding; //�Ƴ��ڵ� projectNode.RemoveChild(projectNode["listUriRule"]); projectNode.RemoveChild(projectNode["listBlockRule"]); projectNode.RemoveChild(projectNode["pageUriRule"]); projectNode.RemoveChild(projectNode["filterWordsRule"]); projectNode.RemoveChild(projectNode["propertyRules"]); /*********** ������ӽڵ� ***************/ //�б��ַ���� propertyNode = xd.CreateElement("listUriRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.ListUriRule)); projectNode.AppendChild(propertyNode); //�б����� propertyNode = xd.CreateElement("listBlockRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.ListBlockRule)); projectNode.AppendChild(propertyNode); //ҳ����� propertyNode = xd.CreateElement("pageUriRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.PageUriRule)); projectNode.AppendChild(propertyNode); //���˴ʹ��� propertyNode = xd.CreateElement("filterWordsRule"); propertyNode.AppendChild(xd.CreateCDataSection(project.FilterWordsRule)); projectNode.AppendChild(propertyNode); //�������Թ��� propertyNode = xd.CreateElement("propertyRules"); XmlNode pn; XmlAttribute xa; foreach (string pname in project.Rules) { pn = xd.CreateElement("add"); //����������� xa = xd.CreateAttribute("name"); xa.Value = pname; pn.Attributes.Append(xa); pn.AppendChild(xd.CreateCDataSection(project.Rules[pname])); propertyNode.AppendChild(pn); } projectNode.AppendChild(propertyNode); //���� xd.Save(configFilePath); return true; }
/// <summary> /// �Ƴ���Ŀ /// </summary> /// <param name="project"></param> /// <returns></returns> public bool RemoveProject(Project project) { XmlDocument xd = new XmlDocument(); xd.Load(configFilePath); XmlNode prjsNode = xd.SelectSingleNode("/config/projects"); XmlNode prjNode = xd.SelectSingleNode(String.Format("/config/projects/project[@id=\"{0}\"]", project.Id)); if (prjNode == null) return false; prjsNode.RemoveChild(prjNode); xd.Save(configFilePath); return true; }
public Project[] GetProjects() { if (projects == null) { Project pro; XmlNodeList propertyNodes; //���������ļ� XmlDocument xd = new XmlDocument(); xd.Load(configFilePath); //��ȡ��Ŀ�����б� XmlNodeList projectList = xd.SelectNodes("/config/projects/project"); projects = new Project[projectList.Count]; int i = 0; foreach (XmlNode node in projectList) { //������Ŀ��Ϊ��Ŀ�����Թ��� pro = new Project(); pro.Rules = new PropertyRule(); pro.Id = node.Attributes["id"].Value; pro.Name = node.Attributes["name"].Value; pro.RequestEncoding = node.Attributes["encoding"].Value; pro.ListUriRule = node["listUriRule"].InnerText; pro.ListBlockRule = node["listBlockRule"].InnerText; pro.PageUriRule = node["pageUriRule"].InnerText; pro.FilterWordsRule = node["filterWordsRule"].InnerText; propertyNodes = xd.SelectNodes(String.Format("/config/projects/project[@id=\"{0}\"]/propertyRules/add", pro.Id)); foreach (XmlNode pnode in propertyNodes) { pro.Rules.Add(pnode.Attributes["name"].Value, pnode.InnerText); } projects[i] = pro; ++i; } } return projects; }
protected string Edit_Post() { string projectId = request.QueryString["projectId"]; bool result; //编辑项目是否成功 Project project = new Project(); project.Rules = new PropertyRule(); string id = request.Form["id"], name = request.Form["name"], encoding = request.Form["encoding"], listRule = request.Form["listRule"], blockRule = request.Form["listBlockRule"], pageRule = request.Form["pageRule"], filterRule = request.Form["filterWordsRule"]; #if DEBUG response.Write(HttpContext.Current.Server.HtmlEncode(String.Format("ID:{0}<br />Name:{1}\r\nListRule:{2}\r\nListBlockRule:{3}\r\nPageRule:{4}\r\nFilterRule:{5}\r\nencoding:{6}", id, name, listRule, blockRule, pageRule, filterRule, encoding))); response.Write("<br />"); #endif if (String.IsNullOrEmpty(id) || String.IsNullOrEmpty(name)) { return "<script>alert('编号或名称不能为空!');</script>"; } project.Id = id; project.Name = name; project.RequestEncoding = encoding; project.ListUriRule = listRule; project.ListBlockRule = blockRule; project.PageUriRule = pageRule; project.FilterWordsRule = filterRule; //添加属性并赋值 //客户端属性与规则匹配:p1 <-> r1 Regex propertyNameRegex = new Regex("^p(\\d+)$"); string propertyIndex; //属性编号 foreach (string key in request.Form) { if (propertyNameRegex.IsMatch(key)) { propertyIndex = propertyNameRegex.Match(key).Groups[1].Value; //如果值不为空,则添加属性 if (request.Form[key] != String.Empty) { project.Rules.Add(request.Form[key], request.Form["r" + propertyIndex]); } } } /* //输出添加到的属性 foreach (string key in project.Rules) { response.Write(HttpContext.Current.Server.HtmlEncode(key + "->" + project.Rules[key]+"<br />")); } */ result = this.director.SaveProject(projectId, project); //清除项目缓存 this.director.ClearProjects(); return result ? "<script>window.parent.tip('修改成功!');</script>" : "<script>window.parent.tip('项目编号已存在!');</script>"; }
/// <summary> /// 采集单页,并返回提示数据 /// </summary> /// <param name="project"></param> /// <param name="pageUri"></param> public virtual string Invoke_SinglePage(Project project, string pageUri) { project.InvokeSingle(pageUri, dp => { #if DEBUG saveLog("\r\n----------------------------------------\r\n标题:"+dp["title"] + "<br />\r\n内容:" + dp["content"]+"\r\n"); #endif }); //重置计数 project.ResetState(); return null; }