Robert McLeod
Description: Gets all the artists from your profile on ilike and displays them on a page of your choice.
*/
/* Copyright 2008 Robert McLeod (email : hamstar@telescum.co.nz)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
function get_image($file, $local_path, $newfilename) {
# From http://www.weberdev.com/get_example-4009.html (modified slightly)
$err_msg = '';
$out = fopen($local_path.$newfilename, 'wb');
if ($out == FALSE){
# Opening the file failed
return false;
exit;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_FILE, $out);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_URL, $file);
curl_exec($ch);
if(curl_error ($ch)) {
return false;
exit;
}
//fclose($handle);
curl_close($ch);
return true;
}//end function
if (!class_exists("Artists_ilikePlugin")) {
class Artists_ilikePlugin {
var $adminOptionsName = 'ArtistsilikeAdminOptions';
function Artists_ilikePlugin() { //constructor
}
function generateArtistsilike($type='') {
# Include our classes
include_once('php/simple_html_dom.php');
include_once('php/curl.php');
# Set some variables
$user = get_option("aiUser");
$url = "http://ilike.com/user/$user/fan_of";
$artists = '';
$eof = 0;
$count = 1;
if($user) {
# Create a new curl object
$curl = new curl();
if(empty($curl)) { echo 'curl is empty'; }
# Recurse through all the pages
while($eof == 0) {
# Get the url with curl
$response = $curl->get("$url?page=$count");
# Make a html_dom object from our curl response
$html = str_get_html($response->body);
# Check that there are artists to get
if(strstr($html,'hasn\'t told us which artists they like yet.')) {
$eof = 1;
} else {
# Search the html for ul
foreach($html->find('ul') as $elem) {
# The artists are kept in the artist_list class
if($elem->class == 'artist_list') {
# Add them to the artists string
$artists .= $elem->innertext;
}
}
}
# Increment the counter
$count++;
}
# Need to modify the a element
$artists = str_get_html($artists);
foreach($artists->find('a') as $a) {
# Turn all the links to ilike into absolute
$href=$a->href;
$a->href = "http://ilike.com$href";
# Try and download the image to the local server
# Get the remote image url from the style
$style = $a->style;
preg_match('/http.+jpg/',$style,$matches);
$url = $matches[0];
$filename = basename($url);
$saveto = dirname(__FILE__).'/thumbs/';
# Try to download the file
if(!file_exists($saveto.$filename)) {
$result = get_image($url,$saveto,$filename);
} else {
$result = true;
}
# If the download worked, try to change the image link in style
# to the one on the local server, otherwise leave it as it was
if($result) {
# Get the web-relative location for the thumbs folder
preg_match('/(\/wp-content.+)/',dirname(__FILE__),$matches);
$fullname = get_option('home') . $matches[1] . "/thumbs/$filename";
# Replace the link
$newstyle = preg_replace('/http.+jpg/',$fullname ,$style);
$a->style = $newstyle;
}
if($type == 'collage') {
$a->style = '';
$a->title = strip_tags($a->innertext);
$a->innertext = "";
}
}
# Remove the clear left div
str_replace('
'; _e("Username Updated.","Artists_ilikePlugin"); echo '
'; #_e("No such user at ilike. Please check your username.","Artists_ilikePlugin"); #echo '
'; _e("Please set a username first.","Artists_ilikePlugin"); echo '
'; _e("Please wait... ","Artists_ilikePlugin"); $type = ($_POST['aiCollage']=='on') ? 'collage' : ''; $map = $this->generateArtistsilike($type); ($_POST['aiCollage']) ? update_option('aiCollage','checked') : update_option('aiCollage',''); update_option('aiArtistMap',$map); update_option('aiLastMapGenerated',time()); _e("Artist Map Regenerated","Artists_ilikePlugin"); echo '