-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.rs
329 lines (284 loc) · 9.21 KB
/
main.rs
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
extern crate reqwest;
use reqwest::Url;
#[macro_use]
extern crate hyper;
use hyper::header::{Headers, Authorization};
extern crate serde;
extern crate serde_json;
extern crate rand;
use rand::Rng;
#[macro_use]
extern crate serde_derive;
extern crate glib;
extern crate gio;
use gio::SettingsExt;
use glib::Variant;
extern crate clap;
use clap::{App, Arg};
use std::fs::File;
use std::path::Path;
use std::error::Error;
use std::io::{Write, copy};
use std::process::Command;
#[derive(Debug, Serialize, Deserialize)]
struct Urls {
raw: String,
full: String,
regular: String,
small: String,
thumb: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct Category {
id: String,
title: String,
photo_count: u32,
links: Links,
}
#[derive(Debug, Serialize, Deserialize)]
struct Exif {
make: Option<String>,
model: Option<String>,
exposure_time: Option<String>,
aperture: Option<String>,
focal_length: Option<String>,
iso: Option<u16>,
}
#[derive(Debug, Serialize, Deserialize)]
struct Links {
#[serde(rename = "self")]
_self: String,
html: String,
photos: Option<String>,
likes: Option<String>,
portfolio: Option<String>,
download: Option<String>,
download_location: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
struct ProfileImage {
small: String,
medium: String,
large: String,
}
#[derive(Debug, Serialize, Deserialize)]
struct User {
id: String,
username: String,
name: Option<String>,
first_name: Option<String>,
last_name: Option<String>,
updated_at: Option<String>,
twitter_username: Option<String>,
portfolio_url: Option<String>,
bio: Option<String>,
location: Option<String>,
total_likes: u32,
total_photos: u32,
total_collections: u32,
profile_image: ProfileImage,
links: Links,
}
#[derive(Debug, Serialize, Deserialize)]
struct Position {
latitude: Option<f64>,
longitude: Option<f64>,
}
#[derive(Debug, Serialize, Deserialize)]
struct Location {
city: Option<String>,
country: Option<String>,
position: Position,
}
#[derive(Debug, Serialize, Deserialize)]
struct Collection {
id: u32,
title: String,
published_at: String,
updated_at: String,
curated: bool,
cover_photo: CoverPhoto,
user: User,
links: Links,
}
#[derive(Debug, Serialize, Deserialize)]
struct CoverPhoto {
id: String,
width: u16,
height: u16,
color: String,
likes: u32,
liked_by_user: bool,
description: Option<String>,
user: User,
urls: Urls,
categories: Vec<Category>,
links: Links,
}
#[derive(Debug, Serialize, Deserialize)]
struct UnsplashFoto {
id: String,
created_at: String,
updated_at: String,
width: u16,
height: u16,
color: String,
downloads: u32,
likes: u32,
liked_by_user: bool,
description: Option<String>,
exif: Exif,
location: Option<Location>,
current_user_collections: Vec<Collection>,
urls: Urls,
categories: Vec<Category>,
links: Links,
user: User,
slug: Option<String>,
}
header! { (AcceptVersion, "Accept-Version") => [String] }
const CLIENT_ID: &'static str = "Client-ID ee88235a89c58088c3ebf8025e90214c4574909913e0b7442165f4f87452384e";
fn main() {
let mut headers = Headers::new();
let client = reqwest::Client::new();
headers.set(Authorization(CLIENT_ID.to_owned()));
headers.set(AcceptVersion("v1".to_owned()));
let matches = App::new("unsplash")
.version("0.1.0")
.about(
"Small utility that queries the Unsplash API for a random picture,
which it then sets as your background wallpaper.",
)
.arg(
Arg::with_name("subject")
.help("Subject of the picture.")
.short("s")
.long("subject")
.takes_value(true),
)
.arg(
Arg::with_name("resolution")
.help("horizontal resolution of picture. Either hd or 4k. Defaults to 4k")
.short("r")
.long("resolution")
.takes_value(true)
)
.get_matches();
let mut params = vec![("orientation", "landscape")];
let query = match matches.value_of("subject") {
Some(s) => s,
None => "space stars",
};
params.push(("query", query));
let resolution = match matches.value_of("resolution") {
None => "3840", // empty default
Some(i) => match i { // inner match on unwraped value
"hd" => "1920",
"4k" | _ => "3840", // default and also 4k
}
};
params.push(("w", resolution));
//println!("params are {:?}", params);
let url = Url::parse_with_params("https://api.unsplash.com/photos/random", ¶ms).unwrap();
// use xdg pictures directory to save images
let output = Command::new("xdg-user-dir")
.arg("PICTURES")
.output()
.expect("failed to read PICTURES from xdg-user-dir");
let input_path = String::from_utf8_lossy(&output.stdout);
let root_path = input_path.trim_right_matches('\n');
println!("XDG_PICTURES_DIR is {:?}", root_path);
let resp = client.get(url).headers(headers).send();
let json: UnsplashFoto = match resp {
Ok(mut response) => {
match response.json() {
Ok(result) => result,
Err(e) => {
// let's keep the jsons responses that error
// to build tests later
let name_rnd: String = rand::thread_rng().gen_ascii_chars().take(10).collect();
let full_name = &["jsons", &name_rnd].join("/");
let path = Path::new(full_name);
let mut buf: Vec<u8> = vec![];
response.copy_to(&mut buf).unwrap();
let mut file = match File::create(&path) {
Err(why) => {
panic!(
"could not create {:?}: {:?}",
path.display(),
why.description()
)
}
Ok(file) => file,
};
match file.write_all(buf.as_slice()) {
Err(why) => {
panic!(
"could not write to {:?}: {:?}",
path.display(),
why.description()
)
}
Ok(_) => println!("wrote faulty json to {:?}", path.display()),
};
panic!(
"JSON PARSE ERROR: {:?} \n {:?}",
e,
response.text().unwrap()
)
}
}
}
Err(e) => panic!("NETWORK ERROR: {:?}", e),
};
match client.get(&json.urls.full).send() {
Ok(mut response) => {
// get respones bytes
// and write them to file
let name_rnd: String = rand::thread_rng().gen_ascii_chars().take(10).collect();
let full_name = &[root_path, "backgrounds", &name_rnd].join("/");
let file_url =
Variant::from(&["file:/", root_path, "backgrounds", &name_rnd].join("/"));
let path = Path::new(full_name);
let mut file = match File::create(&path) {
Err(why) => {
panic!(
"could not create {:?}: {:?}",
path.display(),
why.description()
)
}
Ok(file) => file,
};
match copy(&mut response, &mut file) {
Err(why) => {
panic!(
"could not write to {:?}: {:?}",
path.display(),
why.description()
)
}
Ok(_) => {
println!("wrote image to {:?}", path.display());
let settings = gio::Settings::new("org.gnome.desktop.background");
settings.set_value("picture-uri", &file_url);
gio::Settings::sync();
match json.links.download_location {
Some(download_location) => {
// ping download location per api guidelines
let mut headers = Headers::new();
headers.set(Authorization(CLIENT_ID.to_owned()));
headers.set(AcceptVersion("v1".to_owned()));
match client.get(&download_location).headers(headers).send() {
Ok(_) => println!("Pinged API for download"),
Err(_) => println!("Network error while pinging API for download"),
}
}
None => println!("Response did not contain download link"),
}
}
};
}
Err(why) => panic!("NETWORK ERROR: {:?}", why.description()),
};
}