PHP Gmail API: Trying to gather only last 100 threads - php

I am trying to get only last 100 threads from Gmail. I am using this function to gather threads but I don't know why It always fetches all threads and not only last 100. Also, I want to fetch them only from the inbox. Maybe there is a small mistake but I can't see what is wrong. Hope someone sees what is wrong.
function listThreads($service, $userId) {
$threads = array();
$pageToken = NULL;
do {
try {
$opt_param = array(
'maxResults' => 100,
'labelIds' => 'INBOX'
);
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
}
$threadsResponse = $service->users_threads->listUsersThreads($userId, $opt_param);
if ($threadsResponse->getThreads()) {
$threads = array_merge($threads, $threadsResponse->getThreads());
$pageToken = $threadsResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
foreach ($threads as $thread) {
print 'Thread with ID: ' . $thread->getId() . '<br/>';
}
return $threads;
}

I found a solution. If you set maxResults=>100 It will gather 100 threads from one token and then go ahead to gather another token to grab next 100. So the only thing I had to do was to break out once threads were fetched. Hope this helps someone.
Working code:
function listThreads($service, $userId) {
$threads = array();
$pageToken = NULL;
$opt_param = array(
'maxResults' => 100,
'labelIds' => 'INBOX'
);
do {
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
}
$threadsResponse = $service->users_threads->listUsersThreads($userId, $opt_param);
if ($threadsResponse->getThreads()) {
$threads = array_merge($threads, $threadsResponse->getThreads());
//$pageToken = $threadsResponse->getNextPageToken();
break;
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
$pageToken = NULL;
$x++;
}
} while ($pageToken);
foreach ($threads as $thread) {
print 'Thread with ID: ' . $thread->getId() . '<br/>';
}
return $threads;
}

Related

php redis insert a data at a same time with few queues error

There are 3 queues in redis.
If I insert a data at a same time with different browsers, both requests are accepted.
I get 2 duplicated records. Any helps?
For example, the first request is on the 1st queue.
At the second request, 2nd queue is searched for finding the data.
But the data doesn't exist in the queue.
The second request is inserted on the queue.
Is it understandable?
public function save($evt_no, $type = "redis") {
$name = '';
$res = true;
try{
$headers = getallheaders();
$bodys = $_REQUEST;
$session = $_SESSION;
foreach ($_FILES as $fileKey=>$fileVal) {
if ($fileVal['error'] == 0) {
try {
$uploaded_row = $this->fileL->upload('queueEventParticipant', $fileKey, array() ,'queue');
} catch (\Exception $ex) {
throw $ex;
}
$bodys['file'][_return_numeric($fileKey)] = $uploaded_row;
}
}
$data = array(
'header' => $headers,
'body' => $bodys,
'session' => $session
);
$data['body']['evt_no'] = $evt_no;
$data = json_encode($data);
if($type == "redis"){
$name = $this->attendQueueRedisService->setNewRsvp($data);
} else {
throw new \Exception("No exist");
}
} catch (\Exception $ex){
log_message("error", $ex->getMessage());
throw $ex;
}
if($res){
return $name;
} else {
return "Error";
}
}
class AttendQueueRedisService extends CI_Model {
private $redisClient;
private $redisConfig;
const PREFIX_DONE = 'done_';
const PREFIX_ERROR = 'error_';
public function __construct() {
parent::__construct();
if ($this->load->config('redis', true, true)) {
$this->redisConfig = $this->config->config['redis'];
}
$this->redisClient = new Redis();
$this->redisClient->connect($this->redisConfig['hostname'], $this->redisConfig['port']);
$this->redisClient->auth($this->redisConfig['auth']);
$this->redisClient->select($this->redisConfig['queue']);
}
public function setNewRsvp($data) {
$key = uniqid('rsvp_'.gethostname().'_',true).':redis';
$this->redisClient->set($key, $data, 60 * 30);
return $key;
}
}

Getting Message Threads, modfied after [date]

The Why?
I'm playing around with the GMAIL api, for a minor project at work
The Wanted Result
What I'm trying to get from the Gmail API, is all message threads modfied after a certain timestamp/date.
What do i have so far
So far i manage to get all threads created after a certain date - by using the standart gmail search queries see - https://support.google.com/mail/answer/7190.
The Problem
It's great i can get all the Message Threads after a certain date, but the problem is when i have my queried set as:
after:[todays date - 7 days)
This will show me all Message Threads, up to 7 days ago - but what when i have a Message Thread, created 8 days ago and still getting updated?. What is my best option to get all message threads, modified from now to 7 days ago?
Checking all message threads, all the time is not an option :)
The code
$client = getClient();
$oService = new Google_Service_Gmail($client);
$sUser = 'me';
$tsMailsAfter = '1554280000';
$sIn = 'INBOX';
//before;, after:, in:(inbox, sent)
$aThreads = getlistThread($oService, $sUser, 'after:'.$tsMailsAfter.' in:'.$sIn);
function getlistThread($service, $userId, $aQuery = '')
{
$threads = array();
$pageToken = NULL;
do
{
try
{
$opt_param = array();
if ($pageToken)
{
$opt_param['pageToken'] = $pageToken;
}
$opt_param['q'] = $aQuery;
$threadsResponse = $service->users_threads->listUsersThreads($userId, $opt_param);
if($threadsResponse->getThreads())
{
$threads = array_merge($threads, $threadsResponse->getThreads());
$pageToken = $threadsResponse->getNextPageToken();
}
}
catch (Exception $e)
{
print 'An error occurred: ' . $e->getMessage();
$pageToken = NULL;
}
}
while ($pageToken);
return $threads;
}
i solved my issue by just using a search query (with :after) on ->listUsersMessages() and then collecting the ID's in an array(). Its not the most optimal solution, having a search query :modified_after would be the best solution - this solution defiantly works for my purpose:)
function listThreadsToUpdate($service, $userId, $sQuery = '')
{
$pageToken = NULL;
$messages = array();
$opt_param = array();
do
{
try
{
if ($pageToken)
{
$opt_param['pageToken'] = $pageToken;
}
$opt_param['q'] = $sQuery;
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
if ($messagesResponse->getMessages())
{
$aMessages = array_merge($messages, $messagesResponse->getMessages());
$pageToken = $messagesResponse->getNextPageToken();
}
}
catch (Exception $e)
{
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
$aThreadsToUpdate = array();
if(isset($aMessages) && !empty($aMessages))
{
foreach ($aMessages as $oMessage)
{
$aThreadsToUpdate[] = $oMessage->getThreadId();
}
}
return array_unique($aThreadsToUpdate);
}

Get my instagram posts with the likes of a particular user mgp25/Instagram-API

I am use mgp25/Instagram-API
How can l get my instagram posts with the likes of a particular user?
My code:
set_time_limit(0);
date_default_timezone_set('UTC');
require __DIR__.'/vendor/autoload.php';
$username = 'myInstagramUsername';
$password = 'myInstagramPassword';
$debug = false;
$truncatedDebug = false;
$ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
try {
$ig->login($username, $password);
} catch (\Exception $e) {
echo 'Something went wrong: '.$e->getMessage()."\n";
exit(0);
}
try {
$userId = $ig->people->getUserIdForName($username);
$act = json_encode($ig->people->getRecentActivityInbox(), true);
???????
} catch (\Exception $e) {
echo 'Something went wrong: '.$e->getMessage()."\n";
}
Worked
set_time_limit(0);
date_default_timezone_set('UTC');
require __DIR__.'/vendor/autoload.php';
$username = 'username';
$password = 'password';
$debug = false;
$truncatedDebug = false;
$ig = new \InstagramAPI\Instagram($debug, $truncatedDebug);
try {
$ig->login($username, $password);
} catch (\Exception $e) {
echo 'Something went wrong: '.$e->getMessage()."\n";
exit(0);
}
try {
$posts = [];
$comments = [];
$userId = $ig->people->getUserIdForName($username);
$maxId = null;
$response = $ig->timeline->getUserFeed($userId, $maxId);
foreach ($response->getItems() as $item) {
foreach($item->getLikers($item->getId()) as $h){
$posts[] = ['id' => $item->getId(), 'username' => $h->username];
}
foreach($ig->media->getComments($item->getId()) as $v){
if(count($v->comments) > 0){
foreach($v->comments as $c){
$comments[] = ['id' => $item->getId(), 'username' => $c->user->username, 'text' => $c->text];
}
}
}
}
print_r($posts);
print_r($comments);
} catch (\Exception $e) {
echo 'Something went wrong: '.$e->getMessage()."\n";
}
Try looping through each item of your profile then get the likes and find the username. Then if the item has a like by that user put it in an item array like so:
// Get the UserPK ID for "natgeo" (National Geographic).
$userId = $ig->people->getUserIdForName('natgeo');
// Starting at "null" means starting at the first page.
$maxId = null;
do {
$response = $ig->timeline->getUserFeed($userId, $maxId);
// In this example we're simply printing the IDs of this page's items.
foreach ($response->getItems() as $item) {
//loop through likes as u can see in [source 1][1] there is some method called 'getLikers()' which u can call on a media object.
foreach($item->getMedia()->getLikers() as $h){
// here do some if with if response user == username
}
}
source 1:https://github.com/mgp25/Instagram-API/blob/master/src/Request/Media.php
source 2:https://github.com/mgp25/Instagram-API/tree/master/examples
source 3:https://github.com/mgp25/Instagram-API/blob/e66186f14b9124cc82fe309c98f5acf2eba6104d/src/Response/MediaLikersResponse.php
By reading the source files this could work i havent tested it yet.
for new version of mgp25 this code work fine
POST UPDATED
$likes = [];
$comments = [];
$userId = $ig->people->getUserIdForName($username);
$maxId = null;
$response = $ig->timeline->getUserFeed($userId, $maxId);
$posts = $response->jsonSerialize();
foreach ($response->getItems() as $item) {
$likers = $ig->media->getLikers($item->getId());
if ($likers != null) {
foreach ($likers->getUsers() as $h) {
$likes[] = ['id' => $item->getId(), 'username' => $h->getUsername()];
}
}
$commentsList = $ig->media->getComments($item->getId());
if ($commentsList != null) {
foreach ($commentsList->getComments() as $c) {
$comments[] = ['id' => $item->getId(), 'username' => $c->getUser()->getUsername(), 'text' => $c->getText()];
}
}
}
updated reference link

Gmail List API returning all messages when maxResults set to 100

I am working on getting the Gmail List API setup, but for some reason, it returns all 5,000 messages in my inbox even though I have set maxResults to 100. Here is my current code:
$pageToken = NULL;
$messages = array();
$opt_param = array();
do {
try {
if ($pageToken) {
$opt_param['pageToken'] = $pageToken;
$opt_param['maxResults'] = 100;
$opt_param['labelIds'] = 'INBOX';
}
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
if ($messagesResponse->getMessages()) {
$messages = array_merge($messages, $messagesResponse->getMessages());
$pageToken = $messagesResponse->getNextPageToken();
}
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
} while ($pageToken);
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
}
return $messages;
I'm not sure where I am going wrong, but I need to be able to filter down my inbox to the last 100 messages sent.
So I just realized, you are getting all messages because even though you are getting them by slices of 100, you are, in the end, doing that in a loop until you get all messages in your inbox...
If you only want the first 100, try this instead:
$messages = array();
try {
$opt_param = array(
'maxResults' => 100,
'labelIds' => 'INBOX'
);
$messagesResponse = $service->users_messages->listUsersMessages($userId, $opt_param);
$messages = $messagesResponse->getMessages();
} catch (Exception $e) {
print 'An error occurred: ' . $e->getMessage();
}
if(!empty($messages)) {
foreach ($messages as $message) {
print 'Message with ID: ' . $message->getId() . '<br/>';
}
}
return $messages;
So I removed the do/while loop, basically...
Hope this helps!

Drive API request sometimes returns an object another time an array - PHP

I'm doing a little page that lists all files that are in google drive. In the first time I do the authentication and save the refresh token + user id and email and then I list all the files. To get the information I do this:
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$list=$files->getItems();
$result = array_merge($result,$list);
$pageToken = $files->getNextPageToken();
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
And it works, but if the user refresh the page, I have do refresh the access token (getting the refresh token from db) and then proceed to do the same as above. In this case that code gives me an error because this time I get an associative array when I do:
$files = $service->files->listFiles($parameters);
To make it work I need to change the code to:
$result = array();
$pageToken = NULL;
do {
try {
$parameters = array();
if ($pageToken) {
$parameters['pageToken'] = $pageToken;
}
$files = $service->files->listFiles($parameters);
$list=$files['items'];
$result = array_merge($result,$list);
$pageToken = $files['nextPageToken'];
} catch (Exception $e) {
echo "An error occurred: " . $e->getMessage();
$pageToken = NULL;
}
} while ($pageToken);
This shouldn't happen but I have no idea what's wrong.
From the relevant php source of the class, I guess I found what makes you trouble.
Look at Google_DriveService.php:
// ~Line 119
public function listFiles($optParams = array()) {
$params = array();
$params = array_merge($params, $optParams);
$data = $this->__call('list', array($params));
if ($this->useObjects()) { // THIS CALL HERE
return new Google_FileList($data);
} else {
return $data;
}
}
It checks if you want to work with objects, or not: $this->useObjects().
This method is defined in the super class, Google_ServiceResource.php:
// ~Line 180
public function useObjects() {
global $apiConfig;
return (isset($apiConfig['use_objects']) && $apiConfig['use_objects']);
}
This tells me, that when you configure your service, you will have to set 'use_object' to true.

Categories