/
WordPressClient.cs
179 lines (144 loc) · 6.1 KB
/
WordPressClient.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
using GopherServer.Core.Helpers;
using GopherServer.Core.Models;
using GopherServer.Core.Results;
using GopherServer.Core.WpJson.Extensions;
using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WordPressRestApi;
using WordPressRestApi.Models;
using WordPressRestApi.QueryModel;
namespace GopherServer.Core.WpJson
{
public class WordPressClient
{
public static WordPressApiClient client;
public WordPressClient(string url)
{
client = new WordPressApiClient(url);
}
/// <summary>
/// Produces a basic Homepage for the blog
/// </summary>
/// <returns></returns>
public DirectoryResult GetHomePage()
{
var result = new DirectoryResult();
result.Items.Add(new DirectoryItem("Welcome"));
result.Items.Add(new DirectoryItem("-------"));
result.Items.Add(new DirectoryItem("Latest Posts"));
var latestPosts = client.GetPosts(new PostsQuery { PerPage = 10 }).Result;
result.Items.AddRange(latestPosts.ToDirectoryItems());
result.Items.Add(new DirectoryItem("---"));
result.Items.Add(new DirectoryItem(ItemType.DIRECTORY, "Categories", "/categories/"));
result.Items.Add(new DirectoryItem(ItemType.INDEXSEARCH, "Search", "/search/"));
// TODO: Add Tags and Pages
return result;
}
/// <summary>
/// Peforms a search of the blog
/// </summary>
/// <param name="q"></param>
/// <returns></returns>
public DirectoryResult Search(string q)
{
var posts = client.GetPosts(new PostsQuery { Search = q, PerPage = 100, OrderBy = OrderBy.title.ToString() }).Result;
var directory = new DirectoryResult();
directory.Items.Add(new DirectoryItem("Search for '" + q + "'"));
if (posts.Count > 0)
directory.Items.AddRange(posts.ToDirectoryItems());
else
directory.Items.Add(new DirectoryItem("No results found."));
// TODO - add paging!
return directory;
}
/// <summary>
/// Gets the first 100 categories from the blog ordered by their use.
/// </summary>
/// <returns></returns>
public DirectoryResult GetCategories()
{
List<Category> result = client.GetCategories(new CategoriesQuery() { HideEmpty = true, OrderBy = "count", Order="desc", PerPage = 100 }).Result;
var directory = new DirectoryResult();
directory.Items.Add(new DirectoryItem("Categories"));
directory.Items.AddRange(result.Select(c => new DirectoryItem()
{
Description = string.Format("{0} ({1})", c.Name, c.Count),
ItemType = ItemType.DIRECTORY,
Selector = "/category/" + c.Id
}));
return directory;
}
/// <summary>
/// Returns the post as a DirectoryListing for the spcified ID
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public DirectoryResult GetPost(int id)
{
var result = new DirectoryResult();
var post = client.GetPost(new PostQuery { }, id).Result;
result.Items.Add(new DirectoryItem(post.Title.Rendered.CleanHtml()));
result.Items.Add(new DirectoryItem("---"));
result.Items.Add(new DirectoryItem("Author: " + post.Author));
result.Items.Add(new DirectoryItem("Date Posted: " + post.DateGmt.ToString()));
result.Items.Add(new DirectoryItem(" "));
result.Items.Add(new DirectoryItem(ItemType.DOC, "Text Version", "/posts/text/" + id));
result.Items.Add(new DirectoryItem(ItemType.HTML, "Web Link", "URL:" + post.Link));
result.Items.Add(new DirectoryItem("---"));
result.Items.AddRange(post.Content.Rendered.ToDirectoryItems());
result.Items.Add(new DirectoryItem("----------------------------"));
return result;
}
public TextResult GetPostText(int id)
{
var post = client.GetPost(new PostQuery { }, id).Result;
return new TextResult(post.Content.Rendered.CleanHtml().HtmlToText().WrapText(80));
}
/// <summary>
/// Gets the posts for the specified category.
/// </summary>
/// <param name="category"></param>
/// <returns></returns>
public DirectoryResult GetCategoryPosts(int category)
{
var posts = client.GetPosts(new PostsQuery { Categories = new List<int>() { category }, PerPage = 100 }).Result;
return new DirectoryResult(posts.ToDirectoryItems());
}
/// <summary>
/// Converts the specified URL to a GIF
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public ByteResult GetGif(string url)
{
return new ByteResult(ImageToGif.ConvertImageToGif(url), ItemType.GIF);
}
/// <summary>
/// Returns a HTML page to redirect to the propper address
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public UrlResult Redirect(string url)
{
return new UrlResult(url);
}
public DirectoryResult ProxyPage(string url)
{
var result = new DirectoryResult();
var html = HttpHelpers.GetUrl(url);
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(html);
var title = doc.DocumentNode.Descendants("title").SingleOrDefault();
result.Items.Add(new DirectoryItem("Title: " + title));
result.Items.Add(new DirectoryItem("Url: " + url));
result.Items.Add(new DirectoryItem("---------------"));
result.Items.Add(new DirectoryItem(""));
result.Items.AddRange(html.ToDirectoryItems());
return result;
}
}
}