-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEPicture_Imgur.cs
executable file
·166 lines (151 loc) · 6.08 KB
/
EPicture_Imgur.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
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace EPicture
{
public class Imgur_GetAlbum
{
public Album data;
public bool success;
public int status;
}
public class Imgur_GetGallery
{
public List<GalleryImage> data;
public bool success;
public int status;
}
public class Imgur_AllImages
{
public List<ImgurImage> data;
public bool success;
public int status;
}
public class Imgur_GetUserFavorites
{
public List<GalleryImage> data;
public bool success;
public int status;
}
public class Imgur_Error
{
public string error { get; set; }
public string request { get; set; }
public string method { get; set; }
}
public class Imgur_Request
{
public Imgur_Error data { get; set; }
public bool success { get; set; }
public int status { get; set; }
}
public class EPicture_Imgur
{
public string app_id = "8e0148d95c9866b";
public string secret = "46eba94c9f31b5b053a9f2aa2cb289845a454b7e";
public string access_token { get; set; }
public int expires_in { get; set; }
public string token_type { get; set; }
public object scope { get; set; }
public string refresh_token { get; set; }
public int account_id { get; set; }
public string account_username { get; set; }
public async Task<string> HttpGet(string url)
{
string ret;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.access_token);
using (HttpResponseMessage response = await client.GetAsync(url))
{
using (HttpContent content = response.Content)
{
ret = await content.ReadAsStringAsync();
}
}
}
return ret;
}
public async Task<string> HttpDelete(string url)
{
string ret;
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.access_token);
using (HttpResponseMessage response = await client.DeleteAsync(url))
{
using (HttpContent content = response.Content)
{
ret = await content.ReadAsStringAsync();
}
}
}
return ret;
}
public async Task<Album> getAlbum(string album_id)
{
string ret = await this.HttpGet("https://api.imgur.com/3/album/" + album_id);
return JsonConvert.DeserializeObject<Imgur_GetAlbum>(ret).data;
}
public async Task<List<ImgurImage>> getAllUserImages(int page = 0)
{
string ret = await this.HttpGet("https://api.imgur.com/3/account/" + this.account_username + "/images/" + page.ToString());
return JsonConvert.DeserializeObject<Imgur_AllImages>(ret).data;
}
public async Task<List<ImgurImage>> getFavorites()
{
string ret = await this.HttpGet("https://api.imgur.com/3/account/" + this.account_username + "/favorites/");
return Convert(JsonConvert.DeserializeObject<Imgur_GetUserFavorites>(ret).data);
}
public async Task RemoveFavorite(string picture_id)
{
await this.HttpDelete("https://api.imgur.com/3/image/" + picture_id + "/favorite");
}
public async Task RemoveImage(string picture_id)
{
await this.HttpDelete("https://api.imgur.com/3/image/" + picture_id);
}
public async Task<List<ImgurImage>> SearchGallery(string search)
{
string ret = await this.HttpGet("https://api.imgur.com/3/gallery/search?q=" + search);
return Convert(JsonConvert.DeserializeObject<Imgur_GetUserFavorites>(ret).data);
}
public async Task AddFavorite(string picture_id)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.access_token);
HttpResponseMessage response = await client.PostAsync(new Uri("https://api.imgur.com/3/image/" + picture_id + "/favorite"), null);
string ret = await response.Content.ReadAsStringAsync();
}
}
public async Task<List<ImgurImage>> getGallery(string section = null, string sort = null, int page = 3, string window = null, bool showViral = true)
{
string url = "https://api.imgur.com/3/gallery/";
// hot | top | user_id - Defaults : hot
if (section != null) { url = url + section + "/"; } else { url = url + "hot/"; }
// viral | top | time (only with user) - Defaults : viral
if (sort != null) { url = url + sort + "/"; } else { url = url + "viral/"; }
// number
url = url + page.ToString() + "/";
// day | week | month | year | all
if (window != null) { url = url + window + "/"; } else { url = url + "day/"; }
// true | false
url = url + showViral.ToString();
string ret = await this.HttpGet(url);
return Convert(JsonConvert.DeserializeObject<Imgur_GetGallery>(ret).data);
}
public List<ImgurImage> Convert(List<GalleryImage> list)
{
List<ImgurImage> ret = new List<ImgurImage>();
foreach (GalleryImage img in list)
{
ret.Add(new ImgurImage(img));
}
return ret;
}
}
}