Browse Source

revise 4chan api support

pull/40/head
czaks 10 years ago
parent
commit
d6b8447233
  1. 32
      inc/api.php
  2. 7
      inc/functions.php

32
inc/api.php

@ -34,8 +34,16 @@ class Api {
'filename' => 'filename', 'filename' => 'filename',
'omitted' => 'omitted_posts', 'omitted' => 'omitted_posts',
'omitted_images' => 'omitted_images', 'omitted_images' => 'omitted_images',
'replies' => 'replies',
'images' => 'images',
'sticky' => 'sticky', 'sticky' => 'sticky',
'locked' => 'locked', 'locked' => 'locked',
'bump' => 'last_modified',
);
$this->threadsPageFields = array(
'id' => 'no',
'bump' => 'last_modified'
); );
if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){ if (isset($config['api']['extra_fields']) && gettype($config['api']['extra_fields']) == 'array'){
@ -56,11 +64,13 @@ class Api {
'omitted_images' => 1, 'omitted_images' => 1,
'sticky' => 1, 'sticky' => 1,
'locked' => 1, 'locked' => 1,
'last_modified' => 1
); );
private function translatePost($post) { private function translatePost($post, $threadsPage = false) {
$apiPost = array(); $apiPost = array();
foreach ($this->postFields as $local => $translated) { $fields = $threadsPage ? $this->threadsPageFields : $this->postFields;
foreach ($fields as $local => $translated) {
if (!isset($post->$local)) if (!isset($post->$local))
continue; continue;
@ -72,6 +82,8 @@ class Api {
} }
if ($threadsPage) return $apiPost;
if (isset($post->filename)) { if (isset($post->filename)) {
$dotPos = strrpos($post->filename, '.'); $dotPos = strrpos($post->filename, '.');
$apiPost['filename'] = substr($post->filename, 0, $dotPos); $apiPost['filename'] = substr($post->filename, 0, $dotPos);
@ -93,14 +105,14 @@ class Api {
return $apiPost; return $apiPost;
} }
function translateThread(Thread $thread) { function translateThread(Thread $thread, $threadsPage = false) {
$apiPosts = array(); $apiPosts = array();
$op = $this->translatePost($thread); $op = $this->translatePost($thread, $threadsPage);
$op['resto'] = 0; if (!$threadsPage) $op['resto'] = 0;
$apiPosts['posts'][] = $op; $apiPosts['posts'][] = $op;
foreach ($thread->posts as $p) { foreach ($thread->posts as $p) {
$apiPosts['posts'][] = $this->translatePost($p); $apiPosts['posts'][] = $this->translatePost($p, $threadsPage);
} }
return $apiPosts; return $apiPosts;
@ -114,19 +126,19 @@ class Api {
return $apiPage; return $apiPage;
} }
function translateCatalogPage(array $threads) { function translateCatalogPage(array $threads, $threadsPage = false) {
$apiPage = array(); $apiPage = array();
foreach ($threads as $thread) { foreach ($threads as $thread) {
$ts = $this->translateThread($thread); $ts = $this->translateThread($thread, $threadsPage);
$apiPage['threads'][] = current($ts['posts']); $apiPage['threads'][] = current($ts['posts']);
} }
return $apiPage; return $apiPage;
} }
function translateCatalog($catalog) { function translateCatalog($catalog, $threadsPage = false) {
$apiCatalog = array(); $apiCatalog = array();
foreach ($catalog as $page => $threads) { foreach ($catalog as $page => $threads) {
$apiPage = $this->translateCatalogPage($threads); $apiPage = $this->translateCatalogPage($threads, $threadsPage);
$apiPage['page'] = $page; $apiPage['page'] = $page;
$apiCatalog[] = $apiPage; $apiCatalog[] = $apiPage;
} }

7
inc/functions.php

@ -1132,6 +1132,9 @@ function index($page, $mod=false) {
$thread->add(new Post($po, $mod ? '?/' : $config['root'], $mod)); $thread->add(new Post($po, $mod ? '?/' : $config['root'], $mod));
} }
$thread->images = $num_images;
$thread->replies = isset($omitted['post_count']) ? $omitted['post_count'] : count($replies);
if ($omitted) { if ($omitted) {
$thread->omitted = $omitted['post_count'] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']); $thread->omitted = $omitted['post_count'] - ($th['sticky'] ? $config['threads_preview_sticky'] : $config['threads_preview']);
$thread->omitted_images = $omitted['image_count'] - $num_images; $thread->omitted_images = $omitted['image_count'] - $num_images;
@ -1416,6 +1419,10 @@ function buildIndex() {
$json = json_encode($api->translateCatalog($catalog)); $json = json_encode($api->translateCatalog($catalog));
$jsonFilename = $board['dir'] . 'catalog.json'; $jsonFilename = $board['dir'] . 'catalog.json';
file_write($jsonFilename, $json); file_write($jsonFilename, $json);
$json = json_encode($api->translateCatalog($catalog, true));
$jsonFilename = $board['dir'] . 'threads.json';
file_write($jsonFilename, $json);
} }
if ($config['try_smarter']) if ($config['try_smarter'])

Loading…
Cancel
Save