-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
151 lines (135 loc) · 6.04 KB
/
Program.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
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Collections.Specialized;
namespace RedditBackgroundChanger
{
class Token
{
public string Access_token { get; set; }
public int Expires_in { get; set; }
public string Token_type { get; set; }
}
class Program
{
private static string downloadDirectory = @"C:\Users\jkana\AppData\Local\Temp\DesktopChanger\";
static bool inDebug = true;
public static Token GetToken()
{
NameValueCollection settings = ConfigurationManager.AppSettings;
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("https://www.reddit.com/api/v1/access_token");
wr.Method = "POST";
NetworkCredential credentials = new NetworkCredential("5naxj1FRo3ooIQ", settings["secret"]);
wr.Credentials = credentials;
using (Stream dataStream = wr.GetRequestStream())
{
string postData = $"grant_type=password&username={settings["username"]}&password={settings["password"]}";
byte[] postDataByteArray = Encoding.UTF8.GetBytes(postData);
dataStream.Write(postDataByteArray, 0, postDataByteArray.Length);
}
try
{
using (WebResponse response = wr.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseFromServer = reader.ReadToEnd();
JObject redditToken = JObject.Parse(responseFromServer);
JToken jToken = redditToken;
Token myToken = jToken.ToObject<Token>();
return myToken;
}
}
}
}
catch (WebException e)
{
Console.WriteLine(e.Message);
return new Token();
}
}
public static List<string> GetPosts(Token token, bool over18Allowed = false)
{
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create("https://oauth.reddit.com/r/wallpapers/new?limit=100");
wr.Method = "GET";
wr.UserAgent = "DesktopWallpaperChanger/0.1 by Airspace";
wr.Headers["Authorization"] = $"{token.Token_type} {token.Access_token}";
try
{
using (WebResponse response = wr.GetResponse())
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream))
{
string responseFromServer = reader.ReadToEnd();
JObject redditPosts = JObject.Parse(responseFromServer);
List<JToken> results = redditPosts["data"]["children"].Children().ToList();
List<string> posts = new List<string>();
foreach(JToken result in results)
{
bool postIsOver18 = (bool)result["data"]["over_18"];
if(over18Allowed || !postIsOver18)
{
bool isSelfPosted = (bool)result["data"]["is_self"];
bool possiblyAlbum = (result["data"]["thumbnail_height"].ToString() == "");
if (!isSelfPosted && !possiblyAlbum)
{
int height = (int)result["data"]["preview"]["images"][0]["source"]["height"];
int width = (int)result["data"]["preview"]["images"][0]["source"]["width"];
if (width % 16 == 0 && height % 9 == 0)
posts.Add(result["data"]["url"].ToString());
}
}
}
return posts;
}
}
}
}
catch(WebException)
{
return new List<string>();
}
}
public static int ChooseRandomPost(int numberOfPosts)
{
Random rnd = new Random();
return rnd.Next(0, numberOfPosts);
}
public static void DownloadImageFromPost(string address, string fileType)
{
WebClient webClient = new WebClient();
Directory.CreateDirectory(downloadDirectory);
webClient.DownloadFile(address, $"{downloadDirectory}background." + fileType);
}
static void Main(string[] args)
{
Token myToken = GetToken();
if(!String.IsNullOrEmpty(myToken.Access_token) && myToken.Expires_in > 0)
{
List<string> posts = GetPosts(myToken, true);
if(posts.Count > 0)
{
int postNumber = ChooseRandomPost(posts.Count);
string fileType = posts[postNumber].Substring(posts[postNumber].Length - 3);
DownloadImageFromPost(posts[postNumber], fileType);
if (inDebug)
{
DebugLogging.WriteToLog($"Number of Posts: {posts.Count}\nDownloaded Post: {posts[postNumber]}\nFile Type: {fileType}");
}
Wallpaper.SetDesktopWallpaper($"{downloadDirectory}background." + fileType, WallpaperStyle.Stretch);
}
}
}
}
}