TclGoo

Check-in [f1dda5d765]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fetched the shortened version of an URL from Google's shortener
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:f1dda5d765c989c03019b697de027e8ebe77a8c4
User & Date: setok 2014-01-07 18:21:59
Context
2014-02-01
19:57
Removed debug Leaf check-in: d46df15780 user: setok tags: trunk
2014-01-07
18:21
Fetched the shortened version of an URL from Google's shortener check-in: f1dda5d765 user: setok tags: trunk
14:26
initial empty check-in check-in: 2cb365226c user: setok tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added tclGoo.tcl.









































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
## Goo.gl URL shortner

package provide tclgoo 0.1

package require Tcl 8.5
package require http 2.7
package require json 1.1
package require tls 1.6


http::register https 443 tls::socket


namespace eval tclgoo {
    variable apiKey
    
    set GooURL https://www.googleapis.com/urlshortener/v1/url


    proc setApiKey {key} {
        variable apiKey

        set apiKey $key
        return
    }


    proc shortenURL {url} {
        variable GooURL
        variable apiKey

        if {[info exists apiKey]} {
            puts "Got apikey"
            set requestURL "$GooURL?key=$apiKey"
        } else {
            set requestURL $GooURL
        }

        #set queryString [::http::formatQuery longUrl $url]

        append queryString "\{\"longUrl\": " \" $url \"\}

        set httpToken [::http::geturl $requestURL \
                           -query $queryString -type "application/json"]
        set json [::http::data $httpToken]
        ::http::cleanup $httpToken

        #puts "Return: $json"
        set urlData [::json::json2dict $json]
        return [dict get $urlData id]
    }
}

Added test.tcl.







>
>
>
1
2
3
source [file join [file dirname [info script]] tclgoo.tcl]

puts "Short URL: [tclgoo::shortenURL https://holvi.com/]"