Skip to content

Commit 18edff7

Browse files
committed
Added feature to comment on last tweet; Added redis to store last tweet data for persistency; Improved logging; Code cleanup and restructuring
Signed-off-by: avaakash <as86414@gmail.com>
1 parent 5792ac5 commit 18edff7

File tree

18 files changed

+272
-90
lines changed

18 files changed

+272
-90
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
env
22
env.json
3-
__pycache__
3+
__pycache__*/
44
run_logs.txt

env.json.sample

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"TWITTER_ACCESS_SECRET": "",
66
"TWITTER_USERNAME": "",
77
"TELEGRAM_SECRET": "",
8-
"TELEGRAM_ALLOWED_IDS": "[]"
8+
"TELEGRAM_ALLOWED_IDS": "[]",
9+
"REDIS_HOST": "",
10+
"REDIS_PORT": "",
11+
"REDIS_PASSWORD": ""
912
}

redis_py/__init__.py

Whitespace-only changes.

redis_py/connect.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
Connect to redis server
3+
"""
4+
import redis
5+
6+
from settings import get_env
7+
8+
def connect():
9+
"""Connect to redis server"""
10+
environ_secrets = get_env()
11+
host = environ_secrets["redis_host"]
12+
port = environ_secrets["redis_port"]
13+
password = environ_secrets["redis_password"]
14+
if password:
15+
return redis.Redis(host=host, port=port, password=password)
16+
return redis.Redis(host=host, port=port)

redis_py/database.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
Database actions
3+
"""
4+
5+
from .connect import connect
6+
7+
redis_connect = connect()
8+
9+
def set_tweet(tweet_id, tweet_text):
10+
"""Set a tweet in redis"""
11+
redis_connect.set("tweet_id", tweet_id)
12+
redis_connect.set("tweet_text", tweet_text)
13+
14+
def get_tweet():
15+
"""Get a tweet from redis"""
16+
return (redis_connect.get("tweet_id").decode('UTF-8'),
17+
redis_connect.get("tweet_text").decode('UTF-8'))
18+
19+
def delete_tweet():
20+
"""Delete a tweet from redis"""
21+
redis_connect.delete("tweet_id", "tweet_text")
22+
23+
def set_tweet_list(tweet_id):
24+
"""Set a list of tweets in redis"""
25+
redis_connect.lpush("tweet_list", tweet_id)
26+
27+
def get_latest_tweet_list():
28+
"""Get the latest tweet from redis"""
29+
return redis_connect.lrange("tweet_list", 0, -1)
30+
31+
def remove_last_tweet():
32+
"""Remove the latest tweet from redis"""
33+
redis_connect.lpop("tweet_list")

redis_py/manifests/redis-conf.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: redis-config
5+
data:
6+
redis-config: |
7+
maxmemory 2mb
8+
maxmemory-policy allkeys-lru

redis_py/manifests/redis-pod.yaml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
apiVersion: v1
2+
kind: Pod
3+
metadata:
4+
name: redis
5+
labels:
6+
app: redis
7+
spec:
8+
containers:
9+
- name: redis
10+
image: redis:5.0.4
11+
command:
12+
- redis-server
13+
- "/redis-master/redis.conf"
14+
env:
15+
- name: MASTER
16+
value: "true"
17+
ports:
18+
- containerPort: 6379
19+
resources:
20+
limits:
21+
cpu: "0.1"
22+
volumeMounts:
23+
- mountPath: /redis-master-data
24+
name: data
25+
- mountPath: /redis-master
26+
name: config
27+
volumes:
28+
- name: data
29+
emptyDir: {}
30+
- name: config
31+
configMap:
32+
name: redis-config
33+
items:
34+
- key: redis-config
35+
path: redis.conf

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ backports.zoneinfo==0.2.1
44
cachetools==4.2.2
55
certifi==2021.10.8
66
charset-normalizer==2.0.10
7+
Deprecated==1.2.13
78
idna==3.3
89
isort==5.10.1
910
lazy-object-proxy==1.7.1
1011
mccabe==0.6.1
1112
oauthlib==3.1.1
13+
packaging==21.3
1214
platformdirs==2.4.1
1315
pylint==2.12.2
16+
pyparsing==3.0.6
1417
python-telegram-bot==13.10
1518
pytz==2021.3
1619
pytz-deprecation-shim==0.1.0.post0
20+
redis==4.1.0
1721
requests==2.27.1
1822
requests-oauthlib==1.3.0
1923
six==1.16.0

settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,8 @@ def get_env():
2323
environ_secrets["twitter_username"] = env.get("TWITTER_USERNAME", "")
2424
environ_secrets["telegram_secret"] = env.get("TELEGRAM_SECRET", "")
2525
environ_secrets["telegram_allowed_ids"] = env.get("TELEGRAM_ALLOWED_IDS", "")
26+
environ_secrets["redis_host"] = env.get("REDIS_HOST", "")
27+
environ_secrets["redis_port"] = env.get("REDIS_PORT", "")
28+
environ_secrets["redis_password"] = env.get("REDIS_PASSWORD", "")
2629

2730
return environ_secrets

start.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# !/bin/bash
2-
echo $HOME
32
source "$HOME/tweet-from-message/env/bin/activate"
43
echo Starting Bot...
5-
python main.py > run_logs.txt &
4+
python main.py > $HOME/tweet-from-message/run_logs.txt &
65
sleep 3
76
echo Bot started.

0 commit comments

Comments
 (0)