gelbooru source
This commit is contained in:
		| @@ -18,7 +18,7 @@ namespace Gallery.Danbooru | ||||
|         public async Task<GalleryItem[]> GetRecentItemsAsync(int page) | ||||
|         { | ||||
|             var url = $"https://danbooru.donmai.us/posts?page={page}"; | ||||
|             var (result, error) = await NetHelper.RequestObject<GalleryItem[]>(url, contentHandler: ContentHandler); | ||||
|             var (result, error) = await NetHelper.RequestObject(url, @return: content => ResolveGalleryItems(content)); | ||||
|  | ||||
|             if (result == null || !string.IsNullOrEmpty(error)) | ||||
|             { | ||||
| @@ -29,18 +29,30 @@ namespace Gallery.Danbooru | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         private string ContentHandler(string content) | ||||
|         private GalleryItem[] ResolveGalleryItems(string content) | ||||
|         { | ||||
|             var regex = new Regex(@"<article id=""post_\d+"".+?data-id=""(\d+)"".+?data-tags=""([^""]+?)"".+?data-width=""(\d+?)"" data-height=""(\d+?)"".+?data-source=""([^""]+?)"" data-uploader-id=""(\d+?)"" data-normalized-source=""([^""]+?)"".+?data-file-url=""([^""]+?)"".+?data-preview-file-url=""([^""]+?)""", RegexOptions.Multiline); | ||||
|             var regex = new Regex( | ||||
|                 @"<article id=""post_\d+"".+?data-id=""(\d+)"".+?data-tags=""([^""]+?)"".+?" + | ||||
|                 @"data-width=""(\d+?)"" data-height=""(\d+?)"".+?data-source=""([^""]+?)"".+?" + | ||||
|                 @"data-uploader-id=""(\d+?)"".+?data-normalized-source=""([^""]+?)"".+?" + | ||||
|                 @"data-file-url=""([^""]+?)"".+?data-large-file-url=""([^""]+?)""", RegexOptions.Multiline); | ||||
|             var matches = regex.Matches(content); | ||||
|             var array = new string[matches.Count]; | ||||
|             for (var i = 0; i < array.Length; i++) | ||||
|             var items = new GalleryItem[matches.Count]; | ||||
|             for (var i = 0; i < items.Length; i++) | ||||
|             { | ||||
|                 var g = matches[i].Groups; | ||||
|                 var tags = g[2].Value.Replace(" ", "\",\""); | ||||
|                 array[i] = $"{{\"Id\":{g[1].Value},\"Tags\":[\"{tags}\"],\"Width\":{g[3].Value},\"Height\":{g[4].Value},\"Source\":\"{g[7].Value}\",\"UserId\":\"{g[6].Value}\",\"RawUrl\":\"{g[8].Value}\",\"PreviewUrl\":\"{g[9].Value}\"}}"; | ||||
|                 items[i] = new GalleryItem(int.Parse(g[1].Value)) | ||||
|                 { | ||||
|                     Tags = g[2].Value.Split(' '), | ||||
|                     Width = int.Parse(g[3].Value), | ||||
|                     Height = int.Parse(g[4].Value), | ||||
|                     UserId = g[6].Value, | ||||
|                     Source = g[7].Value, | ||||
|                     RawUrl = g[8].Value, | ||||
|                     PreviewUrl = g[9].Value | ||||
|                 }; | ||||
|             } | ||||
|             return $"[{string.Join(',', array)}]"; | ||||
|             return items; | ||||
|         } | ||||
|  | ||||
|         public void SetCookie() | ||||
|   | ||||
| @@ -1,5 +1,7 @@ | ||||
| using System; | ||||
| using System.Text.RegularExpressions; | ||||
| using System.Threading.Tasks; | ||||
| using Gallery.Util; | ||||
| using Gallery.Util.Interface; | ||||
| using Gallery.Util.Model; | ||||
| using Xamarin.Forms; | ||||
| @@ -13,9 +15,40 @@ namespace Gallery.Gelbooru | ||||
|         public string FlyoutIconKey => "Gelbooru"; | ||||
|         public string HomePage => "https://gelbooru.com"; | ||||
|  | ||||
|         public Task<GalleryItem[]> GetRecentItemsAsync(int page) | ||||
|         public async Task<GalleryItem[]> GetRecentItemsAsync(int page) | ||||
|         { | ||||
|             throw new NotImplementedException(); | ||||
|             var offset = (page - 1) * 42; | ||||
|             var url = $"https://gelbooru.com/index.php?page=post&s=list&tags=all&pid={offset}"; | ||||
|             var (result, error) = await NetHelper.RequestObject(url, @return: content => ResolveGalleryItems(content)); | ||||
|  | ||||
|             if (result == null || !string.IsNullOrEmpty(error)) | ||||
|             { | ||||
|                 Log.Error("gelbooru.content.load", $"failed to load content array, error: {error}"); | ||||
|                 return null; | ||||
|             } | ||||
|  | ||||
|             return result; | ||||
|         } | ||||
|  | ||||
|         private GalleryItem[] ResolveGalleryItems(string content) | ||||
|         { | ||||
|             var regex = new Regex( | ||||
|                 @"<article(.|\n)+?<a id=""p(\d+?)"" href=""([^""]+?)"">(.|\n)+?<img src=""([^""]+?)"" title=""([^""]+?)""", | ||||
|                 RegexOptions.Multiline); | ||||
|             var matches = regex.Matches(content); | ||||
|             var items = new GalleryItem[matches.Count]; | ||||
|             for (var i = 0; i < items.Length; i++) | ||||
|             { | ||||
|                 var g = matches[i].Groups; | ||||
|                 items[i] = new GalleryItem(int.Parse(g[2].Value)) | ||||
|                 { | ||||
|                     RawUrl = g[3].Value, | ||||
|                     PreviewUrl = g[5].Value, | ||||
|                     Tags = g[6].Value.Split(' '), | ||||
|                     IsRawPage = true | ||||
|                 }; | ||||
|             } | ||||
|             return items; | ||||
|         } | ||||
|  | ||||
|         public void SetCookie() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user