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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
//! Library service providers implementation.

use reqwest::{Client, Response};
use reqwest::header::ContentType;
use url::form_urlencoded;


macro_rules! parse_xml_tag {
    ($fname: ident, $tag: expr) => {
        fn $fname(res: &str) -> Option<String> {
            res.split(&format!("<{}>", $tag))
                .nth(1)
                .unwrap_or("")
                .split(&format!("</{}>", $tag))
                .next()
                .map(String::from)
        }
    }
}

macro_rules! parse_json_tag {
    ($fname: ident, $tag: expr, $prefix: expr) => {
        fn $fname(res: &str) -> Option<String> {
            res.to_owned()
                .split(&format!("\"{}\"", $tag))
                .nth(1)
                .unwrap_or("")
                .split(",")
                .next()
                .unwrap_or("")
                .split("\"")
                .nth(1)
                .map(|v| format!("{}{}", $prefix, v.replace("\\", "")))
        }
    }
}

macro_rules! parse {
    ($name:ident) => {
        fn $name(res: &str) -> Option<String> {
            Some(res.to_owned())
        }
    };
}

macro_rules! request {
    ($name:ident, $method:ident, $req_url:expr) => {
        fn $name(url: &str, client: &Client) -> Option<Response> {
            let url = form_urlencoded::byte_serialize(url.as_bytes())
                .collect::<String>();
            let mut client = match client.$method(&format!($req_url, url)) {
                Ok(c) => c,
                _ => return None,
            };
            client.send().ok()
        }
    };

    (B, $name:ident, $method: ident, $req_url:expr, $body:expr) => {
        fn $name(url: &str, client: &Client) -> Option<Response> {
            let mut client = match client.$method($req_url) {
                Ok(c) => c,
                _ => return None,
            };
            client.body(format!($body, url)).send().ok()
        }
    };

    ($name:ident, $method:ident, $req_url:expr, $body:expr, $header:expr) => {
        fn $name(url: &str, client: &Client) -> Option<Response> {
            let mut client = match client.$method($req_url) {
                Ok(c) => c,
                _ => return None,
            };
            client.body(format!($body, url)).header($header).send().ok()
        }
    };
}

/// Used to specify which provider to use to generate a short URL.
#[derive(Clone, Debug)]
pub enum Provider {
    /// http://abv8.me provider
    ///
    /// Notes:
    ///
    /// * You may not shorten more than 20 unique URLs within a 3-minute period.
    /// * You may not shorten more than 60 unique URLs within a 15-minute
    ///   period.
    Abv8,
    /// https://bam.bz provider
    BamBz,
    /// https://bit.ly provider
    BitLy { token: String },
    /// http://bmeo.org provider
    Bmeo,
    /// https://bn.gy provider
    BnGy,
    /// http://fifo.cc provider
    FifoCc,
    /// https://goo.gl provider of Google
    GooGl { api_key: String },
    /// https://hec.su provider
    ///
    /// Notes:
    ///
    /// * Limited to 3000 API requests per day
    HecSu,
    /// http://hmm.rs provider
    HmmRs,
    /// https://is.gd provider
    IsGd,
    /// http://nowlinks.net provider
    NowLinks,
    /// http://phx.co.in provider
    ///
    /// Notes:
    ///
    /// * After some time the service will display ads
    /// * Instead of redirecting, a preview page will be displayed
    /// * Currently unstable
    PhxCoIn,
    /// http://psbe.co provider
    PsbeCo,
    /// http://s.coop provider
    SCoop,
    /// http://rlu.ru provider
    ///
    /// Notes:
    ///
    /// * If you send a lot of requests from one IP, it can be
    ///   blocked. If you plan to add more then 100 URLs in one hour, please let
    ///   the technical support know. Otherwise your IP can be blocked
    ///   unexpectedly. Prior added URLs can be deleted.
    Rlu,
    /// http://sirbz.com provider
    ///
    /// Notes:
    ///
    /// * By default, you are limited to 250 requests per 15 minutes.
    SirBz,
    /// http://tinyurl.com provider
    ///
    /// Notes:
    ///
    /// * This service does not provide any API.
    /// * The implementation result depends on the service result web page.
    TinyUrl,
    /// http://tiny.ph provider
    TinyPh,
    /// http://tny.im provider
    TnyIm,
    /// http://url-shortener.io provider
    UrlShortenerIo,
    /// https://v.gd provider
    VGd,
}

impl Provider {
    /// Converts the Provider variant into its domain name equivilant
    pub fn to_name(&self) -> &str {
        match *self {
            Provider::Abv8 => "abv8.me",
            Provider::BamBz => "bam.bz",
            Provider::BitLy { .. } => "bitly.com",
            Provider::Bmeo => "bmeo.org",
            Provider::BnGy => "bn.gy",
            Provider::FifoCc => "fifo.cc",
            Provider::GooGl { .. } => "goo.gl",
            Provider::HmmRs => "hmm.rs",
            Provider::HecSu => "hec.su",
            Provider::IsGd => "is.gd",
            Provider::NowLinks => "nowlinks.net",
            Provider::PhxCoIn => "phx.co.in",
            Provider::PsbeCo => "psbe.co",
            Provider::SCoop => "s.coop",
            Provider::SirBz => "sirbz.com",
            Provider::Rlu => "rlu.ru",
            Provider::TinyUrl => "tinyurl.com",
            Provider::TinyPh => "tiny.ph",
            Provider::TnyIm => "tny.im",
            Provider::UrlShortenerIo => "url-shortener.io",
            Provider::VGd => "v.gd",
        }
    }
}

/// Returns a vector of all `Provider` variants which do not require authentication.
/// This list is in order of provider quality.
///
/// The providers which are discouraged from use - due to problems such as rate
/// limitations - are at the end of the resultant vector.
///
/// Note that some providers may not provide a generated short URL because the
/// submitted URL may already be short enough and would not benefit from
/// shortening via their service.
pub fn providers() -> Vec<Provider> {
    vec![
        Provider::IsGd,
        Provider::BnGy,
        Provider::VGd,
        Provider::BamBz,
        Provider::TinyPh,
        Provider::FifoCc,
        Provider::SCoop,
        Provider::Bmeo,
        Provider::UrlShortenerIo,
        Provider::HmmRs,

        // The following list are items that have long response sometimes:
        Provider::TnyIm,

        // The following list are items that are discouraged from use:

        // Reasons:
        //
        // * rate limit (250 requests per 15 minutes)
        // * does not accept short urls (ex: http://google.com)
        Provider::SirBz,
        // Reason: rate limit (100 requests per hour)
        Provider::Rlu,
        // Reason: rate limit (3000 requests per day)
        Provider::HecSu,
        // Reason: rate limit (20r/3min; 60r/15min for a UNIQUE urls only)
        Provider::Abv8,
        // Reason: does not provide an api
        Provider::TinyUrl,
        // Reason: unstable work
        Provider::PsbeCo,

        // The following list are items that show previews instead of direct
        // links.
        Provider::NowLinks,
    ]
}

parse!(abv8_parse);
request!(abv8_req, get, "http://abv8.me/?url={}");

parse_json_tag!(bambz_parse, "url", "");
request!(bambz_req,
         post,
         "https://bam.bz/api/short",
         "target={}",
         ContentType::form_url_encoded());

parse!(bitly_parse);
fn bitly_req(url: &str, key: &str, client: &Client) -> Option<Response> {
    let address = format!("https://api-ssl.bitly.com/v3/shorten?access_token={}&longUrl={}&format=txt",
                          key,
                          url);
    if let Ok(mut c) = client.get(&address) {
        c.send().ok()
    } else {
        None
    }
}

parse_json_tag!(bmeo_parse, "short", "");
request!(bmeo_req, get, "http://bmeo.org/api.php?url={}");

parse_xml_tag!(bngy_parse, "ShortenedUrl");
request!(bngy_req, get, "https://bn.gy/API.asmx/CreateUrl?real_url={}");

parse_json_tag!(fifocc_parse, "shortner", "http://fifo.cc/");
request!(fifocc_req, get, "https://fifo.cc/api/v2?url={}");

parse_json_tag!(googl_parse, "id", "");
fn googl_req(url: &str, key: &str, client: &Client) -> Option<Response> {
    let client_post = client.post(&format!("https://www.googleapis.com/urlshortener/v1/url?key={}",
                                           key));
    if let Ok(mut client_post) = client_post {
        client_post.body(format!(r#"{{"longUrl": "{}"}}"#, url))
                   .header(ContentType::json())
                   .send()
                   .ok()
    } else {
        None
    }
}

parse_json_tag!(hmmrs_parse, "shortUrl", "");
request!(hmmrs_req,
         post,
         "http://hmm.rs/x/shorten",
         "url={}",
         ContentType::form_url_encoded());

parse_xml_tag!(hecsu_parse, "short");
request!(hecsu_req, get, "https://hec.su/api?url={}&method=xml");

parse!(isgd_parse);
request!(isgd_req, get, "https://is.gd/create.php?format=simple&url={}");

parse!(nowlinks_parse);
request!(nowlinks_req, get, "http://nowlinks.net/api?url={}");

parse!(phxcoin_parse);
request!(phxcoin_req, get, "http://phx.co.in/shrink.asp?url={}");

parse_xml_tag!(psbeco_parse, "ShortUrl");
request!(psbeco_req, get, "http://psbe.co/API.asmx/CreateUrl?real_url={}");

parse!(scoop_parse);
request!(scoop_req,
         get,
         "http://s.coop/devapi.php?action=shorturl&url={}&format=RETURN");

parse!(rlu_parse);
request!(rlu_req, get, "http://rlu.ru/index.sema?a=api&link={}");

parse_json_tag!(sirbz_parse, "short_link", "");
request!(sirbz_req,
         post,
         "http://sirbz.com/api/shorten_url",
         "url={}",
         ContentType::form_url_encoded());

fn tinyurl_parse(res: &str) -> Option<String> {
    res.split("data-clipboard-text=\"")
        .nth(1)
        .unwrap_or("")
        .split("\">")
        .next()
        .map(String::from)
}
request!(tinyurl_req, get, "http://tinyurl.com/create.php?url={}");

parse_json_tag!(tinyph_parse, "hash", "http://tiny.ph/");
request!(tinyph_req,
         post,
         "http://tiny.ph/api/url/create",
         "url={}",
         ContentType::form_url_encoded());

parse_xml_tag!(tnyim_parse, "shorturl");
request!(tnyim_req, get, "http://tny.im/yourls-api.php?action=shorturl&url={}");

parse!(urlshortenerio_parse);
request!(urlshortenerio_req,
         post,
         "http://url-shortener.io/shorten",
         "url_param={}",
         ContentType::form_url_encoded());

parse!(vgd_parse);
request!(vgd_req, get, "http://is.gd/create.php?format=simple&url={}");

/// Parses the response from a successful request to a provider into the
/// URL-shortened string.
pub fn parse(res: &str, provider: &Provider) -> Option<String> {
    match *provider {
        Provider::Abv8 => abv8_parse(res),
        Provider::BamBz => bambz_parse(res),
        Provider::BitLy { .. } => bitly_parse(res),
        Provider::Bmeo => bmeo_parse(res),
        Provider::BnGy => bngy_parse(res),
        Provider::FifoCc => fifocc_parse(res),
        Provider::GooGl { .. } => googl_parse(res),
        Provider::HmmRs => hmmrs_parse(res),
        Provider::HecSu => hecsu_parse(res),
        Provider::IsGd => isgd_parse(res),
        Provider::NowLinks => nowlinks_parse(res),
        Provider::PhxCoIn => phxcoin_parse(res),
        Provider::PsbeCo => psbeco_parse(res),
        Provider::SCoop => scoop_parse(res),
        Provider::SirBz => sirbz_parse(res),
        Provider::Rlu => rlu_parse(res),
        Provider::TinyUrl => tinyurl_parse(res),
        Provider::TinyPh => tinyph_parse(res),
        Provider::TnyIm => tnyim_parse(res),
        Provider::UrlShortenerIo => urlshortenerio_parse(res),
        Provider::VGd => vgd_parse(res),
    }
}

/// Performs a request to the short link provider.
/// Returns the parsed response on success or a `None` on error.
pub fn request(url: &str,
               client: &Client,
               provider: &Provider)
               -> Option<Response> {
    match *provider {
        Provider::Abv8 => abv8_req(url, client),
        Provider::BamBz => bambz_req(url, client),
        Provider::BitLy { token: ref key } => bitly_req(url, &key, client),
        Provider::Bmeo => bmeo_req(url, client),
        Provider::BnGy => bngy_req(url, client),
        Provider::FifoCc => fifocc_req(url, client),
        Provider::GooGl { api_key: ref key } => googl_req(url, &key, client),
        Provider::HmmRs => hmmrs_req(url, client),
        Provider::HecSu => hecsu_req(url, client),
        Provider::IsGd => isgd_req(url, client),
        Provider::NowLinks => nowlinks_req(url, client),
        Provider::PhxCoIn => phxcoin_req(url, client),
        Provider::PsbeCo => psbeco_req(url, client),
        Provider::SCoop => scoop_req(url, client),
        Provider::SirBz => sirbz_req(url, client),
        Provider::Rlu => rlu_req(url, client),
        Provider::TinyUrl => tinyurl_req(url, client),
        Provider::TinyPh => tinyph_req(url, client),
        Provider::TnyIm => tnyim_req(url, client),
        Provider::UrlShortenerIo => urlshortenerio_req(url, client),
        Provider::VGd => vgd_req(url, client),
    }
}