This repository has been archived by the owner on Dec 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FAClient.cs
131 lines (119 loc) · 5.87 KB
/
FAClient.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
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using System.Net;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
namespace FAExportLib {
public class FAClient {
private readonly JsonSerializerSettings _jsonSettings = new JsonSerializerSettings {
ContractResolver = new DefaultContractResolver {
NamingStrategy = new SnakeCaseNamingStrategy()
}
};
/// <summary>
/// Create a client that connects to FAExport without logging in as a particular user.
/// This allows access to accounts and pictures hidden from guests, including mature and adult content, but does not allow posting journals.
/// </summary>
public FAClient() { }
public string UserAgent { get; set; } = "FAClient/0.3 (https://github.com/libertyernie/FAClient)";
protected virtual string GetFACookie() {
return null;
}
private async Task<string> FAExportRequestAsync(string url, bool useCookie = true) {
var request = WebRequest.CreateHttp(url);
request.UserAgent = UserAgent;
if (useCookie) {
var cookie = GetFACookie();
if (cookie != null)
request.Headers.Add("FA_COOKIE", cookie);
}
try {
using (var response = await request.GetResponseAsync())
using (StreamReader sr = new StreamReader(response.GetResponseStream())) {
return await sr.ReadToEndAsync();
}
} catch (WebException ex) {
throw new FAExportException(ex);
}
}
/// <summary>
/// Get information about a user.
/// </summary>
/// <param name="name">A FurAffinity username</param>
public async Task<FAUser> GetUserAsync(string name) {
var json = await FAExportRequestAsync($"https://faexport.boothale.net/user/{WebUtility.UrlEncode(name)}.json");
return JsonConvert.DeserializeObject<FAUser>(json, _jsonSettings);
}
/// <summary>
/// Get a list of submission IDs for a user's gallery, scraps, or favorites.
/// </summary>
/// <param name="username">A FurAffinity username</param>
/// <param name="folder">The folder type (gallery, scraps, or favorites)</param>
/// <param name="page">The page to start at (each page has up to 60 submissions)</param>
public async Task<IEnumerable<int>> GetSubmissionIdsAsync(string username, FAFolder folder, int page = 1) {
var json = await FAExportRequestAsync($"https://faexport.boothale.net/user/{WebUtility.UrlEncode(username)}/{folder.ToString("g")}.json?page={page}&perpage=60");
return JsonConvert.DeserializeObject<IEnumerable<int>>(json, _jsonSettings);
}
/// <summary>
/// Get a list of minimal submission data for a user's gallery, scraps, or favorites.
/// </summary>
/// <param name="username">A FurAffinity username</param>
/// <param name="folder">The folder type (gallery, scraps, or favorites)</param>
/// <param name="page">The page to start at (each page has up to 60 submissions)</param>
public async Task<IEnumerable<FAFolderSubmission>> GetSubmissionsAsync(string username, FAFolder folder, int page = 1) {
var json = await FAExportRequestAsync($"https://faexport.boothale.net/user/{WebUtility.UrlEncode(username)}/{folder.ToString("g")}.json?full=1&page={page}&perpage=60");
return JsonConvert.DeserializeObject<IEnumerable<FAFolderSubmission>>(json);
}
/// <summary>
/// Get a list of submission IDs from a search query.
/// </summary>
public async Task<IEnumerable<int>> SearchSubmissionIdsAsync(
string q,
int page = 1,
FAOrder order_by = FAOrder.date,
FAOrderDirection order_direction = FAOrderDirection.desc,
FARange range = FARange.all,
FASearchMode mode = FASearchMode.extended,
FARating rating = FARating.general | FARating.mature | FARating.adult,
FAType type = FAType.art | FAType.flash | FAType.music | FAType.photo | FAType.poetry | FAType.story
) {
var url = $"https://faexport.boothale.net/search.json?q={WebUtility.UrlEncode(q)}&page={page}&perpage=60&order_by={order_by}&order_direction={order_direction}&range={range}&mode={mode}&rating={rating.ToString().Replace(" ", "")}&type={type.ToString().Replace(" ", "")}";
var json = await FAExportRequestAsync(url);
return JsonConvert.DeserializeObject<IEnumerable<int>>(json, _jsonSettings);
}
/// <summary>
/// Get a list of submissions from a search query.
/// </summary>
public async Task<IEnumerable<FAFolderSubmission>> SearchSubmissionsAsync(
string q,
int page = 1,
FAOrder order_by = FAOrder.date,
FAOrderDirection order_direction = FAOrderDirection.desc,
FARange range = FARange.all,
FASearchMode mode = FASearchMode.extended,
FARating rating = FARating.general | FARating.mature | FARating.adult,
FAType type = FAType.art | FAType.flash | FAType.music | FAType.photo | FAType.poetry | FAType.story
) {
var url = $"https://faexport.boothale.net/search.json?q={WebUtility.UrlEncode(q)}&page={page}&perpage=60&order_by={order_by}&order_direction={order_direction}&range={range}&mode={mode}&rating={rating.ToString().Replace(" ", "")}&type={type.ToString().Replace(" ", "")}&full=1";
var json = await FAExportRequestAsync(url);
return JsonConvert.DeserializeObject<IEnumerable<FAFolderSubmission>>(json, _jsonSettings);
}
/// <summary>
/// Get in-depth data about a particular submission.
/// </summary>
/// <param name="id">The numeric submission ID</param>
public async Task<FASubmission> GetSubmissionAsync(int id) {
var json = await FAExportRequestAsync($"https://faexport.boothale.net/submission/{id}.json", useCookie: false);
return JsonConvert.DeserializeObject<FASubmission>(json, _jsonSettings);
}
/// <summary>
/// Get a list of journals posted by a user.
/// </summary>
/// <param name="username">A FurAffinity username</param>
public async Task<IEnumerable<FAJournal>> GetJournalsAsync(string username) {
var json = await FAExportRequestAsync($"https://faexport.boothale.net/user/{username}/journals.json?full=1");
return JsonConvert.DeserializeObject<IEnumerable<FAJournal>>(json, _jsonSettings);
}
}
}