Skip to content

Commit

Permalink
desarrollando favoritos
Browse files Browse the repository at this point in the history
  • Loading branch information
tetradog committed Jun 24, 2021
1 parent 6263b1c commit f7657e2
Show file tree
Hide file tree
Showing 25 changed files with 745 additions and 104 deletions.
75 changes: 33 additions & 42 deletions CheckFenix.Core/Capitulo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,46 +11,24 @@

namespace CheckFenix.Core
{
public static class HtmlDic
public class Capitulo
{
public const string URLANIMEFENIX = "https://www.animefenix.com/";
public static string CacheFolder = "CacheHtml";
public static TimeSpan TiempoMinimoRefresh = TimeSpan.FromMinutes(5);
static SortedList<string, KeyValuePair<long, string>> DicHtml { get; set; } = new SortedList<string, KeyValuePair<long, string>>();
public static HtmlDocument GetHtml(Uri url)
{
public static string CacheFolder = "CacheCapitulos";
static SortedList<string, Bitmap> DicImagenes { get; set; }

return new HtmlDocument().LoadString(GetString(url));
}
public static string GetString(Uri url)
{
if (DicHtml.ContainsKey(url.AbsoluteUri))
{
if (DateTime.Now - new DateTime(DicHtml[url.AbsoluteUri].Key) > TiempoMinimoRefresh)
DicHtml.Remove(url.AbsoluteUri);
}
if (!DicHtml.ContainsKey(url.AbsoluteUri))
{
DicHtml.Add(url.AbsoluteUri, new KeyValuePair<long, string>(DateTime.Now.Ticks, url.DownloadString()));
}
return DicHtml[url.AbsoluteUri].Value;
}
public static void SaveCache()
Serie parent;
static Capitulo()
{
Uri urlAnimeFenix = new Uri(URLANIMEFENIX);

foreach (var item in DicHtml)
DicImagenes = new SortedList<string, Bitmap>();
if (!Directory.Exists(CacheFolder))
Directory.CreateDirectory(CacheFolder);
else
{
File.WriteAllText(item.Value.Value,System.IO.Path.Combine(CacheFolder, new Uri(item.Key).MakeRelativeUri(urlAnimeFenix).ToString() + ".html"));
//cargo el cache!
foreach (string item in Directory.GetFiles(CacheFolder))
DicImagenes.Add(Path.GetFileName(item), new Bitmap(item));
}
}
}
public class Capitulo
{
public static string CacheFolder = "CacheCapitulos";
static SortedList<string, Bitmap> DicImagenes { get; set; } = new SortedList<string, Bitmap>();

Serie parent;
public Capitulo() { }
public Capitulo(HtmlNode nodeDiv)
{
Expand All @@ -65,9 +43,10 @@ public Bitmap Image
{
get
{
if (!DicImagenes.ContainsKey(Picture.AbsoluteUri))
DicImagenes.Add(Picture.AbsoluteUri, Picture.GetBitmap().Escala(0.25f));
return DicImagenes[Picture.AbsoluteUri];
string url = Path.GetFileName(Picture.AbsoluteUri);
if (!DicImagenes.ContainsKey(url))
DicImagenes.Add(url, Picture.GetBitmap().Escala(0.25f));
return DicImagenes[url];
}
}
public Uri Pagina { get; set; }
Expand All @@ -88,11 +67,16 @@ public Serie Parent
return parent;
}
}
public IEnumerable<Comentario> GetComentarios(IReadComentario reader)
{

return Comentario.GetComentarios(reader,Pagina);
}
public IEnumerable<string> GetLinks()
{

string url;
string html = HtmlDic.GetString(Pagina);
string html = HtmlDic.GetStringCapitulo(Pagina);
Regex regex = new Regex(@"(?<=<iframe[^>]*?)(?:\s*width=[""'](?<width>[^""']+)[""']|\s*height=[""'](?<height>[^'""]+)[""']|\s*src=[""'](?<src>[^'""]+[""']))+[^>]*?>");
Match match = regex.Match(html);

Expand Down Expand Up @@ -131,7 +115,7 @@ public static IEnumerable<Capitulo> GetCapitulos(HtmlNode nodePagina)
public static Capitulo FromUrl(Uri urlVisionado)
{
Capitulo capitulo = new Capitulo() { Pagina = urlVisionado };
HtmlDocument docUrl = HtmlDic.GetHtml(urlVisionado);
HtmlDocument docUrl = HtmlDic.GetHtmlCapitulo(urlVisionado);
HtmlNode nodoName = docUrl.GetByTagName("h1").FirstOrDefault();


Expand All @@ -142,11 +126,18 @@ public static Capitulo FromUrl(Uri urlVisionado)
}
public static void SaveCache()
{
Uri urlAnimeFenix = new Uri(HtmlDic.URLANIMEFENIX);

string path;
foreach (var item in DicImagenes)
{
item.Value.Save(System.IO.Path.Combine(CacheFolder, new Uri(item.Key).MakeRelativeUri(urlAnimeFenix).ToString() + ".jpg"), System.Drawing.Imaging.ImageFormat.Jpeg);
try
{
path = Path.Combine(CacheFolder, item.Key);
if (!File.Exists(path))
item.Value.Save(path);
}
catch {
System.Diagnostics.Debugger.Break();
}
}
}
}
Expand Down
48 changes: 48 additions & 0 deletions CheckFenix.Core/Comentario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using Gabriel.Cat.S.Extension;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;

namespace CheckFenix.Core
{
public class Comentario
{
static SortedList<string, Bitmap> DicPic = new SortedList<string, Bitmap>();
public Comentario() { }
public Comentario(HtmlNode nodo)
{
Picture =new Uri( nodo.GetByClass("avatar").First().GetByTagName("img").First().Attributes["src"].Value);
Name = nodo.GetByClass("author").First().GetByTagName("a").First().InnerText;
Mensaje = nodo.GetByClass("post-message").First().GetByTagName("p").First().InnerText;
}
public Uri Picture { get; set; }
public Bitmap Image
{
get {

string name = Path.GetFileName(Picture.AbsoluteUri);
if (!DicPic.ContainsKey(name))
{
DicPic.Add(name, Picture.GetBitmap());
}
return DicPic[name];

}
}
public string Name { get; set; }
public string Mensaje { get; set; }

public static IEnumerable<Comentario> GetComentarios(IReadComentario reader,Uri page)
{
// post-list
HtmlNode posts;
reader.Load(page);
reader.Call("$('#showComments').click()");
posts = reader.GetDocument().GetElementbyId("post-list");//no existe hasta que se haga click en $("#showComments")
return Equals(posts,default(HtmlNode))?new Comentario[0]: posts.ChildNodes.Select(p => new Comentario(p));
}
}
}
1 change: 1 addition & 0 deletions CheckFenix.Core/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public static Bitmap GetBitmap(this Uri url)
return new Bitmap(responseStream);
}
}

}
101 changes: 101 additions & 0 deletions CheckFenix.Core/HtmlDic.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
using System;
using System.Collections.Generic;
using HtmlAgilityPack;
using System.IO;
using System.Threading;

namespace CheckFenix.Core
{
public static class HtmlDic
{
public const string URLANIMEFENIX = "https://www.animefenix.com/";
public static string CacheFolder = "CacheCapitulos";
public static TimeSpan TiempoMinimoRefresh { get; set; }
static SortedList<string, KeyValuePair<long, string>> DicHtmlSerie { get; set; }
static SortedList<string, string> DicHtmlCapitulo { get; set; }
static Semaphore smDic = new Semaphore(1, 1);
static HtmlDic()
{
DicHtmlSerie = new SortedList<string, KeyValuePair<long, string>>();
DicHtmlCapitulo = new SortedList<string, string>();
TiempoMinimoRefresh = TimeSpan.FromMinutes(5);
if (!Directory.Exists(CacheFolder))
Directory.CreateDirectory(CacheFolder);
else
{
foreach(string item in Directory.GetFiles(CacheFolder))
{
DicHtmlCapitulo.Add(Path.GetFileName(item),File.ReadAllText(item));
}
}

}
public static HtmlDocument GetHtmlSerie(Uri url)
{

return new HtmlDocument().LoadString(GetStringSerie(url));
}
public static HtmlDocument GetHtmlCapitulo(Uri url)
{

return new HtmlDocument().LoadString(GetStringCapitulo(url));
}
public static string GetStringSerie(Uri url)
{
string html;
try
{
smDic.WaitOne();
if (DicHtmlSerie.ContainsKey(url.AbsoluteUri))
{
if (DateTime.Now - new DateTime(DicHtmlSerie[url.AbsoluteUri].Key) > TiempoMinimoRefresh)
DicHtmlSerie.Remove(url.AbsoluteUri);
}
if (!DicHtmlSerie.ContainsKey(url.AbsoluteUri))
{
DicHtmlSerie.Add(url.AbsoluteUri, new KeyValuePair<long, string>(DateTime.Now.Ticks, url.DownloadString()));
}
html = DicHtmlSerie[url.AbsoluteUri].Value;
}
catch
{
throw;
}
finally
{
smDic.Release();
}
return html;
}
public static string GetStringCapitulo(Uri url)
{
string html;

smDic.WaitOne();
if (!DicHtmlCapitulo.ContainsKey(url.AbsoluteUri))
{
DicHtmlCapitulo.Add(url.AbsoluteUri, url.DownloadString());
}
html= DicHtmlCapitulo[url.AbsoluteUri];
smDic.Release();
return html;
}
public static void SaveCache()
{
string path;
foreach (var item in DicHtmlCapitulo)
{
try
{
path = Path.Combine(CacheFolder, item.Key);
if (!File.Exists(path))
File.WriteAllText(path, item.Value);
}
catch
{
System.Diagnostics.Debugger.Break();
}
}
}
}
}
1 change: 1 addition & 0 deletions CheckFenix.Core/HtmlDocumentExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public static class HtmlDocumentExtension
{
public static string DownloadString(string url)
{

return new WebClient().DownloadString(url);
}
public static HtmlDocument LoadUrl(this HtmlDocument document, Uri url)
Expand Down
12 changes: 12 additions & 0 deletions CheckFenix.Core/IReadComentario.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using HtmlAgilityPack;
using System;

namespace CheckFenix.Core
{
public interface IReadComentario
{
void Load(Uri page);
void Call(string js);
HtmlDocument GetDocument();
}
}
Loading

0 comments on commit f7657e2

Please sign in to comment.