Skip to content

Commit

Permalink
changing to "cursor"
Browse files Browse the repository at this point in the history
  • Loading branch information
pablouser1 committed Aug 20, 2024
1 parent daab41e commit 673145e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 24 deletions.
3 changes: 1 addition & 2 deletions src/Items/Video.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ public function feed(): self {
$this->feed = Feed::fromCache((object) [
"items" => [$this->item],
"hasMore" => false,
"minCursor" => 0,
"maxCursor" => ""
"cursor" => 0
]);
}
}
Expand Down
39 changes: 17 additions & 22 deletions src/Models/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,46 +6,42 @@ class Feed extends Base {
public Meta $meta;
public array $items = [];
public bool $hasMore = false;
public ?int $minCursor = 0;
public string $maxCursor = '0';
public int $cursor = 0;

/**
* Build feed from TikTok response
* @param \TikScraper\Models\Response $req TikTok response
* @param mixed $minCursor Cursor
* @param string $ttwid ttwid token used for trending
* @param mixed $cursor Cursor
* @return \TikScraper\Models\Feed
*/
public static function fromReq(Response $req, ?int $minCursor = 0, string $ttwid = ''): self {
public static function fromReq(Response $req, int $cursor = 0): self {
$feed = new Feed;
$feed->setMeta($req);
if ($feed->meta->success) {
$data = $req->jsonBody;

// Cursor
$maxCursor = null;
if ($ttwid) {
$maxCursor = $ttwid;
} else {
if (isset($data->cursor)) {
$maxCursor = $data->cursor;
}
}

// Items
// Videos
if (isset($data->itemList)) {
$feed->setItems($data->itemList);
}

// Comments
if (isset($data->comments)) {
$feed->setItems($data->comments);
}

// Nav
$hasMore = false;
if (isset($data->hasMore)) {
$hasMore = $data->hasMore;
}

if ($maxCursor) {
$feed->setNav($hasMore, $minCursor, $maxCursor);
$cursor = 0;
if (isset($data->cursor)) {
$cursor = $data->cursor;
}

$feed->setNav($hasMore, $cursor);
}

return $feed;
Expand All @@ -55,18 +51,17 @@ public static function fromCache(object $cache): self {
$feed = new Feed;
$feed->setMeta(Responses::ok());
$feed->setItems($cache->items);
$feed->setNav($cache->hasMore, $cache->minCursor, $cache->maxCursor);
$feed->setNav($cache->hasMore, $cache->cursor);
return $feed;
}

private function setMeta(Response $req) {
$this->meta = new Meta($req);
}

private function setNav(bool $hasMore, ?int $minCursor, string $maxCursor) {
private function setNav(bool $hasMore, int $cursor) {
$this->hasMore = $hasMore;
$this->minCursor = $minCursor;
$this->maxCursor = $maxCursor;
$this->cursor = $cursor;
}

private function setItems(array $items) {
Expand Down

0 comments on commit 673145e

Please sign in to comment.