Skip to content

Instantly share code, notes, and snippets.

@circulosmeos
Created April 2, 2019 18:53
Show Gist options
  • Save circulosmeos/f2aa84c8cf28f695163f2f59b4c32b0c to your computer and use it in GitHub Desktop.
Save circulosmeos/f2aa84c8cf28f695163f2f59b4c32b0c to your computer and use it in GitHub Desktop.
visualization.shadertoy v3.14 wiki - scripts to automatically obtain shaders' snapshots using webthumb.bluga.net and then, after cut images, produce the wiki listing at https://github.com/circulosmeos/visualization.shadertoy/wiki/Home
#!/usr/bin/env perl
#
# using presets_GLES.json from https://github.com/circulosmeos/visualization.shadertoy v3.14
# extract snapshots from urls to shadertoy.com using webthumb.bluga.net
# (by circulosmeos 2019-04)
#
use strict;
my $LIST_FILE = 'presets_GLES.json';
my $SNAPSHOT_SITE_URL = 'http://webthumb.bluga.net';
my $SHADERS_DIRECTORY = 'shaders';
my $TMP_DIRECTORY = 'tmp';
my $JPEGS_DIRECTORY = 'snapshots';
my $TMP_FILE = 'tmp_file';
my $SLEEP_TIME = 61; # in seconds
my $COOKIE_FILE = 'tmp_cookie';
my $JPEG_HEADER = "--header=\"Referer: $SNAPSHOT_SITE_URL/home\"";
my $HTML_HEADER = "--header=\"Accept: text/html\" " . $JPEG_HEADER;
# https://superuser.com/questions/440721/wget-save-cookies-not-working
#my $WGET_HEADER = "--server-response --keep-session-cookies --load-cookie $COOKIE_FILE --save-cookie $COOKIE_FILE --user-agent=\"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0\"";
my $WGET_HEADER = "--keep-session-cookies --load-cookie $COOKIE_FILE --save-cookie $COOKIE_FILE --user-agent=\"Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:66.0) Gecko/20100101 Firefox/66.0\"";
my ($shader_name, $author_name, $file_name);
my ($i, $line, $retries, $command, $shader_url, $ok, $new_cid, $output_file, $jpeg_url);
open fIn, '<', $LIST_FILE;
$i = 0;
foreach $line (<fIn>) {
if ( $line =~ /"([^\"]+) by ([^\"]+)", *"([^\"]+)"/) {
($shader_name, $author_name, $file_name) = ($1, $2, $3);
print "$shader_name, $author_name, $file_name\n";
$i++;
# sleep some time to not disturb remote sites
sleep $SLEEP_TIME if ($i > 1);
# obtain url from shaders' file
open fShader, '<', $SHADERS_DIRECTORY . '/' . $file_name;
$ok = 0;
foreach (<fShader>) {
if (m#(//www\.shadertoy\.com/view/.{6})#) {
$shader_url = 'https:' . $1;
$ok = 1;
last;
}
}
close fShader;
if (! $ok) {
print "\tNO VALID URL DETECTED !!!\n\n";
next;
}
# read home page from $SNAPSHOT_SITE_URL to obtain new cid value
$command = "wget $HTML_HEADER $WGET_HEADER \"${SNAPSHOT_SITE_URL}/home\" -O $TMP_FILE";
print $command, "\n"; sleep 1;
`$command`;
open fTmp, '<', $TMP_FILE;
$ok = 0;
foreach (<fTmp>) {
if (m#name="cid" value="([^\"]+)"#) {
$new_cid = $1;
$ok = 1;
last;
}
}
close fTmp;
if (! $ok) {
print "\tNO VALID cid DETECTED AT $SNAPSHOT_SITE_URL !!!\n\n";
next;
}
# send post data to $SNAPSHOT_SITE_URL and obtain response
$retries = 0;
while ($retries < 8) {
$output_file = "${TMP_DIRECTORY}/${file_name}.html";
$retries++;
if ($retries == 1) {
$command = "wget $HTML_HEADER $WGET_HEADER --post-data=\"url=$shader_url&width=1280&height=720&cid=$new_cid\" \"${SNAPSHOT_SITE_URL}/home\" -O $output_file";
} else {
$command = "wget $HTML_HEADER $WGET_HEADER --post-data=\"url=$shader_url&width=1280&height=720&cid=$new_cid\" \"${SNAPSHOT_SITE_URL}/pickup?id=$new_cid\" -O $output_file";
}
print $command, "\n"; sleep 1;
`$command`;
open fOutputFile, '<', $output_file;
$ok = 0;
foreach (<fOutputFile>) {
if (m#Thumbnail:.+href="([^\"]+)"#) {
$jpeg_url = $SNAPSHOT_SITE_URL . $1;
$jpeg_url =~ s/ /%20/g;
$ok = 1;
$retries = 8;
#last; # obtain always the last Thumbnail == best resolution
}
}
close fOutputFile;
if (! $ok) {
print "\twaiting for jpeg ...\n\n";
sleep 15;
}
}
if (! $ok) {
print "\tNO VALID jpeg url DETECTED IN $output_file !!!\n\n";
next;
}
# download jpeg image from $jpeg_url
$output_file = "${JPEGS_DIRECTORY}/${file_name}.jpg";
$command = "wget $JPEG_HEADER $WGET_HEADER \"$jpeg_url\" -O $output_file";
print $command, "\n"; sleep 1;
`$command`;
print "\tJPEG downloaded to $output_file\n\n";
}
}
close fIn;
#!/usr/bin/env perl
#
# use presets_GLES.json from https://github.com/circulosmeos/visualization.shadertoy v3.14
# and snapshots in $THUMBS_DIRECTORY to create a wiki page in markdown format
# (by circulosmeos 2019-04)
#
use strict;
my $LIST_FILE = 'presets_GLES.json';
my $SHADERS_DIRECTORY = 'shaders';
my $THUMBS_DIRECTORY = 'thumbs';
my $OUTPUT_FILE = 'wiki.txt';
my $SHADERS_PREFIX = 'https://github.com/circulosmeos/visualization.shadertoy/blob/odroid-c2/visualization.shadertoy/resources/';
my $IMAGES_PREFIX = 'https://circulosmeos.files.wordpress.com/2019/04/';
my $SHADERTOY_PREFIX = 'https://www.shadertoy.com/';
my ($shader_name, $author_name, $file_name, $warning);
my ($i, $retries, $line, $shader_url, $ok, $new_cid, $output_file, $jpeg_url);
open fOut, '>', $OUTPUT_FILE;
open fIn, '<', $LIST_FILE;
$i = 0;
foreach $line (<fIn>) {
if ( $line =~ /"([^\"]+) by ([^\"]+)", *"([^\"]+)"/) {
($shader_name, $author_name, $file_name) = ($1, $2, $3);
$warning = '';
if ( $author_name =~ /(.+) - (.+)/ ) {
$author_name = $1;
$warning = $2;
}
print "$shader_name, $author_name, $file_name\n";
$shader_name =~ s/([\[\]])/\\$1/g;
$i++;
# obtain url from shaders' file
open fShader, '<', $SHADERS_DIRECTORY . '/' . $file_name;
$ok = 0;
$shader_url = '';
foreach (<fShader>) {
if (m#(//www\.shadertoy\.com/view/.{6})#) {
$shader_url = 'https:' . $1;
$ok = 1;
last;
}
}
close fShader;
if (! $ok) {
print "\tNO VALID URL DETECTED !!!\n\n";
#next; # but generate line with available data
}
# check $THUMBS_DIRECTORY for an image for this shader
$ok = 0;
if ( -e $THUMBS_DIRECTORY.'/'.$file_name.'.jpg' ) {
$ok = 1;
}
# create line (with image and link, if thumb is available)
$line = "\* **[$shader_name](${SHADERS_PREFIX}$file_name)** by [$author_name](${SHADERTOY_PREFIX}user/$author_name) ";
# image url is :
# https://circulosmeos.files.wordpress.com/2019/04/2dspectrum.frag_.glsl_.jpg
if ( $ok ) {
$line.= "![$shader_name](${IMAGES_PREFIX}" . lc( $file_name =~ s/(frag)\.(glsl)/${1}\_.${2}\_/r ) . ".jpg)";
} else {
$line.= ' *\[see at shadertoy.com\]*' if $shader_url ne '';
}
if ( $shader_url ne '' ) {
if ( $ok ) {
$line =~ s/(\!\[[^\)]+\))/[\1]($shader_url)/;
} else {
$line =~ s/(\*\\\[.+\\\]\*)/[\1]($shader_url)/;
}
} else {
$line.= " *[No actual url at shadertoy.com]*";
}
if ( $warning ne '' ) {
#$line =~ s/ (\[?\!)/\n**WARNING: $warning** \1/;
$line .= "\n\n **WARNING: $warning**\n";
}
$line.="\n\n***\n\n";
print fOut $line;
}
}
close fIn;
close fOut;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment