Skip to content

Commit

Permalink
Merge pull request #1 from alexander-nitsche/task-flexible-time-period
Browse files Browse the repository at this point in the history
Task flexible time period
  • Loading branch information
kdambekalns authored Jan 20, 2023
2 parents 84a429d + 6b1e23c commit 3cc3be2
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions PostNewQuestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,14 @@ class PostNewQuestions
protected string $stackAppsKey = '';

/**
* WebHook URL to channel #stackoverflow
*
* @var string
*/
protected string $slackUrl = '';
protected string $apiTagUrl = 'https://api.stackexchange.com/2.2/questions?site=stackoverflow&filter=withbody&order=asc';

/**
* @var string
*/
protected string $apiTagUrl = 'https://api.stackexchange.com/2.2/questions?site=stackoverflow&filter=withbody&order=asc';
protected string $fileWithTimestampOfLastExecution = 'last_execution.txt';

/**
* @var array
Expand Down Expand Up @@ -127,12 +125,20 @@ public function sendPostToSlack(string $mainTag, array $data): void
*/
public function getNewestPostsInStackOverflow(string $tag): ?array
{
$lastExecution = time() - 24 * 3600;
$lastExecution = (int)file_get_contents($this->fileWithTimestampOfLastExecution) ?: time() - 24 * 3600;
$taggedQuestionsUrl = $this->apiTagUrl . '&tagged=' . $tag . '&key=' . $this->stackAppsKey . '&fromdate=' . $lastExecution;
$questions = file_get_contents('compress.zlib://' . $taggedQuestionsUrl);

return json_decode($questions, true);
}

/**
* @return void
*/
public function setNewTimestamp(): void
{
file_put_contents($this->fileWithTimestampOfLastExecution, time());
}
}

$newPostService = new PostNewQuestions();
Expand All @@ -144,3 +150,7 @@ public function getNewestPostsInStackOverflow(string $tag): ?array
$postData = $newPostService->convertQuestionToSlackData($tag, $newestQuestions);
$newPostService->sendPostToSlack($tag, $postData);
}

if (!empty($postData)) {
$newPostService->setNewTimestamp();
}

0 comments on commit 3cc3be2

Please sign in to comment.