Skip to content
This repository has been archived by the owner on Jul 4, 2020. It is now read-only.

Commit

Permalink
v1.2.6
Browse files Browse the repository at this point in the history
  • Loading branch information
aik27 committed Apr 14, 2018
1 parent 6a70c24 commit bba745e
Show file tree
Hide file tree
Showing 24 changed files with 1,152 additions and 785 deletions.
6 changes: 4 additions & 2 deletions classes/InWidget/API/apiModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,16 +102,18 @@ public static function prepareTag($tag)
* Get API driver
*
* @param string $drive [optional]
* @param string $login [optional]
* @param string $password [optional]
* @return object
*/
public static function getInstance($drive = '')
public static function getInstance($drive = '', $login = '', $password = '')
{
switch ($drive) {
case 'official':
return new apiOfficial();
break;
default:
return new apiScraper();
return new apiScraper($login, $password);
break;
}
}
Expand Down
9 changes: 7 additions & 2 deletions classes/InWidget/API/apiScraper.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@

class apiScraper extends apiModel
{
private $api = '';
public $api = '';

public function __construct()
public function __construct($login = '', $password = '')
{
$this->api = new \InstagramScraper\Instagram();
if(!empty($login) AND !empty($password)) {
$api = $this->api->withCredentials($login, $password);
$api->login();
$this->api = $api;
}
}

/**
Expand Down
37 changes: 31 additions & 6 deletions classes/InWidget/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,16 @@ public function __construct($config = [])
$this->setLang();
$this->setSkin();
$this->setOptions();
if(!empty($this->config['ACCESS_TOKEN'])) {
$this->api = apiModel::getInstance('official');
try {
if(!empty($this->config['ACCESS_TOKEN'])) {
$this->api = apiModel::getInstance('official');
}
else {
$this->api = apiModel::getInstance('', $config['authLogin'], $config['authPassword']);
}
}
else {
$this->api = apiModel::getInstance();
catch (\Exception $e) {
throw new inWidgetException($e->getMessage(), 500, $this->getCacheFilePath());
}
}
/**
Expand Down Expand Up @@ -149,7 +154,12 @@ public function getData()
$this->data = json_decode(file_get_contents($this->getCacheFilePath()));
}
if(!is_object($this->data)) {
throw new \Exception('<b style="color:red;">Cache file contains plain text:</b><br />'.$this->data);
$this->data = $this->getBackup();
if(!is_object($this->data)) {
$this->data = $this->getCache();
throw new \Exception('<b style="color:red;">Cache file contains plain text:</b><br />'.$this->data);
}
else $this->data->isBackup = true;
}
return $this->data;
}
Expand All @@ -163,13 +173,14 @@ private function prepareData()
$data['banned'] = $this->banned;
$data['tags'] = $this->config['HASHTAG'];
$data['images'] = $this->medias;
$data['lastupdate'] = time();
return $data;
}
/**
* @return mixed
* @throws inWidgetException
*/
private function getCache()
private function getCache()
{
if($this->config['cacheSkip'] === true) {
return false;
Expand All @@ -188,13 +199,26 @@ private function getCache()
}
return $cacheData;
}
/**
* @return mixed
*/
private function getBackup() {
$file = $this->getCacheFilePath().'_backup';
if(file_exists($file)) {
$rawData = file_get_contents($file);
$cacheData = json_decode($rawData);
if(!is_object($cacheData)) return $rawData;
else return $cacheData;
}
}
/**
* @return null
*/
private function createCache()
{
$data = json_encode($this->prepareData());
file_put_contents($this->getCacheFilePath(), $data, LOCK_EX);
file_put_contents($this->getCacheFilePath().'_backup', $data, LOCK_EX);
}
/**
* @return string
Expand Down Expand Up @@ -264,6 +288,7 @@ private function checkCacheRights()
throw new inWidgetException('Can\'t get access to file <b>{$cacheFile}</b>. Check file path or permissions.', 101, $this->getCacheFilePath());
}
fclose($cacheFile);

}
/**
* Set widget lang
Expand Down
3 changes: 1 addition & 2 deletions classes/InstagramScraper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

require_once __DIR__ . '/InstagramScraper/Instagram.php';
require_once __DIR__ . '/InstagramScraper/Endpoints.php';
require_once __DIR__ . '/InstagramScraper/InstagramQueryId.php';
Expand All @@ -14,4 +13,4 @@
require_once __DIR__ . '/InstagramScraper/Model/Tag.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramException.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramAuthException.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramNotFoundException.php';
require_once __DIR__ . '/InstagramScraper/Exception/InstagramNotFoundException.php';
19 changes: 15 additions & 4 deletions classes/InstagramScraper/Endpoints.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Endpoints
const LOGIN_URL = 'https://www.instagram.com/accounts/login/ajax/';
const ACCOUNT_PAGE = 'https://www.instagram.com/{username}';
const MEDIA_LINK = 'https://www.instagram.com/p/{code}';
const ACCOUNT_MEDIAS = 'https://www.instagram.com/{username}/?__a=1&max_id={max_id}';
const ACCOUNT_MEDIAS = 'https://instagram.com/graphql/query/?query_id=17888483320059182&id={user_id}&first={count}&after={max_id}';
const ACCOUNT_JSON_INFO = 'https://www.instagram.com/{username}/?__a=1';
const MEDIA_JSON_INFO = 'https://www.instagram.com/p/{code}/?__a=1';
const MEDIA_JSON_BY_LOCATION_ID = 'https://www.instagram.com/explore/locations/{{facebookLocationId}}/?__a=1&max_id={{maxId}}';
Expand All @@ -34,6 +34,16 @@ class Endpoints

const GRAPH_QL_QUERY_URL = 'https://www.instagram.com/graphql/query/?query_id={{queryId}}';

private static $requestMediaCount = 30;

/**
* @param int $count
*/
public static function setAccountMediasRequestCount($count)
{
static::$requestMediaCount = $count;
}

public static function getAccountPageLink($username)
{
return str_replace('{username}', urlencode($username), static::ACCOUNT_PAGE);
Expand All @@ -49,10 +59,11 @@ public static function getAccountJsonInfoLinkByAccountId($id)
return str_replace('{userId}', urlencode($id), static::ACCOUNT_JSON_INFO_BY_ID);
}

public static function getAccountMediasJsonLink($username, $maxId = '')
public static function getAccountMediasJsonLink($userId, $maxId = '')
{
$url = str_replace('{username}', urlencode($username), static::ACCOUNT_MEDIAS);
return str_replace('{max_id}', urlencode($maxId), $url);
$url = str_replace('{user_id}', urlencode($userId), static::ACCOUNT_MEDIAS);
$url = str_replace('{count}', static::$requestMediaCount, $url);
return str_replace('{max_id}', urlencode($maxId), $url);
}

public static function getMediaPageLink($code)
Expand Down
Loading

0 comments on commit bba745e

Please sign in to comment.