Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2009-01-29 14:03:43

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

[request] Proper last.fm Plugin!

I really hate the fact that Textpattern doesn’t have a decent last.fm plugin already out, it would be really awesome if we could get one that basically added some simple tags and allowed us to display a varying amount of data.

e.g.

<txp:abc_lastfm user=“NAME”>

<txp:abc_lastfm_recentalbums limit=“10” />

</txp:abc_lastfm>

It would be cool to get the artwork from say Amazon, and NOT host on it our own domain/cache.

Thoughts?


~ Cameron

Offline

#2 2009-01-29 14:28:28

candyman
Member
From: Italy
Registered: 2006-08-08
Posts: 684

Re: [request] Proper last.fm Plugin!

+1!

Offline

#3 2009-01-29 15:04:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,250
Website GitHub

Re: [request] Proper last.fm Plugin!

driz wrote:

I really hate the fact that Textpattern doesn’t have a decent last.fm plugin

With this much hate, you have a few options:

  1. Write your own plugin (maybe using rsz_lastfm_charts or lastfm_tagcloud as a basis?)
  2. Put down a bit more detail about what you’d like it to do and why. Get a good discussion going so that a plugin author has something to work on. For example, I haven’t the first clue how LastFM’s API is structured, so a few well-chosen links and/or some well-presented ideas on the sort of things you’d expect to do with the plugin (examples, use cases, features) would help no end in scoping out how big the project should be
  3. Put up some bounty and / or commission someone to write one for you

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#4 2009-01-29 16:09:01

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] Proper last.fm Plugin!

Here is an example plugin that WordPress uses:

<?php

/*
Plugin Name: Last.fm for Wordpress
Version: 1.3.2
Plugin URI: http://rick.jinlabs.com/code/lastfm
Description: Displays your recently listened tracks. Based on <a href="http://cavemonkey50.com/code/pownce/">Pownce for Wordpress</a> by <a href="http://cavemonkey50.com/">Cavemonkey50</a>. 
Author: Ricardo Gonz&aacute;lez
Author URI: http://rick.jinlabs.com/
*/

/*  Copyright 2007  Ricardo González Castro (rick[in]jinlabs.com)

    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
*/

//define('MAGPIE_CACHE_AGE', 120);
define('MAGPIE_CACHE_ON', 0); //2.7 Cache Bug

$lastfm_options['widget_fields']['title'] = array('label'=>'Title:', 'type'=>'text', 'default'=>'');
$lastfm_options['widget_fields']['username'] = array('label'=>'Username:', 'type'=>'text', 'default'=>'');
$lastfm_options['widget_fields']['num'] = array('label'=>'Number of links:', 'type'=>'text', 'default'=>'');
$lastfm_options['widget_fields']['update'] = array('label'=>'Show timestamps:', 'type'=>'checkbox', 'default'=>false);
$lastfm_options['widget_fields']['linked'] = array('label'=>'Linked tracks:', 'type'=>'checkbox', 'default'=>false);
$lastfm_options['widget_fields']['encode_utf8'] = array('label'=>'UTF8 Encode:', 'type'=>'checkbox', 'default'=>false);

$lastfm_options['prefix'] = 'lastfm';


// Display Last.fm recently listened tracks.

function lastfm_tracks($username = '', $num = 5, $list = true, $update = true, $linked  = true, $encode_utf8 = false) {
	global $lastfm_options;
	include_once(ABSPATH . WPINC . '/rss.php');

	$songs = fetch_rss('http://ws.audioscrobbler.com/1.0/user/'.$username.'/recenttracks.rss');

	if ($num <=0) $num = 1;
	if ($num >10) $num = 10;

	if ($list) echo '<ul class="lastfm">';

	if ($username == '') {
		if ($list) echo '<li>';
		echo 'Username not configured';
		if ($list) echo '</li>';
	} else {
			if ( empty($songs->items) ) {
				if ($list) echo '<li>';
				echo 'No recently listened tracks.';
				if ($list) echo '</li>';
			} else {
				foreach ( $songs->items as $song ) {
					$msg = htmlspecialchars($song['title']);
					if($encode_utf8) $msg = utf8_encode($msg);
					$link = $song['link'];

					if ($list) echo '<li class="lastfm-item">'; elseif ($num != 1) echo '<p class="lastfm-track">';
					if ($linked) { 
            echo '<a href="'.$link.'" class="lastfm-link">'.$msg.'</a>'; // Puts a link to the song.
          } else {
            echo $msg; // Only the song, no link.
          }
          if($update) {				
            $time = strtotime($song['pubdate']);

            if ( ( abs( time() - $time) ) < 86400 )
              $h_time = sprintf( __('%s ago'), human_time_diff( $time ) );
            else
              $h_time = date(__('Y/m/d'), $time);

            echo sprintf( '%s' ,' <span class="lastfm-timestamp"><abbr title="' . date(__('Y/m/d H:i:s'), $time) . '">' . $h_time . '</abbr></span>' );
           }   

           if ($list) echo '</li>'; elseif ($num != 1) echo '</p>';

					$i++;
					if ( $i >= $num ) break;
				}
			}	
		}
    if ($list) echo '</ul>';
	}

// lastfm widget stuff
function widget_lastfm_init() {

	if ( !function_exists('register_sidebar_widget') )
		return;

	$check_options = get_option('widget_lastfm');
  if ($check_options['number']=='') {
    $check_options['number'] = 1;
    update_option('widget_lastfm', $check_options);
  }
	function widget_lastfm($args, $number = 1) {

	global $lastfm_options;

		// $args is an array of strings that help widgets to conform to
		// the active theme: before_widget, before_title, after_widget,
		// and after_title are the array keys. Default tags: li and h2.
		extract($args);

		// Each widget can store its own options. We keep strings here.
		include_once(ABSPATH . WPINC . '/rss.php');
		$options = get_option('widget_lastfm');

		// fill options with default values if value is not set
		$item = $options[$number];
		foreach($lastfm_options['widget_fields'] as $key => $field) {
			if (! isset($item[$key])) {
				$item[$key] = $field['default'];
			}
		}

		$songs = fetch_rss('http://ws.audioscrobbler.com/1.0/user/'.$username.'/recenttracks.rss');

		// These lines generate our output.
		echo $before_widget . $before_title . $item['title'] . $after_title;
		lastfm_tracks($item['username'], $item['num'], true, $item['update'], $item['linked'], $item['encode_utf8']);
		echo $after_widget;
	}

	// This is the function that outputs the form to let the users edit
	// the widget's title. It's an optional feature that users cry for.
	function widget_lastfm_control($number) {

		global $lastfm_options;

		// Get our options and see if we're handling a form submission.
		$options = get_option('widget_lastfm');

		if ( isset($_POST['lastfm-submit']) ) {

			foreach($lastfm_options['widget_fields'] as $key => $field) {
				$options[$number][$key] = $field['default'];
				$field_name = sprintf('%s_%s_%s', $lastfm_options['prefix'], $key, $number);

				if ($field['type'] == 'text') {
					$options[$number][$key] = strip_tags(stripslashes($_POST[$field_name]));
				} elseif ($field['type'] == 'checkbox') {
					$options[$number][$key] = isset($_POST[$field_name]);
				}
			}

			update_option('widget_lastfm', $options);
		}

		foreach($lastfm_options['widget_fields'] as $key => $field) {

			$field_name = sprintf('%s_%s_%s', $lastfm_options['prefix'], $key, $number);
			$field_checked = '';
			if ($field['type'] == 'text') {
				$field_value = htmlspecialchars($options[$number][$key], ENT_QUOTES);
			} elseif ($field['type'] == 'checkbox') {
				$field_value = 1;
				if (! empty($options[$number][$key])) {
					$field_checked = 'checked="checked"';
				}
			}

			printf('<p style="text-align:right;" class="lastfm_field"><label for="%s">%s <input id="%s" name="%s" type="%s" value="%s" class="%s" %s /></label></p>',
				$field_name, __($field['label']), $field_name, $field_name, $field['type'], $field_value, $field['type'], $field_checked);
		}
		echo '<input type="hidden" id="lastfm-submit" name="lastfm-submit" value="1" />';
	}

	function widget_lastfm_setup() {
		$options = $newoptions = get_option('widget_lastfm');

		if ( isset($_POST['lastfm-number-submit']) ) {
			$number = (int) $_POST['lastfm-number'];
			$newoptions['number'] = $number;
		}

		if ( $options != $newoptions ) {
			update_option('widget_lastfm', $newoptions);
			widget_lastfm_register();
		}
	}


	function widget_lastfm_page() {
		$options = $newoptions = get_option('widget_lastfm');
	?>
		<div class="wrap">
			<form method="POST">
				<h2><?php _e('last.fm Widgets'); ?></h2>
				<p style="line-height: 30px;"><?php _e('How many last.fm widgets would you like?'); ?>
				<select id="lastfm-number" name="lastfm-number" value="<?php echo $options['number']; ?>">
	<?php for ( $i = 1; $i < 10; ++$i ) echo "<option value='$i' ".($options['number']==$i ? "selected='selected'" : '').">$i</option>"; ?>
				</select>
				<span class="submit"><input type="submit" name="lastfm-number-submit" id="lastfm-number-submit" value="<?php echo attribute_escape(__('Save')); ?>" /></span></p>
			</form>
		</div>
	<?php
	}


	function widget_lastfm_register() {

		$options = get_option('widget_lastfm');
		$dims = array('width' => 300, 'height' => 300);
		$class = array('classname' => 'widget_lastfm');

		for ($i = 1; $i <= 9; $i++) {
			$name = sprintf(__('last.fm #%d'), $i);
			$id = "lastfm-$i"; // Never never never translate an id
			wp_register_sidebar_widget($id, $name, $i <= $options['number'] ? 'widget_lastfm' : /* unregister */ '', $class, $i);
			wp_register_widget_control($id, $name, $i <= $options['number'] ? 'widget_lastfm_control' : /* unregister */ '', $dims, $i);
		}

		add_action('sidebar_admin_setup', 'widget_lastfm_setup');
		add_action('sidebar_admin_page', 'widget_lastfm_page');
	}

	widget_lastfm_register();
}

// Run our code later in case this loads prior to any required plugins.
add_action('widgets_init', 'widget_lastfm_init');



?>

and then the insert into your template is this:

<?php lastfm_tracks("username"); ?>

It should be noted that the above PHP script adds WP Widget functionality as well, so most of that wouldn’t be required for TXP site, and so the plugin in theory would be lighter. x

Last edited by driz (2009-01-29 16:16:45)


~ Cameron

Offline

#5 2009-01-29 20:30:00

masa
Member
From: Asturias, Spain
Registered: 2005-11-25
Posts: 1,091

Re: [request] Proper last.fm Plugin!

Bloke wrote:

Put up some bounty and / or commission someone to write one for you

Yes, that’s the way forward if you want things get done quickly as recent evidence goes to show.

If you need it badly and can’t write your own plugin, your best bet is to round up a few more users interested and willing to chip in.

Offline

#6 2009-01-29 21:25:25

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] Proper last.fm Plugin!

I can appreciate that people would like some reward for their effort, but I’m not really in the position of being able to pay for plugins!
I should imagine that quite a few people would like to see a professional last.fm plugin for TXP! WordPress has loads. Hopefully someone can code one up soon. x


~ Cameron

Offline

#7 2009-01-29 21:52:43

TheEric
Plugin Author
From: Wyoming
Registered: 2004-09-17
Posts: 566

Re: [request] Proper last.fm Plugin!

So, what exactly are the requested features? Just display x-num most recent songs played for y-user ?

Offline

#8 2009-01-29 22:22:34

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] Proper last.fm Plugin!

TheEric wrote:

So, what exactly are the requested features? Just display x-num most recent songs played for y-user ?

Basically YES! But it would be awesome to have some control over what data is shown etc. Most people don’t show the songs they have listened to (but some do) they show the last 5 albums regardless of what songs they have listened from each, be it be one song or the whole album.

As a basic concept, wrapping be in this form:

<txp:abc_lastfm></txp:abc_lastfm>

and then have some additional tags such as:

<txp:abc_lastfm_recentsongs limit=“10” link=“true” />
<txp:abc_lastfm_recentartists />
<txp:abc_lastfm_recentalbums />


~ Cameron

Offline

#9 2009-01-30 00:03:32

zero
Member
From: Lancashire
Registered: 2004-04-19
Posts: 1,470
Website

Re: [request] Proper last.fm Plugin!

I’d like a last.fm plugin to do everything I want without asking too. It should be able to read my mind – judge what music I like, serve up plenty of compatible options for me to choose from – if I can be bothered to click- , show my latest things I’ve listened to, wetc, and of course completey free. Cheers! Oh, and please tell me how it fits in with txp. I use lst fm independently but would love to know how it all fits in with textpattern. Cheers!!! Hic.


BB6 Band My band
Gud One My blog

Offline

#10 2009-01-30 00:21:45

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] Proper last.fm Plugin!

zero wrote:

I’d like a last.fm plugin to do everything I want without asking too. It should be able to read my mind – judge what music I like, serve up plenty of compatible options for me to choose from – if I can be bothered to click- , show my latest things I’ve listened to, wetc, and of course completey free. Cheers! Oh, and please tell me how it fits in with txp. I use lst fm independently but would love to know how it all fits in with textpattern. Cheers!!! Hic.

What’s the point in saying something like that! You drunk? :S


~ Cameron

Offline

#11 2009-01-30 00:29:36

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: [request] Proper last.fm Plugin!

driz wrote:

What’s the point in saying something like that! You drunk? :S

Are you by any means an inteligent individual, respectful and friendly to others, then? Possibly getting the point, or not? That is what zero is talking about, basically, my dear driz. Peace and love, pals.

Last edited by Gocom (2009-01-30 00:35:28)

Offline

#12 2009-01-30 13:13:24

driz
Member
From: Huddersfield, UK
Registered: 2008-03-18
Posts: 441
Website

Re: [request] Proper last.fm Plugin!

Can we just get back on track! Cos otherwise this plugin will never get developed


~ Cameron

Offline

Board footer

Powered by FluxBB