Skip to content

Commit

Permalink
Add archive form generation (Closes #9)
Browse files Browse the repository at this point in the history
  • Loading branch information
kubk committed Jul 28, 2018
1 parent 9e47f3d commit 9b09700
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 40 deletions.
1 change: 0 additions & 1 deletion .env.example
Expand Up @@ -7,4 +7,3 @@ TEST_DB_NAME=
ARHIVACH_EMAIL=
ARHIVACH_PASSWORD=
SPHINX_DSN="mysql:host=127.0.0.1;port=9306"
GUZZLE_PROXY=
4 changes: 0 additions & 4 deletions .travis.yml
Expand Up @@ -15,9 +15,5 @@ before_script:
script:
- vendor/bin/phpunit --testdox

# Cache Composer dist downloads
directories:
- $HOME/.cache/composer/files

notifications:
email: false
11 changes: 0 additions & 11 deletions src/BoardClient/ArhivachClient.php
Expand Up @@ -31,12 +31,6 @@ class ArhivachClient
*/
private $password;

/**
* @param Client $guzzle
* @param ArhivachThreadParser $threadParser
* @param string $email
* @param string $password
*/
public function __construct(
Client $guzzle,
ArhivachThreadParser $threadParser,
Expand Down Expand Up @@ -89,11 +83,6 @@ public function generateArchiveLink(): string
return 'http://arhivach.cf/ajax/?act=locate_thread&url=';
}

/**
* @param Thread $thread
*
* @return void
*/
public function archive(Thread $thread): void
{
// login using email + password, save cookie
Expand Down
17 changes: 4 additions & 13 deletions src/BoardClient/DvachClient.php
Expand Up @@ -5,9 +5,12 @@
namespace phpClub\BoardClient;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use function GuzzleHttp\Psr7\str;
use phpClub\Entity\File;
use phpClub\Entity\Post;
use phpClub\Entity\Thread;
use Symfony\Component\DomCrawler\Crawler;

class DvachClient
{
Expand Down Expand Up @@ -63,7 +66,7 @@ private function looksLikePhpThread(array $threadArray): bool
*/
private function extractThread(array $phpThread): Thread
{
$threadId = intval($phpThread['num']);
$threadId = (int) $phpThread['num'];

$responseBody = $this->guzzle->get("https://2ch.hk/pr/res/{$threadId}.json")->getBody();
$responseJson = \GuzzleHttp\json_decode($responseBody, $assoc = true);
Expand Down Expand Up @@ -121,16 +124,4 @@ private function extractFile(array $fileArray): File
->setWidth($fileArray['width'])
->setClientName($fileArray['fullname'] ?? $fileArray['name']);
}

/**
* @param Thread $thread
*
* @return string
*/
public function searchInArchive(Thread $thread): string
{
$archiveBaseUrl = 'https://2ch.hk/pr/arch/0.html';

// TODO: implement search
}
}
6 changes: 5 additions & 1 deletion src/Entity/Post.php
Expand Up @@ -22,7 +22,11 @@ class Post
*/
private $text;

/** @Column(type="datetime_immutable") **/
/**
* @var \DateTimeImmutable
*
* @Column(type="datetime_immutable")
*/
private $date;

/** @Column(type="string", nullable=true) **/
Expand Down
4 changes: 2 additions & 2 deletions src/Service/UrlGenerator.php
Expand Up @@ -53,12 +53,12 @@ public function toDvachArchiveThread(): string
return '';
}

public function toDvachIcon()
public function toDvachIcon(): string
{
return '/media/images/2ch.ico';
}

public function toArhivachIcon()
public function toArhivachIcon(): string
{
return '/media/images/arhivach.ico';
}
Expand Down
13 changes: 13 additions & 0 deletions templates/_dvachArchiveForm.php
@@ -0,0 +1,13 @@
<?php
/** @var string $searchString */
?>

<label>
<img src="/media/images/2ch.ico" title="Искать в архиве 2ch.hk (могут отсутствовать новые треды)">
<form action="https://2ch.hk/makaba/makaba.fcgi" method="POST" style="display: none" enctype="multipart/form-data">
<input type="hidden" name="task" value="search_arch">
<input type="hidden" name="board" value="pr">
<input type="hidden" name="find" value="<?= $searchString ?>">
<input type="submit" value="Найти">
</form>
</label>
9 changes: 5 additions & 4 deletions templates/post.phtml
Expand Up @@ -4,10 +4,11 @@
/** @var \phpClub\Entity\File $file */
/** @var \phpClub\Entity\Post $post */
/** @var \phpClub\Service\UrlGenerator $urlGenerator */
/** @var \Slim\Views\PhpRenderer $this */
use phpClub\Entity\File;
?>

<div id="<?= $post->getId() ?>" class="post <?= ($post->isFirstPost()) ? 'op-post' : '' ?> clearfix" data-id="<?= $post->getId() ?>">
<div id="<?= $post->getId() ?>" class="post <?= $post->isFirstPost() ? 'op-post' : '' ?> clearfix" data-id="<?= $post->getId() ?>">
<div class="post-header clearfix">
<span class="title"><?= $post->getTitle() ?></span>
<span class="author"><?= $post->getAuthor() ?></span>
Expand All @@ -24,13 +25,13 @@ use phpClub\Entity\File;

<?php if ($post->isFirstPost() && isset($thread)) : ?>
<span class="archives">
<a href="<?= $urlGenerator->toDvachArchiveThread() ?>"><img src="/media/images/2ch.ico" title="Искать в архиве 2ch.hk"></a>
<?= $this->fetch('_dvachArchiveForm.php', ['searchString' => $post->getTitle()]) ?>
<a href="<?= $urlGenerator->toArhivachThread() ?>https://2ch.hk/pr/res/<?= $post->getThread()->getId() ?>.html"><img src="/media/images/arhivach.ico" title="Искать в архиваче"></a>
<i class="fa fa-chain"></i>
</span>
<?php endif; ?>

<?php if (isset($threads) and $post->isFirstPost()): ?>
<?php if (isset($threads) && $post->isFirstPost()): ?>
<span class="show">
[<a href="<?= $urlGenerator->toThread($thread) ?>">Просмотреть</a>]
</span>
Expand Down Expand Up @@ -69,7 +70,7 @@ use phpClub\Entity\File;
</div>
<?php endif; ?>

<?php if (isset($threads) and $post->isFirstPost()) : ?>
<?php if (isset($threads) && $post->isFirstPost()) : ?>
<span class="open">Нажмите <a href="<?= $urlGenerator->toThread($thread) ?>">Просмотреть</a>, чтобы открыть.</span>
<?php endif; ?>
</div>
6 changes: 2 additions & 4 deletions tests/BoardClient/DvachClientTest.php
Expand Up @@ -15,7 +15,7 @@

class DvachClientTest extends TestCase
{
public function testThreads92and93()
public function testThreads92and93(): void
{
$dvachClient = $this->createDvachApiClient(
new Response(200, ['Content-Type' => 'application/json'], file_get_contents(__DIR__ . '/../Fixtures/dvach_api/catalog.json')),
Expand Down Expand Up @@ -46,11 +46,9 @@ public function testThreads92and93()
$this->assertCount(1, $thirdPost->getFiles());
$this->assertEquals('https://2ch.hk/pr/src/1049651/15035109769900.jpg', $thirdPost->getFiles()->first()->getPath());
$this->assertEquals('https://2ch.hk/pr/thumb/1049651/15035109769900s.jpg', $thirdPost->getFiles()->first()->getThumbPath());

// TODO: add more tests
}

public function testThreadsNotFound()
public function testThreadsNotFound(): void
{
$dvachApiClient = $this->createDvachApiClient(
new Response(200, ['Content-Type' => 'application/json'], file_get_contents(__DIR__ . '/../Fixtures/dvach_api/catalog_without_php_threads.json'))
Expand Down

0 comments on commit 9b09700

Please sign in to comment.