Skip to content

Commit

Permalink
add user get all ratings method
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikulas committed Feb 14, 2014
1 parent fe415ab commit 175e86b
Show file tree
Hide file tree
Showing 8 changed files with 167 additions and 2 deletions.
52 changes: 52 additions & 0 deletions src/Csfd/Collections/Rating.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

namespace Csfd\Collections;

use Csfd\Entities\Movie;
use DateTime;


class Rating
{

/** @var Csfd\Entities\Movie */
private $movie;

/** @var 0..5 */
private $rating;

/** @var DateTime */
private $date;


public function __construct(Movie $movie, $rating, DateTime $date)
{
if (!is_integer($rating) || $rating < 0 || $rating > 5)
{
throw new RatingException('Rating must be an integer in range 0..5');
}

$this->movie = $movie;
$this->rating = (int) $rating;
$this->date = $date;
}

/** @return Csfd\Entities\Movie */
public function getMovie()
{
return $this->movie;
}

/** @return int 0..5 */
public function getRating()
{
return $this->rating;
}

/** @return DateTime */
public function getDate()
{
return $this->date;
}

}
8 changes: 8 additions & 0 deletions src/Csfd/Collections/RatingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Csfd\Collections;

use Csfd\Exception;


class RatingException extends Exception {}
21 changes: 21 additions & 0 deletions src/Csfd/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Csfd\Entities;

use Csfd\Collections\Ratings;


/**
* @method string getProfile() html
Expand All @@ -26,4 +28,23 @@ protected function getUrlKey($property)
return 'profile';
}

/**
* @param int $page 1..n
* @return Rating[]
*/
public function getRatings($page)
{
$vars = ['entityId' => $this->id, 'page' => $page];
$html = $this->request($this->getUrl('ratings', $vars))->getContent();

$ratings = [];
foreach ($this->getParser()->getRatings($html) as list($id, $rating, $date))
{
$movie = $this->getRepository('movies')->get($id);
$ratings[] = new Rating($movie, $rating, $date);
}

return $ratings;
}

}
13 changes: 13 additions & 0 deletions src/Csfd/Parsers/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ protected function getNode($html, $xpath)
}
return $filtered;
}

/** alias */
protected function getNodes($html, $xpath)
{
return $this->getNode($html, $xpath);
}

public function getFormToken($html, $formId)
{
Expand All @@ -73,6 +79,13 @@ protected function parseCzechDateTime($string)
return DateTime::createFromFormat('j.n.Y*H:i', $string);
}

protected function parseCzechDate($string)
{
$date = DateTime::createFromFormat('j.n.Y', $string);
$date->setTime(0, 0, 0); // not current time
return $date;
}

/**
* @param string $css
* @return string url
Expand Down
24 changes: 24 additions & 0 deletions src/Csfd/Parsers/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Csfd\Parsers;

use Symfony\Component\DomCrawler\Crawler;


class User extends Parser
{
Expand Down Expand Up @@ -170,4 +172,26 @@ public function getAvatarUrl($html)
return $this->normalizeUrl($url);
}

public function getRatings($html)
{
try {
$nodes = $this->getNodes($html, '//*[@class="profile-content ratings"]//tbody/tr');
} catch (Exception $e) {
return []; // user has no ratings
}

$ratings = $nodes->each(function(Crawler $row) {
$id = $this->getIdFromUrl($row->filterXPath('//td[1]/a')->attr('href'));

$node = $row->filterXPath('//td[2]/img');
$rating = $node->count() ? strlen($node->attr('alt')) : 0;

$date = $this->parseCzechDate($row->filterXPath('//td[3]')->text());

return [$id, $rating, $date];
});

return $ratings;
}

}
1 change: 1 addition & 0 deletions src/Csfd/urls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ entities:
profile: 'uzivatel/{$entityId}/'
profileToken: 'uzivatel/{$userId}/profile-edit/'
profileEdit: 'uzivatel/{$userId}/profile-edit/?do=profileForm-submit'
ratings: 'uzivatel/{$entityId}/hodnoceni/strana-{$page}/'
movie:
default: 'film/{$entityId}/'
7 changes: 7 additions & 0 deletions tests/cases/Entities/UserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,11 @@ public function testRequest()
$this->assertInternalType('string', $e->getUrlKey('any property'));
}

/** @covers Csfd\Entities\User::getRatings() */
public function testGetRatings()
{
$ratings = $this->entity->getRatings(1);
$this->assertContainsOnlyInstancesOf('Csfd\Collections\Rating', $ratings);
}

}
43 changes: 41 additions & 2 deletions tests/cases/Parsers/UserParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ class UserParserTest extends TestCase
{

private $parser;
private $builder;
private $html;

public function setUp()
{
$this->parser = new User;
$builder = UrlBuilder::factory(__DIR__ . '/../../../src/Csfd/urls.yml');
$this->builder = UrlBuilder::factory(__DIR__ . '/../../../src/Csfd/urls.yml');

$auth = $this->getAuthenticator();
$account = $this->getConfig()['account'];
$auth->setCredentials($account['username'], $account['password']);

$factory = $this->getRequestFactory();
$url = $builder->get(['entities', 'user', 'profile'], ['entityId' => $account['id']]);
$url = $this->builder->get(['entities', 'user', 'profile'], ['entityId' => $account['id']]);
$this->html = $factory->create($url, NULL, NULL, $auth->getCookie())->getContent();
}

Expand Down Expand Up @@ -118,4 +119,42 @@ public function testGetAvatarUrl()
$this->assertSame($exp, $this->parser->getAvatarUrl($this->html));
}

/** @covers Csfd\Parsers\User::getRatings() */
public function testGetRatings_empty()
{
$exp = [];

$userId = 434388; // csfdapi.cz account
$url = $this->builder->get(['entities', 'user', 'ratings'],
['entityId' => $userId, 'page' => 1]);
$html = $this->getRequestFactory()->create($url)->getContent();

$this->assertSame($exp, $this->parser->getRatings($html));
}

/** @covers Csfd\Parsers\User::getRatings() */
public function testGetRatings()
{
$exp = [
[7684, 2, '2014/02/14'],
[10503, 1, '2014/02/14'],
[310284, 4, '2014/02/14'],
[348147, 3, '2014/02/14'],
[291800, 0, '2014/02/14'],
[305234, 5, '2014/02/14'],
];

$account = $this->getConfig()['account'];
$url = $this->builder->get(['entities', 'user', 'ratings'],
['entityId' => $account['id'], 'page' => 1]);
$html = $this->getRequestFactory()->create($url)->getContent();

$ratings = $this->parser->getRatings($html);
array_walk($ratings, function(&$rating) {
$rating[2] = $rating[2]->format('Y/m/d');
});

$this->assertSame($exp, $ratings);
}

}

0 comments on commit 175e86b

Please sign in to comment.