示例#1
0
        public static void GetContentFromRSS(string domain, string rss, int menuID, string gettag, string getclass, string deltag, string delclass)
        {
            try
            {
                var reader = new XmlTextReader(rss);
                var ds     = new DataSet();
                try { ds.ReadXml(reader); }
                catch { }

                for (int i = 0; ds.Tables["item"] != null && i < ds.Tables["item"].Rows.Count; i++)
                {
                    try
                    {
                        string pubdate = ds.Tables["item"].Rows[i]["pubDate"].ToString();

                        string[] formats =
                        {
                            "M/d/yyyy h:mm:ss tt",            "M/d/yyyy h:mm tt",
                            "MM/dd/yyyy hh:mm:ss",            "M/d/yyyy h:mm:ss",
                            "M/d/yyyy hh:mm tt",              "M/d/yyyy hh tt",
                            "M/d/yyyy h:mm",                  "M/d/yyyy h:mm",
                            "MM/dd/yyyy hh:mm",               "M/dd/yyyy hh:mm",
                            "yyyy/M/d h:mm:ss tt",            "yyyy/M/d h:mm tt",
                            "yyyy/MM/dd hh:mm:ss",            "yyyy/MM/dd h:mm:ss",
                            "yyyy/M/d hh:mm tt",              "yyyy/M/d hh tt",
                            "yyyy/M/d h:mm",                  "yyyy/M/d h:mm",
                            "yyyy/MM/dd hh:mm",               "yyyy/MM/dd hh:mm",
                            "M-d-yyyy h:mm:ss tt",            "M-d-yyyy h:mm tt",
                            "MM-dd-yyyy hh:mm:ss",            "M-d-yyyy h:mm:ss",
                            "M-d-yyyy hh:mm tt",              "M-d-yyyy hh tt",
                            "M-d-yyyy h:mm",                  "M-d-yyyy h:mm",
                            "MM-dd-yyyy hh:mm",               "M-dd-yyyy hh:mm",
                            "yyyy-M-d h:mm:ss tt",            "yyyy-M-d h:mm tt",
                            "yyyy-MM-dd hh:mm:ss",            "yyyy-MM-dd h:mm:ss",           "yyyy-MM-dd HH:mm:ss",            "yyyy-MM-dd H:mm:ss",
                            "yyyy-M-d hh:mm tt",              "yyyy-M-d hh tt",
                            "yyyy-M-d h:mm",                  "yyyy-M-d h:mm",
                            "yyyy-MM-dd hh:mm",               "yyyy-MM-dd hh:mm",

                            "MM/dd/yyyy HH:mm:ss",
                            "M/d/yyyy HH:mm tt",              "M/d/yyyy HH tt",
                            "MM/dd/yyyy HH:mm",               "M/dd/yyyy HH:mm",
                            "yyyy/MM/dd HH:mm:ss",
                            "yyyy/M/d HH:mm tt",              "yyyy/M/d HH tt",
                            "yyyy/MM/dd HH:mm",               "yyyy/MM/dd HH:mm",
                            "MM-dd-yyyy HH:mm:ss",
                            "M-d-yyyy HH:mm tt",              "M-d-yyyy HH tt",
                            "MM-dd-yyyy HH:mm",               "M-dd-yyyy HH:mm",              "yyyy-M-d H:mm:ss tt",            "yyyy-M-d H:mm tt",
                            "M-d-yyyy H:mm",                  "M-d-yyyy H:mm",
                            "M-d-yyyy H:mm:ss tt",            "M-d-yyyy H:mm tt",             "M-d-yyyy H:mm:ss",
                            "yyyy/M/d H:mm",                  "yyyy/M/d H:mm",
                            "yyyy/M/d H:mm:ss tt",            "yyyy/M/d H:mm tt",             "yyyy/MM/dd H:mm:ss",
                            "M/d/yyyy H:mm",                  "M/d/yyyy H:mm",                "M/d/yyyy H:mm:ss",               "M/d/yyyy H:mm:ss tt","M/d/yyyy H:mm tt",
                            "yyyy-M-d HH:mm tt",              "yyyy-M-d HH tt",
                            "yyyy-M-d H:mm",                  "yyyy-M-d H:mm",
                            "yyyy-MM-dd HH:mm",               "yyyy-MM-dd HH:mm",

                            "dddd, MMMM dd yyyy",             "dddd, MMMM dd, yyyy h:mm tt",
                            "dddd, MMMM dd, yyyy h:mm:ss tt", "MMMM dd",
                            "MMMM, yyyy",                     "ddd, dd MMM yyyy HH:mm:ss GMT","ddd, dd MMM yyyy HH:mm:ss zzz",
                            "yyyy-MM-dd HH:mm:ssZ",
                            "MM/dd/yyyy HH:mm:ss GMT",
                            "ddd, dd MMM yyyy HH:mm:ss GMTZ", //"Mon, 20 Mar 2017 21:56:05 GMT+7"
                            "ddd, dd MMM yyyy HH':'mm':'ss 'GMT+7'"
                        };
                        DateTime published = DateTime.Now;

                        try
                        {
                            //published = DateTime.ParseExact(pubdate, formats,
                            //    CultureInfo.CurrentCulture,
                            //    DateTimeStyles.None);
                            published = DateTime.ParseExact(pubdate, formats,
                                                            CultureInfo.CurrentUICulture,
                                                            DateTimeStyles.None);
                        }
                        catch (FormatException e)
                        {
                            continue;
                        }
                        //if (published.ToShortDateString() == DateTime.Now.ToShortDateString())
                        //{
                        string url         = ds.Tables["item"].Rows[i]["link"].ToString().Replace(System.Environment.NewLine, " ");
                        string description = ds.Tables["item"].Rows[i]["description"].ToString();


                        string img = string.Empty, des = string.Empty;


                        if (description.Contains("<img"))
                        {
                            int start = description.IndexOf("<img");
                            int end   = description.IndexOf("</a>");
                            if (start <= end)
                            {
                                img = description.Substring(start, description.Length - start - 1);
                            }
                        }

                        string matchString = Regex.Match(img, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
                        if (string.IsNullOrEmpty(matchString) && description.Contains("<img"))
                        {
                            int start = description.IndexOf("<img");
                            int end   = description.IndexOf("/>");
                            if (start <= end)
                            {
                                img = description.Substring(start, description.Length - start - 1);
                            }
                            matchString = Regex.Match(img, "<img.+?src=[\"'](.+?)[\"'].+?>", RegexOptions.IgnoreCase).Groups[1].Value;
                        }
                        //if (description.Contains("<img"))
                        //{
                        //    description = description.Replace(matchString, "");
                        //}
                        if (description.Contains("<a"))
                        {
                            int start = description.IndexOf("<a");
                            int end   = description.IndexOf("</a>");
                            if (start <= end)
                            {
                                des = description.Substring(start, end - start + 4);
                            }
                            description = description.Replace(des, "").Trim();
                        }
                        if (description.Contains("<img"))
                        {
                            int start = description.IndexOf("<img");
                            int end   = description.IndexOf("/>");
                            if (start <= end)
                            {
                                des = description.Substring(start, end - start + 2);
                            }
                            description = description.Replace(des, "").Trim();
                        }
                        string title = ds.Tables["item"].Rows[i]["title"].ToString();
                        if (CheckNews(Data.GetCode(title), title) == false)
                        {
                            var entity = new ModRSSEntity
                            {
                                Name       = title,
                                Code       = Data.GetCode(title),
                                Url        = url,
                                Published  = published,
                                NgayLayTin = DateTime.Now,
                                Nguon      = domain,
                                File       = matchString,
                                Summary    = description,
                                Content    = GetContentFromURL(url, gettag, getclass, deltag, delclass),
                                MenuID     = menuID,
                                Activity   = false
                            };
                            ModRSSService.Instance.Save(entity);
                        }
                        //}
                    }
                    catch (Exception ex)
                    {
                        continue;
                    }
                }
            }
            catch (Exception)
            {
            }
        }
示例#2
0
        public static string Upload(string name, string old, string sDir, ref string alert, bool overide)
        {
            string         sFile = string.Empty;
            HttpPostedFile DKKD  = HttpContext.Current.Request.Files[name];

            if (DKKD != null && DKKD.FileName.Length > 0)
            {
                string _fileName = DKKD.FileName;
                if (DKKD.ContentLength > 5 * 1024 * 1024)
                {
                    alert += @"\r\nKích cỡ " + name + " không được lớn hơn 5MB.";
                    return(string.Empty);
                }
                else
                {
                    try
                    {
                        string sDirFull = HL.Core.Global.Application.BaseDirectory + sDir;
                        //if (!System.IO.Directory.Exists(sDirFull))
                        //    System.IO.Directory.CreateDirectory(sDirFull);
                        sFile = sDir +
                                HL.Lib.Global.Data.GetCode(System.IO.Path.GetFileNameWithoutExtension(_fileName)) +
                                System.IO.Path.GetExtension(_fileName);
                        string             sFileFull = HL.Core.Global.Application.BaseDirectory + sFile;
                        System.IO.FileInfo fi        = new System.IO.FileInfo(sFileFull);
                        //Kiểm tra tồn tại, nếu tồn tại thì xóa
                        bool exits = fi.Exists;
                        if (exits)
                        {
                            if (overide)
                            {
                                string unique = string.Format("{0:ddMMyyyyhhmmss}", DateTime.Now);
                                sFile     = sDir + Data.GetCode(System.IO.Path.GetFileNameWithoutExtension(_fileName)) + "_" + unique + System.IO.Path.GetExtension(_fileName);
                                sFileFull = HL.Core.Global.Application.BaseDirectory + sFile;
                            }
                            else
                            {
                                fi.Delete();
                            }
                        }
                        if (!string.IsNullOrEmpty(old) && old != sFile)
                        {
                            if (System.IO.File.Exists(HL.Core.Global.Application.BaseDirectory + old.Replace("~/", "/")))
                            {
                                System.IO.File.Delete(HL.Core.Global.Application.BaseDirectory + old.Replace("~/", "/"));
                            }
                        }
                        //them vao db
                        DKKD.SaveAs(sFileFull);
                    }
                    catch (Exception ex)
                    {
                        Error.Write("Loi upload file : " + ex.Message);
                    }
                }
            }
            if (!string.IsNullOrEmpty(sFile))
            {
                return("~" + sFile);
            }
            else
            {
                return("");
            }
        }