-
-
Save mpdaugherty/3774987 to your computer and use it in GitHub Desktop.
v6 Examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function url2png_v6($url, $args) { | |
# Get your apikey from http://url2png.com/plans | |
$URL2PNG_APIKEY = "PXXXX"; | |
$URL2PNG_SECRET = "SXXXX"; | |
# urlencode request target | |
$options['url'] = urlencode($url); | |
$options += $args; | |
# create the query string based on the options | |
foreach($options as $key => $value) { $_parts[] = "$key=$value"; } | |
# create a token from the ENTIRE query string | |
$query_string = implode("&", $_parts); | |
$TOKEN = md5($query_string . $URL2PNG_SECRET); | |
return "http://beta.url2png.com/v6/$URL2PNG_APIKEY/$TOKEN/png/?$query_string"; | |
} | |
# usage | |
$options['force'] = 'false'; # [false,always,timestamp] Default: false | |
$options['fullpage'] = 'false'; # [true,false] Default: false | |
$options['thumbnail_max_width'] = 'false'; # scaled image width in pixels; Default no-scaling. | |
$options['viewport'] = "1280x1024"; # Max 5000x5000; Default 1280x1024 | |
$src = url2png_v6("google.com", $options); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
from urllib import urlencode | |
def url2png(url, api_key, secret, fullpage=None, max_width=None, | |
force=None, viewport_width=1280, viewport_height=1024): | |
data = { | |
'url': url, | |
'fullpage': 'true' if fullpage else 'false', | |
'thumbnail_max_width': max_width, | |
'force': force, | |
'viewport': '{}x{}'.format(viewport_width, viewport_height), | |
} | |
filtered_data = dict((opt, data[opt]) for opt in data if data[opt]) | |
query_string = urlencode(filtered_data) | |
token = hashlib.md5('{}{}'.format(query_string, secret)).hexdigest() | |
return "http://beta.url2png.com/v6/{}/{}/png/?{}".format(api_key, token, query_string) | |
api_key="PXXXX" | |
secret="SXXXX" | |
print url2png ("http://google.com/", api_key, secret) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cgi' unless defined?(CGI) | |
require 'digest' unless defined?(Digest) | |
def url2png_v6(url, options={}) | |
url2png_apikey = 'PXXXX' | |
url2png_secret = 'SXXXX' | |
query = { | |
:url => url, | |
:force => options[:force], # [false,always,timestamp] Default: false | |
:fullpage => options[:fullpage], # [true,false] Default: false | |
:max_width => options[:max_width], # scaled img width px; Default no-scaling | |
:viewport => options[:viewport], # Max 5000x5000; Default 1280x1024 | |
} | |
query_string = query. | |
sort_by {|s| s[0].to_s }. # sort query by keys for uniformity | |
select {|s| s[1] }. # skip empty options | |
map {|s| s.map {|v| CGI::escape(v.to_s) }.join('=') }. # escape keys & vals | |
join('&') | |
token = Digest::MD5.hexdigest(query_string + url2png_secret) | |
"http://beta.url2png.com/v6/#{url2png_apikey}/#{token}/png/?#{query_string}" | |
end | |
puts url2png_v6 "http://www.google.com" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment