diff --git a/SjUpdater/MainWindow.xaml b/SjUpdater/MainWindow.xaml index fbb362d..abcfa28 100644 --- a/SjUpdater/MainWindow.xaml +++ b/SjUpdater/MainWindow.xaml @@ -24,6 +24,9 @@ + + + @@ -494,9 +497,15 @@ - @@ -567,12 +576,14 @@ SelectedValue="{Binding CurrentAccent, ElementName=Window}" /> - - - + @@ -582,9 +593,8 @@ SelectedValue="{Binding NotificationTimeout, Source={x:Static local:Settings.Instance}}"/> - - - + + diff --git a/SjUpdater/Model/FavShowData.cs b/SjUpdater/Model/FavShowData.cs index f336000..334ffeb 100644 --- a/SjUpdater/Model/FavShowData.cs +++ b/SjUpdater/Model/FavShowData.cs @@ -157,9 +157,15 @@ public void ApplyFilter(bool reset=true) ignoreCurrentUpload = true; do { - if ((currentUpload.Language & FilterLanguage) == 0) //Filter: Language + + UploadLanguage language = currentUpload.Language; + if (!Settings.Instance.MarkSubbedAsGerman && currentUpload.Subbed) //dont mark german-subbed as german + { + language&=~UploadLanguage.German; //remove german + } + + if ((language & FilterLanguage) == 0) //Filter: Language break; - if (!String.IsNullOrWhiteSpace(FilterRuntime) && //Filter: Runtime !(new Regex(FilterRuntime).Match(currentUpload.Runtime).Success)) @@ -213,10 +219,11 @@ public void ApplyFilter(bool reset=true) int episodeNr = -1; if (seasonNr != -1) { - Match m1 = new Regex("S0{0,4}" + seasonNr + "E(\\d+)", RegexOptions.IgnoreCase).Match(download.Title); - if (m1.Success) + MatchCollection mts = new Regex("S0{0,4}" + seasonNr + "E(\\d+)", RegexOptions.IgnoreCase).Matches(download.Title); + MatchCollection mts_ep = new Regex("[^A-Z]E(\\d+)", RegexOptions.IgnoreCase).Matches(download.Title); + if (mts.Count==1 && mts_ep.Count==1) //if there is exactly one match for "SE" and there is no second "E" (e.g. S01E01-E12) { - int.TryParse(m1.Groups[1].Value, out episodeNr); + int.TryParse(mts[0].Groups[1].Value, out episodeNr); } if (episodeNr == -1 && !FilterShowNonEpisode.GetValueOrDefault()) //Filter: NonEpisode Stuff diff --git a/SjUpdater/Model/UploadData.cs b/SjUpdater/Model/UploadData.cs index 635b251..d070cae 100644 --- a/SjUpdater/Model/UploadData.cs +++ b/SjUpdater/Model/UploadData.cs @@ -20,6 +20,7 @@ public UploadData() Size = ""; Runtime = ""; Language = 0; + Subbed = false; Season = null; Favorized = false; } @@ -30,6 +31,7 @@ public UploadData() public String Runtime { get; set; } public UploadLanguage Language { get; set; } public SeasonData Season { get; set; } + public bool Subbed { get; set; } public bool Favorized { get; set; } //Todo: move to Fav* class, since it's user data diff --git a/SjUpdater/Settings.cs b/SjUpdater/Settings.cs index 5e0f888..e14c337 100644 --- a/SjUpdater/Settings.cs +++ b/SjUpdater/Settings.cs @@ -111,6 +111,8 @@ public ObservableCollection TvShows /// /// The Numer of Threads used to fetch updates on programm start /// + + public bool MarkSubbedAsGerman { get; set; } public uint NumFetchThreads { get { return numFetchThreads; } @@ -171,6 +173,7 @@ public uint NumFetchThreads public String FilterUploader { get; set; } public String FilterSize { get; set; } public String FilterRuntime { get; set; } + public Settings() { @@ -186,6 +189,7 @@ public Settings() FilterLanguage = UploadLanguage.Any; FilterShowNonEpisode = true; FilterShowNonSeason = true; + MarkSubbedAsGerman = false; NoPersonalData = false; EnableImages = true; CheckForUpdates = true; diff --git a/SjUpdater/SjInfo.cs b/SjUpdater/SjInfo.cs index cfc51ab..565982a 100644 --- a/SjUpdater/SjInfo.cs +++ b/SjUpdater/SjInfo.cs @@ -8,6 +8,7 @@ using System.Net; using System.Text; using System.Text.RegularExpressions; +using System.Windows.Controls; using RestSharp; using SjUpdater.Model; using SjUpdater.Utils; @@ -203,11 +204,14 @@ private static List ParseSite(ShowData showData, string url, out s Match m; SeasonData seasonData=null; UploadData uploadData=null; - // int season = -1; while (!reader.EndOfStream) { String line = reader.ReadLine(); + if (line.Contains("line-height") && line.Contains(" ")) //detect:
 
+ { + continue; //I can only hope this will never occour in any other case.. + } if (inContent) { if (inPost) @@ -229,15 +233,6 @@ private static List ParseSite(ShowData showData, string url, out s continue; } string title = WebUtility.HtmlDecode(m.Groups[1].Value); - /*int episode = -1; - if (season != -1) - { - Match m1 = new Regex("S0{0,4}" + season + "E(\\d+)", RegexOptions.IgnoreCase).Match(title); - if (m1.Success) - { - int.TryParse(m1.Groups[1].Value, out episode); - } - }*/ var downloads = new Dictionary(); Regex r = new Regex(" ParseSite(ShowData showData, string url, out s key = keyOrg + "(" + ++num + ")"; } String val = m2.Groups[1].Value; - if (val != null && !String.IsNullOrWhiteSpace(val) && val.StartsWith("http://")) + if (val != null && !String.IsNullOrWhiteSpace(val) && val.Trim().StartsWith("http://")) { downloads.Add(key, val); } else { + Console.WriteLine("Warning: Invalid Download received while parsing " + showData.Name + ". Ignoring link"); //ignoring invalid download } } if (line.Contains("

")) break; } - - if (title.Contains("720p") ) { @@ -295,10 +289,13 @@ private static List ParseSite(ShowData showData, string url, out s DownloadData dd = new DownloadData(); dd.Upload = uploadCache == null ? uploadData : uploadCache.GetUniqueUploadData(uploadData); - - //ed.EpisodeN = episode; dd.Title = title; + if (title.ToLower().Contains("subbed")) + { + dd.Upload.Subbed = true; + } + foreach (var download in downloads) { dd.Links.Add(download.Key,download.Value); @@ -354,6 +351,10 @@ private static List ParseSite(ShowData showData, string url, out s { uploadData.Language |= UploadLanguage.English; } + if (value.Contains("subbed")) + { + uploadData.Subbed = true; + } } } } @@ -388,13 +389,6 @@ private static List ParseSite(ShowData showData, string url, out s seasonData.Url= m.Groups[1].Value; seasonData.Title = WebUtility.HtmlDecode(m.Groups[2].Value); - /*season = -1; - Match m2 = new Regex("(?:season|staffel)\\s*(\\d+)", RegexOptions.IgnoreCase).Match(seasonData.Title); - if (m2.Success) - { - int.TryParse(m2.Groups[1].Value,out season); - }*/ - } else if (new Regex("").Match(line).Success) { diff --git a/SjUpdater/SjUpdater.csproj b/SjUpdater/SjUpdater.csproj index 4e9b9d8..8768b15 100644 --- a/SjUpdater/SjUpdater.csproj +++ b/SjUpdater/SjUpdater.csproj @@ -128,6 +128,7 @@ + @@ -138,6 +139,7 @@ + @@ -186,6 +188,7 @@ ResXFileCodeGenerator Resources.Designer.cs + Designer diff --git a/SjUpdater/Utils/CachedBitmap.cs b/SjUpdater/Utils/CachedBitmap.cs index d9ec69a..a1daecd 100644 --- a/SjUpdater/Utils/CachedBitmap.cs +++ b/SjUpdater/Utils/CachedBitmap.cs @@ -178,7 +178,7 @@ static MemoryStream DownloadData(string url) } } - private static BitmapImage BitmapImageFromStream(Stream stream, bool freeze = true) + public static BitmapImage BitmapImageFromStream(Stream stream, bool freeze = true) { try { diff --git a/SjUpdater/Utils/FavIcon.cs b/SjUpdater/Utils/FavIcon.cs new file mode 100644 index 0000000..fd02fb5 --- /dev/null +++ b/SjUpdater/Utils/FavIcon.cs @@ -0,0 +1,150 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using System.Xml.Serialization; + +namespace SjUpdater.Utils +{ + public class FavIcon + { + + private static Dictionary _dictCache = new Dictionary(); + private readonly static string cachePath = Path.Combine(Path.GetTempPath(), "sjupdater", "faviconcache"); + static FavIcon() + { + + Directory.CreateDirectory(cachePath); + string[] files = Directory.GetFiles(cachePath); + foreach (var file in files) + { + FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read); + var bitmap = CachedBitmap.BitmapImageFromStream(fs); + _dictCache.Add(Path.GetFileNameWithoutExtension(file),bitmap); + fs.Close(); + } + + + } + + public static ImageSource Get(String value) + { + try + { + var b = GetFromCache(value); + if (b == null) b = GetFromUrl(value); + //todo: draw two letters + return b; + } + catch (Exception) + { + + return null; + } + + } + + + + + + + private static BitmapImage GetFromCache(string value) + { + value.ToLower(); + foreach (var key in _dictCache.Keys) + { + String key2 = key.ToLower(); + if (key2.Contains(value) ||value.Contains(key2)) + { + return _dictCache[key]; + } + } + return null; + } + + + + + + static String FindUrl(string value) + { + if (!value.StartsWith("http")) + { + value = "http://" + value; + } + Uri u = new Uri(value); + if (Uri.CheckHostName(u.DnsSafeHost) == UriHostNameType.Unknown) + { + return null; + } + + + HttpWebRequest req = HttpWebRequest.CreateHttp(value); + req.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64)"; + req.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate; + var res = req.GetResponse(); + StreamReader reader = new StreamReader(res.GetResponseStream()); + String html = reader.ReadToEnd(); + MatchCollection mts = new Regex("]*", RegexOptions.IgnoreCase).Matches(html); + foreach (Match mt in mts) + { + String m = mt.Value.ToLower(); + if (new Regex("rel\\s*=\\s*['\"][a-z0-9_\\- ]*(icon|shortcut)[a-z0-9_\\- ]*['\"]", RegexOptions.IgnoreCase).Match(m).Success) + { + Match murl = new Regex("href\\s*=\\s*['\"]([^'\"]+)['\"]", RegexOptions.IgnoreCase).Match(m); + if (murl.Success) + { + String path = murl.Groups[1].Value; + if (!path.StartsWith("http")) + { + path = res.ResponseUri + "/" + path; + } + return path; + } + } + } + return null; + } + + private static BitmapImage GetFromUrl(string value) + { + String url = FindUrl(value); + if (url == null) return null; + String[] url_parts = new Uri(url).DnsSafeHost.Split(new char[] {'.'}); + String key = url_parts[url_parts.Length - 2]; + try + { + MemoryStream ms = new MemoryStream(); + HttpWebRequest request = WebRequest.CreateHttp(url); + + HttpWebResponse response = request.GetResponse() as HttpWebResponse; + Stream responseStream = response.GetResponseStream(); + responseStream.CopyTo(ms); + responseStream.Close(); + response.Close(); + + ms.Position = 0; + FileStream f = new FileStream(Path.Combine(cachePath,key+url.Substring(url.LastIndexOf('.'))),FileMode.Create,FileAccess.Write); + ms.CopyTo(f); + + f.Close(); + ms.Position = 0; + var bmap = CachedBitmap.BitmapImageFromStream(ms); + _dictCache.Add(key, bmap); + return bmap; + + } + catch (Exception e) + { + return null; + } + } + } +} diff --git a/SjUpdater/Utils/StringToFaviconConverter.cs b/SjUpdater/Utils/StringToFaviconConverter.cs new file mode 100644 index 0000000..3348ddc --- /dev/null +++ b/SjUpdater/Utils/StringToFaviconConverter.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Data; +using System.Windows.Media; + +namespace SjUpdater.Utils +{ + public class StringToFaviconConverter : IValueConverter + { + public object Convert(object value, Type targetType, + object parameter, CultureInfo culture) + { + if (targetType != typeof(ImageSource) || value.GetType() != typeof(string)) + { + throw new ArgumentException(); + } + return FavIcon.Get(value as String); + } + + public object ConvertBack(object value, Type targetType, + object parameter, CultureInfo culture) + { + return null; + } + } +}