-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrun.php
48 lines (39 loc) · 1.59 KB
/
run.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
require __DIR__ . '/../vendor/autoload.php';
use Dotenv\Dotenv;
use Noweh\TwitterApi\Client as TwitterClient;
use Noweh\MarvelMemories\MarvelService;
use Noweh\MarvelMemories\MarvelClient;
use Noweh\MarvelMemories\DBAdapter;
// Only allowed for cli
if (PHP_SAPI !== 'cli') {
die('Not allowed');
}
$start = microtime(true);
// Load .env data
$dotenv = Dotenv::createUnsafeImmutable(__DIR__.'/config', '.env');
$dotenv->safeLoad();
try {
// Retrieve Twitter and Marvel env data and load Clients
$marvelSettings = [];
$twitterSettings = [];
foreach (getenv() as $settingKey => $settingValue) {
if (str_starts_with($settingKey, 'MARVEL_')) {
$marvelSettings[str_replace('marvel_', '', mb_strtolower($settingKey))] = $settingValue;
}
if (str_starts_with($settingKey, 'TWITTER_')) {
$twitterSettings[str_replace('twitter_', '', mb_strtolower($settingKey))] = $settingValue;
}
}
$dbAdapter = new DBAdapter(__DIR__ . '//database//db.sqlite');
$marvelClient = new MarvelClient($marvelSettings['private_key'], $marvelSettings['public_key']);
// Tweet a random comic details
$return = (new TwitterClient($twitterSettings))->tweet()->create()->performRequest([
'text' => (new MarvelService($dbAdapter, $marvelClient))
->findRandomComicFormattedForTweet()
]);
echo "script completed without error\r\n";
} catch (Exception | \GuzzleHttp\Exception\GuzzleException $e) {
echo "error in script: " . $e->getMessage() . "\r\n";
}
echo 'execution time ' . round(microtime(true) - $start, 2) . ' seconds';