Skip to content

Commit

Permalink
Fixes for sharing and database.
Browse files Browse the repository at this point in the history
  • Loading branch information
folkien committed Apr 14, 2023
1 parent 38fbb00 commit b85bce4
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,4 @@ python3.8/
.env
.vscode/
temp/*
database/*
Empty file added database/README.md
Empty file.
91 changes: 91 additions & 0 deletions helpers/Database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
'''
Dataclass of posts database in directory 'database' with
every post named with date.
'''
from dataclasses import asdict, dataclass, field
from datetime import date
import json
import os

from models.Post import Post


@dataclass
class PostDatabase:
''' Dataclass of posts database in directory 'database' with
every post named with date. '''
path: str = 'database'

def __post_init__(self):
''' Post init. '''
# Check if path exists
if (not os.path.exists(self.path)):
# Create path
os.makedirs(self.path)

def __createdPostName(self, date: date) -> str:
''' Get created post filename. '''
return f'Created{date}.json'

def __postedPostName(self, date: date) -> str:
''' Get posted post filename. '''
return f'Posted{date}.json'

def IsCreated(self, date: date) -> bool:
''' Get post by date. '''
# Created post filename
createdPostPath = os.path.join(self.path, self.__createdPostName(date))
# Posted post filename
postedPostPath = os.path.join(self.path, self.__postedPostName(date))

return os.path.exists(createdPostPath) or os.path.exists(postedPostPath)

def IsPosted(self, date: date) -> bool:
''' Get post by date. '''
# Posted post filename
postedPostPath = os.path.join(self.path, self.__postedPostName(date))
return os.path.exists(postedPostPath)

def GetPost(self, date: date) -> Post:
''' Get post by date. '''
# Get file path
filePath = os.path.join(self.path, self.__createdPostName(date))

# Check if file exists
if (not os.path.exists(filePath)):
# Return None
return None

# Load file
with open(filePath, 'r') as fileObject:
# Load json
data = json.load(fileObject)

# Return post
return Post(**data)

def AddCreated(self, post: Post) -> bool:
''' Save post. '''
# Get file path
filePath = os.path.join(self.path, self.__createdPostName(post.date))

# Save file
with open(filePath, 'w') as fileObject:
json.dump(asdict(post), fileObject,
indent=4, ensure_ascii=False)

# Return success
return True

def AddPosted(self, post: Post) -> bool:
''' Save post. '''
# Get file path
filePath = os.path.join(self.path, self.__postedPostName(post.date))

# Save file
with open(filePath, 'w') as fileObject:
# Dump post as json
json.dump(asdict(post), fileObject, indent=4, ensure_ascii=False)

# Return success
return True
46 changes: 40 additions & 6 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
from dataclasses import asdict
from datetime import date
import json
import logging
import sys
from helpers.Ayrshare import post_to_social_media
from helpers.CommentaryGPT import get_gpt_commentary, get_gpt_test
from helpers.Database import PostDatabase
from helpers.LoggingSetup import loggingSetup
from helpers.ReadingsFetcherDeon import get_bible_reading
from models.Post import Post
Expand All @@ -16,9 +19,8 @@ def SetupLogging():
log_line_template='%(color_on)s %(asctime)s [%(threadName)s] [%(levelname)-8s] %(message)s%(color_off)s')


if __name__ == '__main__':
SetupLogging()

def PostCreate(database: PostDatabase):
''' Create post.'''
# Get daily readings from website
readings = get_bible_reading()
if (readings is None):
Expand All @@ -41,10 +43,42 @@ def SetupLogging():

# Create media post
post = Post(readings=readings, commentary=commentary)
# Database : Save created post
database.AddCreated(post)

return post


def PostUpload(database: PostDatabase, post: Post):
''' Upload post. '''
# Create post text/view
postText = ViewPost.View(post)

# Save media post view as temporary object
with open('temp/post.txt', 'w') as fileObject:
fileObject.write(ViewPost.View(post))
fileObject.write(postText)

result = post_to_social_media(postText)

# Database : Save posted post
if (result):
database.AddPosted(post)


if __name__ == '__main__':
SetupLogging()
database = PostDatabase()

# Check : if post exists then read it
post = database.GetPost(date.today())
# Create post if not exists
if (not database.IsCreated(date.today())):
post = PostCreate(database)

# Check : Post is posted, do nothing.
if (database.IsPosted(date.today())):
logging.info('Post is already posted!')
sys.exit(0)

# Post media post to social media
# TODO
# Upload post.
PostUpload(database, post)
4 changes: 3 additions & 1 deletion models/Post.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@
Dataclass for readings and commentary media post.
'''
from dataclasses import dataclass
from dataclasses import dataclass, field
from models.Commentary import Commentary
from models.Readings import Readings
from datetime import date


@dataclass
class Post:
''' Dataclass for readings and commentary media post.'''
readings: Readings = None
commentary: Commentary = None
date: date = field(init=False, default_factory=date.today)

def __post_init__(self):
''' Checks if all fields are not None '''
Expand Down

0 comments on commit b85bce4

Please sign in to comment.