Support postimg.org albums; move to Cloudinary

This commit is contained in:
Sakuhl 2018-01-28 15:37:16 +01:00
parent 1c5c6af5f8
commit 9e200d3aa7
2 changed files with 90 additions and 10 deletions

View File

@ -4,9 +4,12 @@ name = "wynnautocrop"
version = "0.1.0"
[dependencies]
base64 = "0.9.0"
image = "0.18.0"
imgur = "0.7.0"
palette = "0.2.1"
regex = "0.2.5"
reqwest = "0.8.2"
rocket = "0.3.5"
rocket_codegen = "0.3.5"
serde_json = "1.0.9"

View File

@ -4,15 +4,25 @@
extern crate rocket;
extern crate reqwest;
use reqwest::{Client, ClientBuilder};
use reqwest::header;
extern crate regex;
use regex::Regex;
extern crate serde_json;
extern crate image;
use image::ImageFormat;
extern crate imgur;
extern crate base64;
//extern crate imgur;
extern crate wynnautocrop;
use wynnautocrop::*;
use std::collections::HashMap;
use std::io::{Cursor, Read};
#[derive(FromForm)]
@ -23,14 +33,81 @@ struct UrlForm {
#[get("/crop?<url>")]
fn crop_url(url: UrlForm) -> String {
let url = url.url;
if url.starts_with("https://postimg.org/gallery/") {
postimg_gallery(url)
} else {
let mut buf = Vec::new();
reqwest::get(&url).unwrap().read_to_end(&mut buf).unwrap();
let mut img = image::load(Cursor::new(&*buf), image::guess_format(&buf).unwrap()).unwrap();
img = crop(img);
let mut buf = Vec::new();
img.save(&mut buf, ImageFormat::PNG).unwrap();
let handle = imgur::Handle::new("c819d6faf0219ac".to_owned());
handle.upload(&buf).unwrap().link().unwrap().to_owned()
//let handle = imgur::Handle::new("c819d6faf0219ac".to_owned());
//handle.upload(&buf).unwrap().link().unwrap().to_owned()
upload_image(&Client::new(), &buf)
}
}
fn postimg_gallery(url: String) -> String {
let html = reqwest::get(&url).unwrap().text().unwrap();
let regex = Regex::new(r"embed_value=(\{.+\});").unwrap();
let regex2 = Regex::new(r#"href="(.*\?dl=1)""#).unwrap();
let json = regex.captures(&html).unwrap().get(1).unwrap().as_str();
let data: serde_json::Value = serde_json::from_str(json).unwrap();
let mut acc = String::new();
let mut headers = header::Headers::new();
headers.set(header::UserAgent::new("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36".to_string()));
let client = ClientBuilder::new()
.default_headers(headers)
.build().unwrap();
//let handle = imgur::Handle::new("c819d6faf0219ac".to_owned());
for key in data.as_object().unwrap().keys() {
let url = format!("https://postimg.org/image/{}/", key);
println!("getting {:?}", url);
let html = reqwest::get(&url).unwrap().text().unwrap();
let url = regex2.captures(&html).unwrap().get(1).unwrap().as_str();
println!("getting {:?}", url);
let mut buf = Vec::new();
loop {
if let Ok(mut res) = client.get(url).send() {
if let Ok(_) = res.read_to_end(&mut buf) {
if let Ok(format) = image::guess_format(&buf) {
if let Ok(mut img) = image::load(Cursor::new(&*buf), format) {
img = crop(img);
let mut buf = Vec::new();
img.save(&mut buf, ImageFormat::PNG).unwrap();
//let upload = handle.upload(&buf).unwrap();
//println!("uploaded to {:?}", upload.link());
//acc += upload.link().unwrap();
let url = upload_image(&client, &buf);
println!("uploaded to {:?}", url);
acc += &url;
acc += " ";
break;
}
}
}
}
buf = Vec::new();
println!("retrying..");
}
}
acc
}
fn upload_image(client: &Client, buf: &[u8]) -> String {
let mut file = "data:image/png;base64,".to_owned();
let mut params = HashMap::new();
params.insert("upload_preset", "unsigned_upload");
let base64 = base64::encode(&buf);
file.push_str(&base64);
params.insert("file", &file);
let json = client.post("https://api.cloudinary.com/v1_1/wynnbotcrop1/upload")
.form(&params)
.send().unwrap().text().unwrap();
let data: serde_json::Value = serde_json::from_str(&json).unwrap();
data.as_object().unwrap().get("secure_url").unwrap().as_str().unwrap().to_owned()
}
fn main() {