Skip to content

Instantly share code, notes, and snippets.

@url2png
Created June 4, 2011 09:01
Show Gist options
  • Save url2png/1007737 to your computer and use it in GitHub Desktop.
Save url2png/1007737 to your computer and use it in GitHub Desktop.
Example usage of url2png.com automated screenshot API
' VB.NET Example
' Supplied by Thomas Kristensen
Public Shared Function url2png(ByVal UrlToSite As String) As String
Dim url2pngAPIKey As String = "API_KEY"
Dim url2pngPrivateKey As String = "PRIVATE_KEY"
Dim url As String = HttpUtility.UrlEncode(UrlToSite)
Dim SecurityHash As String = Md5HashPHPCompliant(_url2pngPrivateKey & "+" & url).ToLower
Return "http://api.url2png.com/v3/" & _url2pngAPIKey & "/" & SecurityHash & "/" & "100x100/" & url
End Function
Public Shared Function Md5HashPHPCompliant(ByVal pass As String) As String
Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim dataMd5 As Byte() = md5.ComputeHash(Encoding.UTF8.GetBytes(pass))
Dim sb As New StringBuilder()
For i As Integer = 0 To dataMd5.Length - 1
sb.AppendFormat("{0:x2}", dataMd5(i))
Next
Return sb.ToString()
End Function
/* ## # #
# # #######
# # #
# #######
# # # #
# */
private static string url2png_apikey { get { return "<APIKEY>"; } }
private static string url2png_secret { get { return "<SECRET>"; } }
public static string GetScreenShotByUrl(string url)
{
var token = Md5Hash(string.Format("{0}+{1}",url2png_secret,url));
return "http://api.url2png.com/v3/" + url2png_apikey + "/" + token + "/300x300/" + url;
}
public static string Md5Hash(string foo)
{
MD5 md5 = MD5CryptoServiceProvider.Create();
byte[] dataMd5 = md5.ComputeHash(Encoding.Default.GetBytes(foo));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < dataMd5.Length; i++)
sb.AppendFormat("{0:x2}", dataMd5[i]);
return sb.ToString();
}
###### # # # # ##### #######
# # # # # ## # # # # #
# # # # # # # # # # #
# # # # # # # # # #### # #
# # # # ####### # # # # # # #
# # # # # # # ## # # # #
###### ##### # # # # ##### #######
import hashlib
from django.conf import settings
from django import template
from django.template.defaultfilters import stringfilter, urlencode
register = template.Library()
@register.filter
@stringfilter
def screenshot(url, bounds='300x300'):
"""
Generate the url2png screenshot for a given url.
To use, define URL2PNG_API_KEY and URL2PNG_SECRET in your settings.py,
save this file in a templatetags directory, then load and use as a custom
filter, for instance:
{{ post.url|screenshot:'420x360' }}
See https://docs.djangoproject.com/en/dev/howto/custom-template-tags/ for
instructions on using custom filters.
"""
URL2PNG_API = 'http://api.url2png.com/v3/%(key)s/%(token)s/%(bounds)s/%(url)s'
key = settings.URL2PNG_API_KEY
url = urlencode(url, '')
token = hashlib.md5('%s+%s' % (settings.URL2PNG_SECRET, url)).hexdigest()
return URL2PNG_API % locals()
screenshot.is_safe = True
<?php
##### # # #####
# # # # # #
# # ###### # #
##### # # #####
# # # #
# # # #
DEFINE("API_KEY", "-Your-API-Key-");
DEFINE("SECRET", "-Your-Secret-");
function url2png($url,$size,$api_key,$secret)
{
$url = urlencode(trim($url));
$token = md5("$secret+$url");
return "http://api.url2png.com/v3/$api_key/$token/$size/$url";
}
$size = "300x300";
$url = "nytimes.com";
$src = url2png($url,$size,API_KEY,SECRET);
$img_tag = sprintf("<img src='%s' title='%s'>", $src, $url);
echo $img_tag;
#!/usr/bin/python
######
# # # # ##### # # #### # #
# # # # # # # # # ## #
###### # # ###### # # # # #
# # # # # # # # # #
# # # # # # # # ##
# # # # # #### # #
import hashlib
def url2png(url, bounds, api_key, secret):
token = hashlib.md5( "%s+%s" % (secret, url) ).hexdigest()
return "http://api.url2png.com/v3/%s/%s/%s/%s" % (api_key, token, bounds, url)
api_key="-Your-API-Key-"
secret="-Your-Secret-"
bounds="300x300"
print url2png ("nytimes.com", bounds, api_key, secret)
######
# # # # ##### # #
# # # # # # # #
###### # # ##### #
# # # # # # #
# # # # # # #
# # #### ##### #
# Be sure to checkout https://github.com/wout/url2png-gem
def url2png(url)
safe_url = CGI.escape(url)
token = Digest::MD5.hexdigest("YOUR-SECRET-GOES-HERE+#{safe_url}")
return "http://api.url2png.com/v3/YOUR-API-KEY-GOES-HERE/#{token}/300x300/#{safe_url}"
end
#!/bin/bash
##### ## #### # #
# # # # # # #
##### # # #### ######
# # ###### # # #
# # # # # # # #
##### # # #### # #
#
# Usage
# cd ~/screenshot_via_url2png
# /bin/cat list_of_urls_one_per_line.txt | /bin/bash url2png-example.sh
#
API_KEY=-Your-API-Key-
SECRET=-Your-Secret-
while read url; do
url=${url//%/%25}
url=${url// /%20}
url=${url//&/%26}
url=${url//#/%23}
url=${url//?/%3F}
file=${url//\//-}.png
TOKEN=$(echo -n "$SECRET+$url" | md5sum | cut -d " " -f 1)
wget -O "$file" "http://api.url2png.com/v3/$API_KEY/$TOKEN/300x300/$url"
done
@halilim
Copy link

halilim commented Sep 5, 2011

Why urlencode() is not used in place of those character replaces in the PHP version?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment