Skip to content

Commit

Permalink
修正图片加载尺寸不正确的问题以及部分缓存问题
Browse files Browse the repository at this point in the history
  • Loading branch information
aiqinxuancai committed Apr 1, 2024
1 parent 70c2c89 commit 8dd5557
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 60 deletions.
29 changes: 24 additions & 5 deletions Aria2Fast/Service/MikanManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public string IndexPageExtractWeeklyAnimeJson(string htmlContent)
var imageAbsUrl = imageRelUrl;

imageAbsUrl = imageAbsUrl.Replace("width=400", "width=460");
imageAbsUrl = imageAbsUrl.Replace("height=400", "width=640");
imageAbsUrl = imageAbsUrl.Replace("height=400", "height=640");

if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(imageAbsUrl))
{
Expand Down Expand Up @@ -210,7 +210,6 @@ public string IndexPageExtractWeeklyAnimeJson2(string htmlContent)

foreach (var dayNode in bangumiNodes)
{
//var dateString = dayNode.SelectSingleNode(".//div[@id='data-row-2']")?.InnerText;
var dateNode = dayNode.SelectSingleNode(".//div[starts-with(@id, 'data-row-')]");
var dateString = dateNode?.InnerText;
dateString = dateString?.Trim();
Expand All @@ -221,16 +220,31 @@ public string IndexPageExtractWeeklyAnimeJson2(string htmlContent)

foreach (var animeNode in animeNodes)
{
var name = animeNode.SelectSingleNode(".//a[@class='an-text']")?.InnerText;
var nameNode = animeNode.SelectSingleNode(".//a[@class='an-text']");
var name = nameNode?.InnerText;

if (string.IsNullOrWhiteSpace(name))
{
// 从最后一个div,class=date-text中提取名称
var lastNameNode = animeNode.SelectSingleNode(".//div[@class='an-info-group']/div[contains(@class, 'date-text')][last()]");
name = lastNameNode?.InnerText;


}


name = HtmlEntity.DeEntitize(name);

var url = animeNode.SelectSingleNode(".//a[@class='an-text']")?.GetAttributeValue("href", string.Empty);

var imageNode = animeNode.SelectSingleNode(".//span[contains(@class, 'js-expand_bangumi')]");
var imageNode = animeNode.SelectSingleNode(".//span");
var dataSrc = imageNode?.GetAttributeValue("data-src", string.Empty);
var imageUrl = dataSrc;

if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(url) && !string.IsNullOrEmpty(imageUrl))
imageUrl = imageUrl.Replace("width=400", "width=460");
imageUrl = imageUrl.Replace("height=400", "height=640");

if (!string.IsNullOrEmpty(name) && !string.IsNullOrEmpty(imageUrl))
{
animeList.Add(new
{
Expand Down Expand Up @@ -263,6 +277,11 @@ public string AnimePage(string htmlContent)
var divs = htmlDoc.DocumentNode.SelectNodes("//div[contains(@class, 'subgroup-text')]");
var subgroupInfoList = new List<object>();

if (divs == null)
{
return "[]";
}

foreach (var div in divs)
{
var subgroupNames = new List<string>();
Expand Down
13 changes: 9 additions & 4 deletions Aria2Fast/Service/Model/MikanModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,20 @@ public int NewEpisode
{
get
{
if (Rss == null)
if (Rss == null || Rss.Count == 0)
{
return 0;
}
return Rss!.Max(a =>



int newEpisode = Rss!.Max(a =>
{
return a.Episode;
}
);
});

return newEpisode;

}
}

Expand Down
82 changes: 31 additions & 51 deletions Aria2Fast/Utils/ImageCacheUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,24 @@ public static async Task PreloadImageCache()
{
var dirPath = Path.Combine(Directory.GetCurrentDirectory(), "ImageCached");
var files = Directory.GetFiles(dirPath);
MemoryCache cache = MemoryCache.Default;
//MemoryCache cache = MemoryCache.Default;

await Task.Run(() => {
foreach (var file in files)
{
var fileName = Path.GetFileName(file);
string cacheKey = fileName;
if (!cache.Contains(cacheKey))
{

BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(file);
bitmap.EndInit();
BitmapImage bitmap = new BitmapImage();
bitmap.BeginInit();
bitmap.CreateOptions = BitmapCreateOptions.IgnoreImageCache | BitmapCreateOptions.DelayCreation;
bitmap.CacheOption = BitmapCacheOption.OnLoad;
bitmap.UriSource = new Uri(file);
bitmap.EndInit();

bitmap.Freeze();
bitmap.Freeze();

cache.Set(cacheKey, bitmap, new CacheItemPolicy());
Debug.WriteLine($"[缓存]{cacheKey}");

}
//cache.Set(cacheKey, bitmap, new CacheItemPolicy());
Debug.WriteLine($"[缓存]{cacheKey}");
}
});

Expand All @@ -56,14 +51,14 @@ await Task.Run(() => {
/// <returns></returns>
public static BitmapImage GetImageWithLocalCache(Uri uri)
{
MemoryCache cache = MemoryCache.Default;
//MemoryCache cache = MemoryCache.Default;
var fileName = Path.GetFileName(uri.LocalPath);
if (cache.Contains(fileName))
{
var bmp = (BitmapImage)cache.Get(fileName);
//bmp.Freeze();
return bmp;
}
//if (cache.Contains(fileName))
//{
// var bmp = (BitmapImage)cache.Get(fileName);
// //bmp.Freeze();
// return bmp;
//}

var dirPath = Path.Combine(Directory.GetCurrentDirectory(), "ImageCached");
if (!Directory.Exists(dirPath))
Expand All @@ -80,12 +75,22 @@ public static BitmapImage GetImageWithLocalCache(Uri uri)
}
else
{
var result = uri.GetBytesAsync().Result;
if (result != null && result.Length > 0)
try
{
var result = uri.GetBytesAsync().Result;
if (result != null && result.Length > 0)
{
img.StreamSource = new MemoryStream(result);
File.WriteAllBytes(localFilePath, result);
}
}
catch (Exception ex)
{
img.StreamSource = new MemoryStream(result);
File.WriteAllBytes(localFilePath, result);

}



}

img.CacheOption = BitmapCacheOption.OnLoad;
Expand All @@ -98,30 +103,5 @@ public static BitmapImage GetImageWithLocalCache(Uri uri)
return img;
}

public static BitmapImage GetImageFromCache(Uri uri)
{
BitmapImage img = null;

MemoryCache cache = MemoryCache.Default;

var fileName = Path.GetFileName(uri.LocalPath);
string cacheKey = fileName;

if (cache.Contains(cacheKey))
{
img = (BitmapImage)cache.Get(cacheKey);
}
else
{
img = new BitmapImage(uri);
img.CacheOption = BitmapCacheOption.OnLoad;
img.DownloadCompleted += (s, e) =>
{
cache.Set(cacheKey, img, new CacheItemPolicy());
};
}

return img;
}
}
}

0 comments on commit 8dd5557

Please sign in to comment.