/// <summary> /// Replaces the or add subdomain. /// </summary> /// <param name="host">The host.</param> /// <param name="subdomainTo">The subdomain to.</param> /// <param name="path">The path.</param> /// <returns></returns> public static string ReplaceOrAddSubdomainPath(string host, string subdomainTo, string path) { if (NullableHelper.AnyIsNull(host, subdomainTo, path)) { return(path); } // path: //www.campingbooking.com // host: campingbooking.com // subdomainto: cdn. // -> //cdn.campingbooking.com Regex regexHostNoDomain = new Regex(string.Concat("//www.", host)); path = regexHostNoDomain.Replace(path, string.Concat("//", subdomainTo, host)); // path: //www.campingbooking.com // host: www.campingbooking.com // subdomainto: cdn. // -> //cdn.campingbooking.com var hostWithNewSubdomain = host.Replace("www.", subdomainTo); path = path.Replace("//" + host, "//" + hostWithNewSubdomain); // path: //campingbooking.com // host: campingbooking.com // subdomainto: cdn. // -> //cdn.campingbooking.com Regex regexHostNoWWW = new Regex(string.Concat("//", host)); path = regexHostNoWWW.Replace(path, string.Concat("//", subdomainTo, host)); return(path); }
/// <summary> /// Replaces the or add subdomain. /// img src=www.campingbooking -> img src=cdn.campingbooking.com /// </summary> /// <param name="host">The host.</param> /// <param name="subdomainTo">The subdomain to.</param> /// <param name="html">The HTML.</param> /// <returns></returns> public static string ReplaceOrAddSubdomain(string host, string subdomainTo, string html) { if (NullableHelper.AnyIsNull(host, subdomainTo, html)) { return(html); } // all imgs using (var regexImgReplacer = new RegexSubdomainReplacer("imgsrc", host, subdomainTo)) { html = RegexImgElements.Replace(html, regexImgReplacer.Replace); } // all script using (var regexScriptReplacer = new RegexSubdomainReplacer("scriptsrc", host, subdomainTo)) { html = RegexScriptElements.Replace(html, regexScriptReplacer.Replace); } // all link using (var regexLinkReplacer = new RegexSubdomainReplacer("linkhref", host, subdomainTo)) { html = RegexLinkElements.Replace(html, regexLinkReplacer.Replace); } // any plain stuff if (html.StartsWith("//") || html.StartsWith("http")) { html = ReplaceOrAddSubdomainPath(host, subdomainTo, html); } return(html); }
public static void GetDateTime(string date, out DateTime pubDate) { if (string.IsNullOrWhiteSpace(date)) { pubDate = DateTime.Today; return; } if (DateTime.TryParse(date, out pubDate)) { return; } date = date.ToLower(); const string pattern = @"(?<day>[1-9]|[12][0-9]|3[01]) (?<month>jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec) (?<year>(?:19|20)[0-9]{2})"; MatchCollection matches = Regex.Matches(date, pattern, RegexOptions.IgnoreCase); Match match = matches[0]; if (string.IsNullOrEmpty(match.Value)) { pubDate = DateTime.Today; return; } int month = GetMonth(Regex.Replace(match.Value, pattern, $"${nameof(month)}")); int day = Convert.ToInt32(Regex.Replace(match.Value, pattern, $"${nameof(day)}")); int year = Convert.ToInt32(Regex.Replace(match.Value, pattern, $"${nameof(year)}")); pubDate = !NullableHelper.AnyIsNull(day, month, year) ? new DateTime(year, month, day) : DateTime.Today; }
/// <summary> /// https://msdn.microsoft.com/en-us/library/ms229716(v=vs.110).aspx /// </summary> /// <returns></returns> public static string GetFileNamePathByPattern(string ftpPath, string filePattern, string ftpUser, string ftpPassword) { if (NullableHelper.AnyIsNull(ftpPath, filePattern, ftpUser, ftpPassword)) { return(null); } FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath)); ftp.Timeout = 60000; ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftp.UseBinary = true; ftp.Proxy = null; if (string.IsNullOrEmpty(ftpUser) || string.IsNullOrEmpty(ftpPassword)) { throw new InvalidCredentialException(); } ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword); FtpWebResponse response = (FtpWebResponse)ftp.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader reader = new StreamReader(responseStream); List <string> files = new List <string>(); while (reader.Peek() >= 0) { string line = reader.ReadLine(); //string[] fileItems = line.CollapseWhiteSpaces().Split(' '); files.Add(line); } string ftpFile = null; foreach (var item in files.OrderByDescending(o => o)) { if (new Regex(filePattern).IsMatch(item)) { ftpFile = item; break; } } return($"{ftpPath}/{ftpFile}"); }
/// <summary> /// Opgegeven datum valt in de periode startdatum en einddatum /// /// </summary> /// <param name="date">de datum</param> /// <param name="lowerBound">startdatum</param> /// <param name="lowerBoundType"></param> /// <param name="upperBound">einddatum</param> /// <param name="upperBoundType"></param> /// <returns></returns> public static bool IsInDateRange(this DateTime?date, DateTime?lowerBound, RangeBoundaryType lowerBoundType, DateTime?upperBound, RangeBoundaryType upperBoundType) { if (NullableHelper.AnyIsNull(date, lowerBound, upperBound)) { return(false); } // Als de range binnen dezelfde dag ligt op 1 stellen. if (lowerBound.HasValue && upperBound.HasValue && lowerBound.Equals(upperBound)) { upperBound = upperBound.Value.AddDays(1); } return(((lowerBoundType == RangeBoundaryType.Exclusive && date > lowerBound) || (lowerBoundType == RangeBoundaryType.Inclusive && date >= lowerBound)) && ((upperBoundType == RangeBoundaryType.Exclusive && date < upperBound) || (upperBoundType == RangeBoundaryType.Inclusive && date <= upperBound))); }
public static List <string> GetFiles(string ftpPath, string ftpUser, string ftpPassword) { if (NullableHelper.AnyIsNull(ftpPath, ftpUser, ftpPassword)) { return(null); } FtpWebRequest ftp = (FtpWebRequest)WebRequest.Create(new Uri(ftpPath)); ftp.Timeout = 60000; ftp.Method = WebRequestMethods.Ftp.ListDirectoryDetails; ftp.UseBinary = true; ftp.Proxy = null; if (string.IsNullOrEmpty(ftpUser) || string.IsNullOrEmpty(ftpPassword)) { throw new InvalidCredentialException(); } ftp.Credentials = new NetworkCredential(ftpUser, ftpPassword); FtpWebResponse response = (FtpWebResponse)ftp.GetResponse(); Stream responseStream = response.GetResponseStream(); List <string> files = new List <string>(); if (responseStream != null) { StreamReader reader = new StreamReader(responseStream); while (reader.Peek() >= 0) { string fileItems = reader.ReadLine(); //files.Add(fileItems[fileItems.LastIndex()]); } } return(files); }