This repository has been archived by the owner on May 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from erikaheidi/friends-circle
New Interactions Banner
- Loading branch information
Showing
9 changed files
with
254 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace App\Command\Generate; | ||
|
||
use App\ImageSource\TwitterInteractionsImageSource; | ||
use App\Service\TwitterServiceProvider; | ||
use App\Storage; | ||
use App\Template; | ||
use GDaisy\Placeholder\ImagePlaceholder; | ||
use GDaisy\PlaceholderInterface; | ||
use Minicli\Command\CommandController; | ||
|
||
class InteractionsController extends CommandController | ||
{ | ||
public function handle(): int | ||
{ | ||
//Start by building a dynamic template | ||
$template = new Template('twitter-interactions', [ | ||
'width' => 675, | ||
'height' => 1200, | ||
]); | ||
|
||
//Set up BG as first element | ||
$template->addPlaceholder('background', new ImagePlaceholder([ | ||
'width' => $template->width, | ||
'height' => $template->height, | ||
'pos_x' => 0, | ||
'pos_y' => 0, | ||
'image' => "app/Resources/images/interactions.png" | ||
])); | ||
|
||
$limit = 30; | ||
$per_line = 5; | ||
$prefix = "int"; | ||
$avatar_size = 100; | ||
$spacing = 25; | ||
$line = 1; | ||
$col = 1; | ||
|
||
/** @var TwitterServiceProvider $twitter */ | ||
$twitter = $this->getApp()->twitter; | ||
|
||
//get own user credentials | ||
$owner = $twitter->client->get('/account/verify_credentials'); | ||
|
||
//place owner avatar at the center | ||
$avatar_path = str_replace('normal', 'bigger', $owner->profile_image_url_https); | ||
$avatar = Storage::downloadImage($avatar_path); | ||
$template->addPlaceholder('owner', new ImagePlaceholder([ | ||
'width' => $avatar_size*1.5, | ||
'height' => $avatar_size*1.5, | ||
'pos_x' => ($template->width/2) - ($avatar_size*1.5/2), | ||
'pos_y' => ($template->height/2) - ($avatar_size*1.5/2), | ||
'image' => $avatar, | ||
'filters' => [ "GDaisy\\Filter\\Circle" ] | ||
])); | ||
|
||
$source = new TwitterInteractionsImageSource(); | ||
$featured = $source->getImageList($this->getApp(), $limit, ['mutuals' => $this->hasFlag('mutuals')]); | ||
|
||
//build the template | ||
$linegap = ceil((count($featured) / 2) / $per_line); | ||
$start_x = 20; | ||
$start_y = 20; | ||
|
||
for ($i = 1; $i <= $limit; $i++) { | ||
$pos_x = $start_x + ($spacing*$col) + ($avatar_size*$col - $avatar_size); | ||
$pos_y = $start_y + ($spacing*$line) + ($avatar_size*$line - $avatar_size); | ||
$template->addPlaceholder($prefix . $i, new ImagePlaceholder([ | ||
'width' => $avatar_size, | ||
'height' =>$avatar_size, | ||
'pos_x' => $pos_x, | ||
'pos_y' => $pos_y, | ||
'filters' => [ "GDaisy\\Filter\\Circle" ] | ||
])); | ||
|
||
$col++; | ||
if ($i % $per_line == 0) { | ||
if ($line == $linegap) { | ||
//second block starts at line 7 | ||
$line = 6; | ||
} | ||
$line++; | ||
$col = 1; | ||
} | ||
} | ||
|
||
//Apply template elements | ||
/** | ||
* @var string $key | ||
* @var PlaceholderInterface $placeholder | ||
*/ | ||
foreach ($template->placeholders as $key => $placeholder) { | ||
if ($placeholder instanceof ImagePlaceholder and $placeholder->image) { | ||
$placeholder->apply($template->getResource(), ['image_file' => $placeholder->image]); | ||
continue; | ||
} | ||
|
||
if (isset($featured[$key])) { | ||
$placeholder->apply($template->getResource(), $featured[$key]); | ||
} | ||
} | ||
|
||
$save_path = Storage::root() . 'latest_header.png'; | ||
$template->write($save_path); | ||
$this->getPrinter()->info("Finished generating cover at $save_path."); | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace App\ImageSource; | ||
|
||
use App\ImageSource; | ||
use App\Service\TwitterServiceProvider; | ||
use App\Storage; | ||
use Minicli\App; | ||
|
||
class TwitterInteractionsImageSource implements ImageSource | ||
{ | ||
static string $prefix = "int"; | ||
|
||
public function getImageList(App $app, int $limit = 5, array $params = []): array | ||
{ | ||
/** @var TwitterServiceProvider $twitter */ | ||
$twitter = $app->twitter; | ||
$mutualsOnly = $params['mutuals'] ?? false; | ||
|
||
return $this->getFeaturedByMentions($twitter, $limit, self::$prefix, $mutualsOnly); | ||
} | ||
|
||
public function getFeaturedByMentions(TwitterServiceProvider $twitter, $limit, $prefix = "int", $mutualsOnly = false): array | ||
{ | ||
$response = $twitter->client->get('/statuses/mentions_timeline', [ | ||
'count' => 200, | ||
'include_entities' => false | ||
]); | ||
|
||
$interactions = []; | ||
$users = []; | ||
|
||
//sort interactions by user id + count | ||
foreach ($response as $tweet) { | ||
$users[$tweet->user->id] = $tweet->user; | ||
$int_count = $interactions[$tweet->user->id] ?? 0; | ||
$interactions[$tweet->user->id] = $int_count+1; | ||
} | ||
arsort($interactions); | ||
|
||
if ($mutualsOnly) { | ||
$users_ids = array_keys($users); | ||
$query_chunks = array_chunk($users_ids, 100); | ||
$validation_queries = []; | ||
|
||
foreach ($query_chunks as $chunk) { | ||
$users_ids_list = implode(',', $chunk); | ||
$validation_queries[] = $this->getRelationships($twitter, $users_ids_list); | ||
} | ||
|
||
$validated = []; | ||
foreach ($validation_queries as $relationships) { | ||
foreach ($relationships as $relationship) { | ||
if (isset($relationship->connections) && (count($relationship->connections) > 1)) { | ||
$validated[$relationship->id_str] = $users[$relationship->id_str]; | ||
} | ||
} | ||
} | ||
|
||
$users = $validated; | ||
} | ||
|
||
$featured = []; | ||
$count = 1; | ||
|
||
//build an array with user info | ||
foreach ($interactions as $user_id => $interaction) { | ||
$user = $users[$user_id] ?? null; | ||
if ($user) { | ||
if (!$user->profile_image_url_https) { | ||
continue; | ||
} | ||
|
||
$avatar_path = str_replace('normal', 'bigger', $user->profile_image_url_https); | ||
$avatar = Storage::downloadImage($avatar_path); | ||
|
||
if ($avatar) { | ||
$featured[$prefix . "$count"] = [ | ||
'screen_name' => $user->screen_name, | ||
'avatar' => $avatar, | ||
'image_file' => $avatar | ||
]; | ||
|
||
$count++; | ||
} | ||
} | ||
|
||
if ($count > $limit) { | ||
break; | ||
} | ||
} | ||
|
||
return $featured; | ||
} | ||
|
||
public function getRelationships(TwitterServiceProvider $twitter, string $lookup) | ||
{ | ||
return $twitter->client->get("friendships/lookup", [ | ||
"user_id" => $lookup | ||
]); | ||
} | ||
|
||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.