Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require 'vendor/autoload.php';
- class TagChecker
- {
- const dev_token = '5356430951.6775add.248985fd3f0a475a939f5851245491a7';
- private $guzzle;
- private $tag;
- public function __construct(string $tag, $token = null)
- {
- $this->tag = $tag;
- $this->guzzle = new \GuzzleHttp\Client(
- ['base_uri' => 'https://api.instagram.com/v1/tags/']
- );
- if ($token) {
- $this->token = $token;
- } else {
- $this->token = self::dev_token;
- }
- }
- public function handle() : string
- {
- $data = $this->getTagInfoFromInstagram();
- $uploaders = $this->getUploadersInfo($data);
- return json_encode($uploaders);
- }
- private function sendRequest(string $method, string $endpoint, array $headers = []) : stdClass
- {
- $request = $this->guzzle->request($method, $endpoint, $headers);
- return json_decode($request->getBody()->getContents());
- }
- private function getTagInfoFromInstagram() : array
- {
- $url = $this->tag . '/media/recent?access_token=' . $this->token;
- return $this->sendRequest('GET', $url)->data;
- }
- private function getUploadersInfo(array $data) : stdClass
- {
- $response = new stdClass();
- $response->inserted = 0;
- foreach ($data as $row) {
- $uploader = new TagModelUser();
- $uploader
- ->setTag($this->tag)
- ->setUserId($row->user->id)
- ->setUsername($row->user->username)
- ->setFullName($row->user->full_name);
- $result = $uploader->save();
- if ($result) {
- $response->inserted += 1;
- }
- }
- $tagModelUser = new TagModelUser();
- $response->uploaders = $tagModelUser->count($this->tag);
- return $response;
- }
- }
- class TagModelUser extends TagModel
- {
- public function getTag() : string
- {
- return $this->tag;
- }
- public function setTag($tag) : self
- {
- $this->tag = $tag;
- return $this;
- }
- public function getUserId() : int
- {
- return $this->userId;
- }
- public function setUserId(int $userId) : self
- {
- $this->userId = $userId;
- return $this;
- }
- public function getUsername() : string
- {
- return $this->username;
- }
- public function setUsername(string $username) : self
- {
- $this->username = $username;
- return $this;
- }
- public function getFullName() : string
- {
- return $this->fullName;
- }
- public function setFullName(string $fullName) : self
- {
- $this->fullName = $fullName;
- return $this;
- }
- public function save() : bool
- {
- if(!$this->getTag()) {
- echo 'Tag cannot be empty!';
- return false;
- }
- if (!$this->getUserId()) {
- echo 'User ID cannot be empty!';
- return false;
- }
- if (!$this->getUsername()) {
- echo 'Username cannot be empty!';
- return false;
- }
- if (!$this->getFullName()) {
- echo 'Full name cannot be empty!';
- return false;
- }
- return parent::save();
- }
- }
- class TagModel
- {
- const db_name = 'instagram_api';
- const db_host = 'localhost';
- const db_username = 'root';
- const db_password = '';
- private $db;
- protected $tag;
- protected $userId;
- protected $username;
- protected $fullName;
- public function __construct()
- {
- $dsn = 'mysql:dbname=' . self::db_name . ';host=' . self::db_host;
- $this->db = new PDO($dsn, self::db_username, self::db_password);
- $this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- $this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
- }
- public function save() : bool
- {
- $tagId = $this->getTagId($this->tag);
- $user = $this->checkIfUserExists($this->userId, $tagId);
- if (!$user) {
- return $this->insertUser($tagId);
- }
- return false;
- }
- public function count($tag) : int
- {
- $tagId = $this->getTagId($tag);
- $sql = "SELECT COUNT(id) FROM users WHERE users.tagId = $tagId";
- return $this->db->query($sql)->fetchColumn();
- }
- public function getTagId(string $tag) : int
- {
- $sql = "SELECT id FROM tags WHERE tags.name = '$tag'";
- $result = $this->db->query($sql)->fetchColumn();
- if ($result) {
- return $result;
- }
- $id = $this->insertTag($tag);
- return $id;
- }
- private function insertTag(string $tag) : int
- {
- $sql = "INSERT INTO tags (`name`) VALUES (?)";
- $query = $this->db->prepare($sql);
- $result = $query->execute([$tag]);
- return $this->db->lastInsertId();
- }
- private function checkIfUserExists(int $userId, int $tagId) : bool
- {
- $sql = "SELECT id FROM users WHERE users.userId = $userId AND users.tagId = $tagId";
- $result = $this->db->query($sql)->fetchColumn();
- return $result ? true : false;
- }
- private function insertUser($tag) : bool
- {
- $sql = "INSERT INTO users (`userId`, `username`, `fullname`, `tagId`) VALUES (?, ?, ?, ?)";
- $query = $this->db->prepare($sql);
- $result = $query->execute([
- $this->userId, $this->username, $this->fullName, $tag
- ]);
- return $result;
- }
- }
- $tagChecker = new TagChecker('TasteTheDR');
- header('Content-Type: application/json;charset=utf8');
- echo $tagChecker->handle();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement