Skip to content
This repository has been archived by the owner on May 12, 2023. It is now read-only.

Commit

Permalink
Updated Readme to include Docker instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
Erika Heidi committed Oct 14, 2021
1 parent 317ef0b commit b278057
Showing 1 changed file with 128 additions and 48 deletions.
176 changes: 128 additions & 48 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,96 @@ Other types of dynamic banners can also be generated. Dynacover uses [erikaheidi
<img src="https://user-images.githubusercontent.com/293241/120888813-b559f700-c5fa-11eb-901f-0dac22afd662.png" alt="dynamic twitter profile header"/>
</p>

The demo is explained in detail in this guide: [How to Dynamically Update Twitter Cover Image to Show Latest Followers Using PHP GD and TwitterOAuth](https://dev.to/erikaheidi/how-to-dynamically-update-twitter-cover-image-to-show-latest-followers-using-php-gd-and-twitteroauth-62n). Please notice that the version that is compatible with this tutorial is [0.1.0](https://github.com/erikaheidi/dynacover/releases/tag/0.1), the initial release. Newer versions use [erikaheidi/gdaisy](https://github.com/erikaheidi/gdaisy) as dependency to work with JSON templates.
This guide shows how Dynacover was built: [How to Dynamically Update Twitter Cover Image to Show Latest Followers Using PHP GD and TwitterOAuth](https://dev.to/erikaheidi/how-to-dynamically-update-twitter-cover-image-to-show-latest-followers-using-php-gd-and-twitteroauth-62n). Please notice that the version that is compatible with this tutorial is [0.1.0](https://github.com/erikaheidi/dynacover/releases/tag/0.1), the initial release. Newer versions use [erikaheidi/gdaisy](https://github.com/erikaheidi/gdaisy) as dependency to work with JSON templates.

## Requirements

- PHP (cli only) 7.4+
- GD / PHP-GD
- Curl
- Composer
You can run Dynacover in two different ways:

You also need to register an application within the [Twitter Developers Portal](https://dev.twitter.com) and obtain **4 tokens**:
- **with Docker**: the simplest way to run Dynacover is by using the public [erikaheidi/dynacover](https://hub.docker.com/repository/docker/erikaheidi/dynacover) Docker image.
- to further customize your cover, you can clone the [dynacover repo](https://hub.docker.com/repository/docker/erikaheidi/dynacover) to customize banner resources (JSON template and header images, both located at `app/Resources`), then build a local copy of the Dynacover Docker image to use your custom changes.
- **with a local PHP environment**: this will require `php-cli` 7.4+, Composer, and a few extensions, such as `php-gd`, `php-mbstring`, `php-curl`, and `php-json`.

### Obtaining Required Tokens

To upload your header images, you'll need to register an application within the [Twitter Developers Portal](https://dev.twitter.com) and obtain **4 tokens**:

- Consumer / App Token
- Consumer / App Secret
- User / Access Token
- User / Access Token Secret

Additionally, you can set up a [Personal GitHub API key](https://github.com/settings/tokens) to fetch your sponsors, in case you are enrolled in the GitHub Sponsors program, and you want to use the included `cover_sponsors.json` template. This is optional.
Additionally, you can set up a [Personal GitHub API key](https://github.com/settings/tokens) to fetch your sponsors, in case you are enrolled in the GitHub Sponsors program. In this case, you should use the included `cover_sponsors.json` template. This is optional.

## Running Dynacover with Docker (Recommended)
Dynacover has a self-contained Docker image that you can use to run Dynacover with a single command. The container receives API credentials via a `.env` file provided at run time.

### Using the default Docker image

First, you'll need to set up your API credentials. [Check this blog post](https://dev.to/erikaheidi/how-to-dynamically-update-twitter-cover-image-to-show-latest-followers-using-php-gd-and-twitteroauth-62n) for detailed instructions on how to obtain these.

Create an environment file using [this template](https://github.com/erikaheidi/dynacover/blob/main/.env.example). In this example, we'll name the file `.dynacover`.

```bash
cd ~
nano .dynacover
```

```ini
# Credentials
TW_CONSUMER_KEY=YOUR_APP_KEY
TW_CONSUMER_SECRET=YOUR_APP_SECRET
TW_USER_TOKEN=USER_ACCESS_TOKEN
TW_USER_TOKEN_SECRET=USER_ACCESS_TOKEN_SECRET
GITHUB_TOKEN=GITHUB_PERSONAL_TOKEN

## Installation
# Default Template
DEFAULT_TEMPLATE=app/Resources/templates/cover_basic.json
```

Save the file once you're done adding your API credentials there. You can also use this file to set up your default template of choice.

To test that your credentials are valid, you can list your latest followers with the following command, which will spin up a temporary container and inject the environment variables contained in `.dynacover`:

```shell
docker container run --env-file .dynacover --rm -v $(pwd) erikaheidi/dynacover php dynacover fetch followers
```
The following command will generate a dynamic cover based on the default template specified in `.dynacover`, and upload the resulting image as your Twitter header.

```bash
docker container run --env-file .dynacover --rm -v $(pwd) erikaheidi/dynacover php dynacover cover update
```

### Building a custom header

To customize your header with your own JSON template file and PNG images, you'll need to build a local Docker image with your changes. You don't need to have PHP installed locally. You need only Docker for that.

Start by cloning this repository to your local development machine:

```bash
git clone https://github.com/erikaheidi/dynacover.git
```

The Dockerfile included within the application will copy the files in the current directory into the image. When you are finished with any changes you made to customize your banner, you can run:

```bash
docker build . -t myuser/dynacover
```

Then, you can run dynacover using your custom image with:

```bash
docker container run --env-file .dynacover --rm -v $(pwd) myuser/dynacover php dynacover cover update
```

## Running Dynacover with a local PHP environment

It is also possible to run Dynacover in a local PHP environment. This will require:

- PHP (cli only) 7.4+
- GD / PHP-GD
- Curl
- Composer

Installation is done in a few steps: clone, install dependencies, set up credentials, and you are ready to go.

Expand All @@ -44,27 +115,39 @@ cd dynacover
```shell
composer install
```
This will install a few dependencies and generate a `credentials.php` file in the root folder of your application.
This will install a few dependencies and generate a `config.php` file in the root folder of your application.

### 3. Set Up Twitter Credentials

Open the generated `credentials.php` file and set up your credentials:
Open the generated `config.php` file and set up your credentials:

```php
#credentials.php
#config.php
<?php

return [
'twitter_consumer_key' => 'APP_CONSUMER_KEY',
'twitter_consumer_secret' => 'APP_CONSUMER_SECRET',
'twitter_user_token' => 'USER_ACCESS_TOKEN',
'twitter_token_secret' => 'USER_ACCESS_TOKEN_SECRET',
//Twitter API Keys
'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY',
'twitter_consumer_secret' => getenv('TW_CONSUMER_SECRET') ?: 'APP_CONSUMER_SECRET',
'twitter_user_token' => getenv('TW_USER_TOKEN') ?: 'USER_ACCESS_TOKEN',
'twitter_token_secret' => getenv('TW_USER_TOKEN_SECRET') ?: 'USER_ACCESS_TOKEN_SECRET',

//GitHub Personal Token (for templates using GH Sponsors)
'github_api_bearer' => getenv('GITHUB_TOKEN') ?: 'GITHUB_API_BEARER_TOKEN',

'github_api_bearer' => 'GITHUB_API_BEARER_TOKEN'
//Default Template
'default_template' => getenv('DEFAULT_TEMPLATE') ?: 'app/Resources/templates/cover_basic.json'
];
```
_The `github_api_bearer` token is optional, only in case you want to use the GitHub API to fetch Sponsors._

Replace the keys accordingly and save the file.
You need to replace the following strings with the corresponding tokens:

- `APP_CONSUMER_KEY`
- `APP_CONSUMER_SECRET`
- `USER_ACCESS_TOKEN`
- `USER_ACCESS_TOKEN_SECRET`
- `GITHUB_API_BEARER_TOKEN`

### 4. Test Twitter Connection

Expand Down Expand Up @@ -94,6 +177,34 @@ Built-in templates are located in the `app/Resources/templates` directory. You c

Covers are generated in the root of the application folder, with the name `latest_header.png`. Check the generated image before uploading it to Twitter to confirm it has your latest followers, and it looks like you expect.

### 6. Upload to Twitter

To upload the latest generated cover to twitter, run:

```shell
php dynacover cover upload
```
To generate **and** update your cover using the template that is configured as default in the app config, run:

```shell
php dynacover cover update
```

### 7. Set Up Crontab (Optional)

For this to be completely dynamic and update frequently, you'll need to include the script to your Crontab or equivalent. First make sure you set up the `default_template` config parameter in the `dynacover` script to your preferred template. By default, this is `app/Resources/templates/cover_basic.json`.

To open the current user's crontab, run:

```shell
crontab -e
```

This will open up a text editor. You should include the full paths to both the `php` executable and the `dynacover` script, like in this example which will update the cover every 5 minutes:

```
*/5 * * * * /usr/bin/php /home/erika/dynacover/dynacover cover update > /dev/null 2>&1
```
### Available Templates

### `cover_basic.json`
Expand Down Expand Up @@ -125,37 +236,6 @@ This template uses the Github image source to obtain sponsors and include them i
<img src="https://user-images.githubusercontent.com/293241/120888781-8c396680-c5fa-11eb-8d1d-f3889fdd06e7.png" alt="cover with github sponsors"/>
</p>



### 6. Upload to Twitter

To upload the latest generated cover to twitter, run:

```shell
php dynacover cover upload
```
To generate **and** update your cover using the template that is configured as default in the app config, run:

```shell
php dynacover cover update
```

### 7. Set Up Crontab (Optional)

For this to be completely dynamic and update frequently, you'll need to include the script to your Crontab or equivalent. First make sure you set up the `default_template` config parameter in the `dynacover` script to your preferred template. By default, this is `app/Resources/templates/cover_basic.json`.

To open the current user's crontab, run:

```shell
crontab -e
```

This will open up a text editor. You should include the full paths to both the `php` executable and the `dynacover` script, like in this example which will update the cover every 5 minutes:

```
*/5 * * * * /usr/bin/php /home/erika/dynacover/dynacover cover update > /dev/null 2>&1
```

## Recent Interactions Banner

The "interactions banner" is generated based on your recent interactions and can be limited to only include mutuals (people that follows you and you follow them back).
Expand Down

0 comments on commit b278057

Please sign in to comment.