/// <summary> /// 创建用户状态服务 /// </summary> public UserCookieService() { String dataDir = DataCenter.GetUserPath() + "\\data"; if (!FCFile.isDirectoryExist(dataDir)) { FCFile.createDirectory(dataDir); } String dataBasePath = DataCenter.GetUserPath() + "\\data\\usercookies.db"; m_connectStr = "Data Source = " + dataBasePath; if (!FCFile.isFileExist(dataBasePath)) { CreateTable(); } }
/// <summary> /// 用户数据存储路径 /// </summary> /// <returns>程序路径</returns> public static String GetUserPath() { String userPath = Environment.GetEnvironmentVariable("LOCALAPPDATA"); if (!FCFile.isDirectoryExist(userPath)) { userPath = GetAppPath(); } else { userPath += "\\idesigner"; if (!FCFile.isDirectoryExist(userPath)) { FCFile.createDirectory(userPath); } } return(userPath); }
/// <summary> /// 开始服务 /// </summary> public override void start() { base.start(); String dir = DataCenter.getDataPath() + "\\sinanews"; if (!FCFile.isDirectoryExist(dir)) { FCFile.createDirectory(dir); } List <Security> securities = DataCenter.SecurityService.getSecurities(); int securitiesSize = securities.Count; List <WorkDataInfo> dataInfos = new List <WorkDataInfo>(); for (int i = 0; i < securitiesSize; i++) { SecurityDataInfo dataInfo = new SecurityDataInfo(); dataInfo.m_id = i; dataInfo.m_security = securities[i]; dataInfos.Add(dataInfo); } m_workThread.startWork(dataInfos); }
/// <summary> /// 工作中 /// </summary> /// <param name="dataInfo">信息</param> /// <returns>状态</returns> public override int onWorking(WorkDataInfo dataInfo) { Console.WriteLine(dataInfo.m_id.ToString()); SecurityDataInfo securityDataInfo = dataInfo as SecurityDataInfo; String url = String.Format(m_listUrl, FCStrEx.ConvertDBCodeToSinaCode(securityDataInfo.m_security.m_code)); Log = String.Format("下载:{0}", securityDataInfo.m_security.m_code); String html = DataCenter.getHttpWebRequest(url, "GB2312"); if (html != null && html.Length > 0) { NewsInfo newsInfo = new NewsInfo("sinanews"); newsInfo.m_code = securityDataInfo.m_security.m_code; String dir = newsInfo.getDirectory(); if (!FCFile.isDirectoryExist(dir)) { FCFile.createDirectory(dir); } String identifier = "<div class=\"datelist\">"; int pos = html.IndexOf(identifier); if (pos != -1) { html = html.Substring(pos + identifier.Length); html = html.Substring(0, html.IndexOf("</ul>")); html = html.Replace("<ul>\r\n\t\t\t", "").Replace(" ", " "); String[] strs = html.Split(new String[] { "<br>" }, StringSplitOptions.RemoveEmptyEntries); int strsSize = strs.Length; for (int i = 0; i < strsSize; i++) { try { newsInfo.m_content = ""; String str = strs[i]; String[] subStrs = str.Split(new String[] { "<a target='_blank' href='" }, StringSplitOptions.RemoveEmptyEntries); if (subStrs.Length >= 2) { newsInfo.m_time = subStrs[0].Trim(); String[] sunStrs = subStrs[1].Split(new String[] { "'>" }, StringSplitOptions.RemoveEmptyEntries); newsInfo.m_url = sunStrs[0]; newsInfo.m_title = sunStrs[1].Replace("</a>", ""); String fileName = newsInfo.getFileName(); if (!FCFile.isFileExist(fileName)) { Log = String.Format("下载:{0}", securityDataInfo.m_security.m_code); String contentHtml = DataCenter.getHttpWebRequest(newsInfo.m_url, "UTF-8"); String sIdentifier = "<!-- 原始正文start -->", eIdentifier = "<!-- 原始正文end -->"; int sPos = contentHtml.IndexOf(sIdentifier); if (sPos != -1) { String content = contentHtml.Substring(sPos + sIdentifier.Length); int ePos = content.IndexOf(eIdentifier); newsInfo.m_content = content.Substring(0, ePos); FCFile.write(fileName, newsInfo.ToString()); } } } } catch (Exception ex) { } } } } return(1); }
/** * 输出日志 */ public override void log(int logType, String message) { if (checkOut(logType)) { String type = null; if (logType == 1) { type = "Debug"; } else if (logType == 2) { type = "Info"; } else if (logType == 3) { type = "Warn"; } else if (logType == 4) { type = "Error"; } else if (logType == 5) { type = "Fatal"; } //yyyyMMdd String dateTime = getNowDate(m_fileAppenderConfig.m_datePattern); message = String.Format("{0} {1} {2}\r\n", dateTime, type, message); String logFile = m_fileAppenderConfig.m_logFile; if (logFile.Length == 0) { return; } String separator = LogService.FILESEPARATOR; String date = getNowDate("yyyyMMdd"); int np = logFile.LastIndexOf(date); if (np < 0) { String logDir = LogService.getAppPath() + "\\log\\" + date; FCFile.createDirectory(logDir); int pos = logFile.LastIndexOf(separator); String logFileName = logFile.Substring(pos, logFile.Length - pos + 1); String newLogFile = logDir + separator + logFileName; m_fileAppenderConfig.m_logFile = newLogFile; logFile = newLogFile; } if (FCFile.isFileExist(logFile)) { FileInfo file = new FileInfo(logFile); long len = file.Length; if (len >= m_maxFileSize) { String logDir = LogService.getAppPath() + "\\log\\" + date; ArrayList <String> files = new ArrayList <String>(); FCFile.getFiles(logDir, files); int count = files.size(); files.clear(); int pos = logFile.LastIndexOf(separator); int pos2 = logFile.LastIndexOf("."); String logFileName = logFile.Substring(pos, pos2 - 1 - pos + 1); String newFileName = String.Format("{0}\\{1}.{2}.log", logDir, logFileName, count); FileInfo oldFile = new FileInfo(logFile); FileInfo newFile = new FileInfo(newFileName); oldFile.MoveTo(newFile.FullName); } } FCFile.append(logFile, message); } }