Skip to content
This repository has been archived by the owner on Mar 17, 2022. It is now read-only.

Commit

Permalink
feature/ops (#36)
Browse files Browse the repository at this point in the history
* fix: move main routine outside of the project modules

* fix: update docker builds

* fix: update requirements

* feat: improve handler readability

* feat: create script for container build automation

* feat: create container build script

* feat: create development script

* fix: update divulgator runner script

* fix: update installation script arguments

* feat: update installation scripts

* feat: update container build with release tag

* fix: removed external port 3306 connection from database
  • Loading branch information
vcwild authored Nov 10, 2021
1 parent 34af7bc commit 7c4ffd1
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,9 @@ streamers.csv

# Docker
.docker
.eggs
*.egg-info
src
test
Dockerfile
docker/dump.sql
11 changes: 4 additions & 7 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,19 @@ FROM python:3.9-slim-bullseye

LABEL app="livedivulgador"

ADD . ./app
WORKDIR /app

ADD . .

RUN apt-get update \
&& apt-get -yy install --no-install-recommends gcc libmariadb-dev \
&& pip3 install -r ./app/requirements.txt \
&& pip3 install -r requirements.txt \
&& apt-get -y remove --purge --auto-remove gcc \
&& apt-get -y autoremove \
&& apt-get -y autoclean \
&& apt-get -y clean \
&& rm -rf /tmp/* \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /var/cache/apt/archives/* \

&& pip cache purge \
&& rm -rf /root/.cache/*

WORKDIR /app

# CMD [ "python3", "./app/src/bot/main.py" ]
12 changes: 5 additions & 7 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ version: '3.1'

services:
app:
image: vcwild/livedivulgator
container_name: livedivulgator_app
build:
context: .
dockerfile: Dockerfile
depends_on:
- db
env_file:
- .env
# restart: always
# command: pytest -k send_tweet
environment:
DATABASE_HOST: livedivulgator_db
entrypoint: python3
command: main.py run

db:
container_name: livedivulgator_db
ports:
- "3306:3306"
build:
context: ./docker/
dockerfile: database.dockerfile
Expand Down
2 changes: 1 addition & 1 deletion entry_points.cfg
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[console_scripts]
divulgator = live_divulgator.main:main
divulgator = main:main
34 changes: 25 additions & 9 deletions live_divulgator/handlers/post_tweet.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class PostTweet:
def handle(cls) -> None:
try:
data = VerifyOnlineStreamers.handle()
logger.info("Tweeting about `data`")
cls.tweet(data)
except Exception as e:
logger.error(e)
Expand Down Expand Up @@ -82,7 +81,7 @@ def refresh_tweeted_cache(cls):
cls.cache["tweeted"].remove(element)

@classmethod
def remove_cached_from_tweet_list(cls, data):
def remove_cached_from_tweet_list(cls, data) -> list[dict]:
cls.refresh_tweeted_cache()

tweeted = cls.cache["tweeted"]
Expand All @@ -96,6 +95,28 @@ def remove_cached_from_tweet_list(cls, data):

return online_streamer_to_tweet

@classmethod
def handle_send_tweets(
cls, not_cached_live_list: list[dict], live_tweet_metadata: list[TweetMetadata]
) -> None:
if not_cached_live_list != []:
logger.info(f"Tweeting about streamers")
deque(map(cls.twitter_client.send_tweet, live_tweet_metadata))

return None

logger.info("No new live streamers to tweet")

@classmethod
def handle_update_cache(cls, not_cached_live_list: list) -> None:
if not_cached_live_list != []:
deque(map(cls.update_tweeted, not_cached_live_list))
logger.info("Updated tweeted streamers")

return None

logger.info("No new live streamers to add to cache")

@classmethod
def tweet(cls, live_list: list[dict]) -> None:
not_cached_live_list = cls.remove_cached_from_tweet_list(live_list)
Expand All @@ -104,11 +125,6 @@ def tweet(cls, live_list: list[dict]) -> None:
map(cls.generate_tweet_metadata, not_cached_live_list)
)

if not_cached_live_list == []:
logger.info("No new live streamers to tweet")

logger.info(f"Tweeting about streamers")
deque(map(cls.twitter_client.send_tweet, live_tweet_metadata))
cls.handle_send_tweets(not_cached_live_list, live_tweet_metadata)

deque(map(cls.update_tweeted, not_cached_live_list))
logger.info("Updated tweeted streamers")
cls.handle_update_cache(not_cached_live_list)
File renamed without changes.
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
tweepy==3.10
httpx
SQLAlchemy
click
httpx
mariadb
python-dotenv
pyyaml
timeloop
tweepy
21 changes: 21 additions & 0 deletions scripts/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# terminal color
color_default="\e[39m"
color_error="\e[31m"
color_ok="\e[32m"
color_header="\e[34m"

# functions
print_header() {
echo
echo -e "--$color_header $1 $color_default"
}

print_check() {
status=$?
if [ $status -eq 0 ]
then
echo -e "[$color_ok OK $color_default] $1"
else
echo -e "[$color_error KO $color_default] $1"
fi
}
21 changes: 21 additions & 0 deletions scripts/container_build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/sh

source $(dirname "$0")/common.sh

# Build the container

container_name="vcwild/livedivulgator:$TAG_RELEASE"

main() {
if [[ -z "${TAG_RELEASE}" ]]; then
TAG_RELEASE="latest"
echo -e "Preparing container with tag:${TAG_RELEASE}"
else
echo -e "Preparing container with tag:${TAG_RELEASE}"
fi

print_header "Building the container, please wait..."
docker build -t $container_name .
}

main
32 changes: 32 additions & 0 deletions scripts/install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/bin/bash

source $(dirname "$0")/common.sh

current_directory="$(pwd)"
virtualenv_directory="$current_directory/venv"

main() {
print_header "Preparing python virtual environment"

python3 -m venv "$virtualenv_directory" >/dev/null 2>&1
print_check "virtualenv initialization $virtualenv_directory"

$virtualenv_directory/bin/pip install --upgrade pip >/dev/null 2>&1
print_check "pip upgrade"

$virtualenv_directory/bin/pip install setuptools_scm >/dev/null 2>&1
print_check "pip install setuptools_scm"

# install local development build
echo -e " ... installing Livedivulgator bot component"
$virtualenv_directory/bin/pip install -e .[dev] >/dev/null 2>&1
print_check "bot installation"

# check installation
echo -e " ... checking CLI"
$virtualenv_directory/bin/divulgator --help >/dev/null 2>&1
print_check "divulgator CLI installation"
}

# main
main

0 comments on commit 7c4ffd1

Please sign in to comment.