Exemplo n.º 1
0
        /// <summary>
        /// 导入收藏夹
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public ContentResult ImportBookmarks(string files)
        {
            IList <UploadedItem> uploads = UploadedManager.Parse(files);

            return(TryAction(r =>
            {
                int count = 0;
                int successCount = 0;
                int errorCount = 0;
                foreach (UploadedItem item in uploads)
                {
                    string path = Server.MapPath(UrlIconPath);
                    var fullFile = Server.MapPath(item.FilePath);
                    string fileContent = FileHelper.GetFileContent(fullFile, Encoding.GetEncoding("utf-8"));
                    List <string> listItems = DataMiningHelper.GetItemsByTab(fileContent, "<A", "</A>");
                    count += listItems.Count;
                    foreach (string listItem in listItems)
                    {
                        try
                        {
                            IDictionary <string, string> attrs = TagHelper.ParseAttributes(listItem);
                            string url = attrs["HREF"];
                            if (urlsRepository.FindAll().Where(o => o.Url == url).Count() <= 0)
                            {
                                //不存在则新增
                                string encoding = "utf-8";
                                // string html = DataMining.GetHtmlAutoEncode(url, out encoding);
                                Uri uri = new Uri(url);
                                if (attrs.ContainsKey("ICON") && !string.IsNullOrEmpty(attrs["ICON"]))
                                {
                                    string base64 = attrs["ICON"].Replace("data:image/png;base64,", "");
                                    ImgHelper.SaveBase64(base64, Path.Combine(path, uri.Host + ".png"));
                                }
                                else if (attrs.ContainsKey("ICON_URI") && !string.IsNullOrEmpty(attrs["ICON_URI"]))
                                {
                                    DownloadUrlIcon(attrs["ICON_URI"], uri.Host);
                                }
                                UrlsEntity entity = new UrlsEntity()
                                {
                                    Url = url,
                                    CreateTime = DateTime.Now,
                                    Encoding = encoding,
                                    IconName = uri.Host,
                                    IconUrl = string.Format("http://{0}/favicon.ico", uri.Host),
                                    Title = attrs["InnerText"],
                                    Remark = attrs["ADD_DATE"]
                                };
                                urlsRepository.Add(entity);
                            }
                            successCount++;
                        }
                        catch (Exception ex)
                        {
                            errorCount++;
                        }
                    }
                }
                r.Msg = string.Format("导出完毕,总共【{0}】项,成功【{1}】,失败【{2}】", count, successCount, errorCount);
            }));
        }
Exemplo n.º 2
0
 /// <summary>
 /// 获取网页标题
 /// </summary>
 /// <param name="url"></param>
 /// <returns></returns>
 public ContentResult GetPageTitle(string url)
 {
     return(TryAction(r =>
     {
         string encoding = "utf-8";
         string html = DataMiningHelper.GetHtmlAutoEncode(url, out encoding);
         string title = DataMiningHelper.GetItemsByTab(html, "<title>", "</title>")[0];
         r.Add("title", title);
         r.Add("encoding", encoding);
     }, "获取网页标题"));
 }