Skip to content

Instantly share code, notes, and snippets.

@MarkBorcherding
Forked from url2png/url2png-example-c-sharp
Created November 16, 2011 20:37
Show Gist options
  • Save MarkBorcherding/1371299 to your computer and use it in GitHub Desktop.
Save MarkBorcherding/1371299 to your computer and use it in GitHub Desktop.
Example usage of url2png.com automated screenshot API
/* ## # #
# # #######
# # #
# #######
# # # #
# */
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();
}
######
# # # # ##### # #
# # # # # # # #
###### # # ##### #
# # # # # # #
# # # # # # #
# # #### ##### #
# 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
###### # # # # ##### #######
# # # # # ## # # # # #
# # # # # # # # # # #
# # # # # # # # # #### # #
# # # # ####### # # # # # # #
# # # # # # # ## # # # #
###### ##### # # # # ##### #######
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 = str_replace('%','%25',$url);
$url = str_replace(' ','%20',$url);
$url = str_replace('&','%26',$url);
$url = str_replace('#','%23',$url);
$url = str_replace('?','%3F',$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)
#!/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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment