Jump to content

Need help costructing a query


Recommended Posts

Posted

First, thank you for Alfred Remote. Brilliant new app that really has to be used to be appreciated.

 

I'm trying to construct a URL that I can launch with just a touch from the Remote. Easy enough, but the URL involves converting something typed at the keyboard into a coded reference that's then embedded into the URL.

 

I'll simplify it to just he basics on the hopes that someone here can steer me in the right direction.

 

I want to be able to find the data for a specific baseball player easily. The web page to access David Ortiz' statistics, for example, is:

 

http://www.baseball-reference.com/players/o/ortizda01.shtml

 

That last underlined part is the unique part of the URL. I can easily embed that into the URL in Alfred with:

 

http://www.baseball-reference.com/pl/player_search.cgi?search={query}

 

The trouble is, I don't want to have to remember the unique "ortizda01" code to get to Ortiz' page. I'd rather be able to use something friendlier, like "Ortiz." and have that looked up and converted to "ortizda01". Naturally, I'd like to create a whole table of these so that I can access other players just as easily, converting say, "Victorino" into "victosh01", etc.

 

I suspect there's a bit of coding involved here and I don't mind rolling up my sleeves, but I'd appreciate if I could get a high-level overview of the approach that seems best for this kind of thing.

 

Thanks so much in advance.

 

-- Robert

Posted

So do you want a separate button for each player? Or are you going to enter the query after calling the workflow from alfred remote?

Posted

So do you want a separate button for each player? Or are you going to enter the query after calling the workflow from alfred remote?

The latter. I'd like to avoid having buttons for every player. I'd like to type "Ortiz" and have that convert to "ortizda01" or type "Vic" (or whatever abbreviation makes sense in my head) and have that convert to "victosh01."

 

-- Robert

Posted

Well, what you want to do is to create a workflow with a script filter that leads to an Open URL action. But for the remote part, you don't need to do much, just add in a button that runs the AppleScript:

tell application "Alfred 2" to search "KEYWORD"

There, KEYWORD will be the keyword that starts the script filter, and you can start typing from there. You can write the script filter in any language that you want that Alfred supports. I've been writing a lot of PHP recently, so here's a quick version of one that I just whipped up. Note: it might have some syntax errors; I didn't test it.

<?php 

// Include David Ferguson's PHP library
// http://dferg.us/workflows-class/
// download this and place it in the workflow directory
require_once( 'workflows.php' );
// Create a workflow object
$workflow = new Workflow;

$query = '';
if ( isset( $argv[1] ) ) {
	$query = strtolower( trim( $argv[1] ) );
}

if ( empty( $query ) ) {
	// Since the query is empty, we'll just add a message to the script filter
	$workflow->result( '', '', 'Please enter a search string', '', '', 'no' );
	// Print out the script filter xml
	print $workflow->toxml();
	// Exit the script because we're done
	exit(0);
}

if ( ! isset( $_SERVER['alfred_workflow_data'] ) ) {
	// There is a problem here because you're running outside of a workflow environment, so
	// the data directory has not been set. By default, it's:
	// ~/Library/Application Support/Alfred 2/Workflow Data/$BUNDLEID
	// where $BUNDLEID is the bundle id of your workflow.
	// But, the workflow will choke here, so just throw in an exception.
}

// Make sure that the file exists
if ( ! file_exists( $_SERVER['alfred_workflow_data'] ) ) {
	mkdir( $_SERVER['alfred_workflow_data'], 0775, true );
}

// create a file in the workflow
$file = $_SERVER['alfred_workflow_data'] . '/players.json';

if ( ! file_exists( $file ) ) {
	// This creates an empty json file
	file_put_contents( $file, json_encode([]) );
	$players = []; // There are no players
}

// Read the json file into a php array
// I'm assuming that the array will be $code => $your_custom_name
$players = json_decode( file_get_contents( $file ), true );

$base_url = 'http://www.baseball-reference.com/players/';

// This is a really crude search function
foreach ( $players as $code => $player ) :
	// Check if the query is in the player name, if so, add it to the script filter
	if ( false !== strpos( strtolower( $player ), $query ) ) {
		// I'm assuming that the first letter of the code is the next part of the url
		$url = $base_url . substr( strtolower( $code ), 0, 1 ) . '/' . $code . '.shtml';
		$workflow->result( $player, $url, $player, '', '', yes, $player );
	}
endforeach;

// Check if there is anything in the results. If not, add an item that says "bad query"
if ( 0 === count( $workflow->results() ) ) {
	$workflow->result( '', '', 'Query does not match any items', '', '', 'no' );
}

print $workflow->toxml();

// That's it.

So, you'll see that it uses David Ferguson's PHP library to create the XML easier. Also, it depends on a file that exists in the workflow's data directory called 'players.json'. You could change the filename / location. The file expects the JSON to read "code": "player", so, from your examples above:

{
  "ortizda01":"Ortiz",
  "victosh01":"Victorino"
}

So, something like that.

  • 2 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...