Skip to content

Commit

Permalink
Commit working state of hypem resolver, needs a cleanup, especially t…
Browse files Browse the repository at this point in the history
…he cookie handling.
  • Loading branch information
David Singleton committed Apr 21, 2009
1 parent 77215e4 commit 5ad4dc4
Showing 1 changed file with 54 additions and 15 deletions.
69 changes: 54 additions & 15 deletions etc/hypem-resolver.php 100644 → 100755
@@ -1,39 +1,85 @@
#!/usr/local/php5/bin/php
<?php

require_once dirname(__FILE__) . '/phpresolver/playdarresolver.php';

/**
* A resolver for MP3s from blogs indexed by Hype Machine
* @author David Singleton (http://dsingleton.co.uk)
* @todo Get the cookie once only, cache and re-send.
*/
class HypeMachineResolver extends PlaydarResolver
{
protected $name = 'HypeMachine Resolver';
protected $targetTime = 150; // Rough guess for a remote service
protected $weight = 70;
protected $targetTime = 5000; // Rough guess for a remote service
protected $weight = 60;

private $cookie = array();

public function resolve($request)
{
$source = sprintf('http://hypem.com/search/"%s"+"%s"/', urlencode($request->artist), urlencode($request->track));

// $html = file_get_contents("test/hypem.html");
// We need to grab the AUTH cookie we get issued here.
$html = file_get_contents($source);
$ch = curl_init();

preg_match_all('/\({.*?}\)/s', $html, $matches);
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, 'Playdar 0.1');

$results = array();
$x = curl_exec($ch);
curl_close($ch);

if (!$x) {
// you are offline...
return array();
}

$headers = array();

list($head, $html) = explode("\r\n\r\n", $x, 2);

$aHeaders = explode("\r\n", $head);
array_shift($aHeaders);

foreach($aHeaders as $header) {
list($key, $val) = explode(": ", $header);
$headers[$key] = $val;
}
list($cookies) = explode(";", $headers['Set-Cookie']);
list($key, $val) = explode("=", $cookies);
$aCookies = array($key => $val);

$auth = $aCookies['AUTH'];
// $auth = 'XXXX';

preg_match_all('/\({.*?}\)/s', $html, $matches);
$results = array();
foreach($matches[0] as $match) {
// We might want to filter out some of the fields, they're mostly useless.
$json = $this->reformatHypeJSON(substr($match, 2, -2));
$result = json_decode($json);

if (!$result) {
continue;
}
$result->score = $this->calculateRelevancy($request, $result);
$result->url = sprintf("http://hypem.com/serve/play/%s/%s", $result->id, $result->key);
$result->track = $result->song;

$result->duration = (int) $result->time;
$result->extra_headers = array("Cookie: AUTH=$auth");
$result->source = "Hype Machine";
// Use the unique id as a fraction to vary keys where the score is the same.
$key = round($result->score) . "-{$result->id}";

$result->bitrate = 128;
$results[$key] = $result;
}

krsort($results, SORT_NUMERIC);
$results = array_values(array_slice($results, 0, 2));

return $results;
}

Expand Down Expand Up @@ -71,14 +117,7 @@ private function calculateRelevancy($request, $result)
}

// Out of 100
return ($artistSimilarity + $trackSimilarity) / 2;
}

function proxy($url)
{
// get cookie val
// make request
// passthru not download
return ($artistSimilarity + $trackSimilarity) / 200;
}
}

Expand Down

0 comments on commit 5ad4dc4

Please sign in to comment.