Last active
March 6, 2020 22:09
-
-
Save salvatorecapolupo/dee2d8ea010e81d2a3c02297b1adac94 to your computer and use it in GitHub Desktop.
This is an automated heavy metal verses generator, based on a very basic pseudo-crawler (pseudo AI involved). Step 1: select a random band on darklyrics.com Step 2) pick an album 3) extract 3 not contiguous verses 4) show it! This script needs https://sourceforge.net/projects/simplehtmldom/
This file contains hidden or 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 | |
/* | |
Post-Identity Dark Lyrics Generator | |
Author: Salvatore Capolupo | |
Version 1.1 | |
Date: 03/06/2020 | |
Loosely based on Uncreative Writing: Managing Language in the Digital Age by Kenneth Goldsmith | |
*/ | |
include_once ( "simple_html_dom.php" ); | |
function get_http_response( $url ){ | |
$handle = curl_init($url); | |
curl_setopt($handle, CURLOPT_RETURNTRANSFER, TRUE); | |
$response = curl_exec($handle); | |
$httpCode = curl_getinfo($handle, CURLINFO_HTTP_CODE); | |
curl_close($handle); | |
return $httpCode; | |
} | |
//start | |
$start = microtime(true); | |
$letter = chr(rand(97,122)); echo "Selected letter: ".$letter."\n"; | |
$seed_url = "http://www.darklyrics.com/".$letter.".html"; | |
while ( get_http_response($seed_url) != 200 ){ | |
echo get_http_response($seed_url)." "; | |
$letter = chr(rand(97,122));echo "Selected letter: ".$letter."\n"; | |
$seed_url = "http://www.darklyrics.com/".$letter.".html"; | |
} | |
$seed_html = file_get_html( $seed_url ); | |
$tmp = ["div.artists.fr", "div.artists.fl"]; | |
$sub_selector = $tmp[ rand(0,1) ]; | |
foreach($seed_html->find($sub_selector.' a') as $element) | |
{ | |
$selected_band = $element->href; | |
$selected_band_url = "http://www.darklyrics.com/".$selected_band; | |
if ( get_http_response($selected_band_url) == 200 && rand(1,2)==2 ){ | |
break; | |
} | |
} | |
$selected_band_html = file_get_html( $selected_band_url ); | |
foreach( $selected_band_html->find('div.album a') as $element ) | |
{ | |
$selected_album = $element->href; | |
$selected_album_url = "http://www.darklyrics.com/".$selected_album; | |
$selected_album_url = preg_replace( "/\.\.\//","", $selected_album_url); | |
if ( get_http_response($selected_album_url) == 200 && rand(1,2)==2 ){ | |
break; | |
} | |
} | |
$html = file_get_html( $selected_album_url ); | |
foreach($html->find('div.lyrics') as $element) | |
{ | |
$content = $element->plaintext; | |
$rows = explode("\r",$content); | |
$end = 15; | |
$i = rand(0, sizeof($rows)-$end); | |
$j = rand(0, sizeof($rows)-$end); | |
$k = rand(0, sizeof($rows)-$end); | |
while (trim($rows[$i])==""){ | |
$i = rand(0, sizeof($rows)-$end); | |
} | |
while (trim($rows[$j])==""){ | |
$j = rand(0, sizeof($rows)-$end); | |
} | |
while (trim($rows[$k])==""){ | |
$k = rand(0, sizeof($rows)-$end); | |
} | |
$verse1 = preg_replace("/[0-9]+/", "", $rows[$i] ); | |
$verse2 = preg_replace("/[0-9]+/", "", $rows[$j] ); | |
$verse3 = preg_replace("/[0-9]+/", "", $rows[$k] ); | |
echo( $verse1 . "\r".$verse2 . "\r".$verse3 ); | |
} | |
$time_elapsed_secs = round( microtime(true) - $start,2 ); | |
echo ( "\r\r\rTime elapsed: ".$time_elapsed_secs." s." ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of text generated with 4 runs.
_(Another point of view is
Life pointed directions
Haide, nu mai aiuri!
Printre lighioane.
For those universal souls
Screams of a ghoul
Lying to friends
Corrupt them with hate
Thirty thousand dead_