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

Commit

Permalink
chore: create posts if there is a 'new' one
Browse files Browse the repository at this point in the history
use env var for number of posts
update bashrc after posting to cohost with the new number
todo: add looping with some delay, more env vars and maybe a better way of handling them
this feels very janky

NOTE: Docker is now broken because of files outside of the container. DO NOT USE DOCKER NOW
  • Loading branch information
CrimsonTome committed Oct 24, 2022
1 parent 2995072 commit abfa1f6
Showing 1 changed file with 41 additions and 13 deletions.
54 changes: 41 additions & 13 deletions scripts/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,42 @@
from cohost.models.user import User
from cohost.models.block import AttachmentBlock, MarkdownBlock


# import os.environ for environment variable management, sys for exiting upon exception and time.sleep to not overload cohost or your own site with requests
# import os.environ for environment variable management, sys for exiting upon exception, time.sleep to not overload cohost or your own site with requests and feedparser for fetching feeds
from os import environ
import sys
from time import sleep
import feedparser as fp

try:
feedLength = environ.get("LENGTH")
except KeyError:
print ('Please set the environment variable LENGTH with export LENGTH="NUMBER-OF-POSTS"')
sys.exit(1)

feedLength = int(feedLength)
# print (feedLength)



# fetch and parse the feed
data = fp.parse("https://crimsontome.com/feed/feed.xml") # replace with your feed url

title = data.entries[0].title
url = data.entries[0].link
date = data.entries[0].updated.split("T", 1)[0]

postNum = len(data.entries)

# print (title, url, date, postNum)


# set cookie in with below line bwlow, you will have to run again when you reload your shell. to get around this run the line below that and then reload your shell
# export COOKIE="YOUR-TOKEN-HERE"
# echo 'export COOKIE="YOUR-TOKEN-HERE"' >> ~/.bashrc
# see https://github.com/valknight/Cohost.py#tokens for how to get your token


# try import the cookie, tells the user to set it if it does not exist
try:
try:
cookie = environ.get("COOKIE")
except KeyError:
print ('Please set the environment variable COOKIE with export COOKIE="YOUR-TOKEN-HERE"')
Expand All @@ -28,12 +50,18 @@
#login
user = User.loginWithCookie(cookie)

for project in user.editedProjects:
print(project) # Print all pages you have edit permissions for
# project = user.getProject('username') # will retrieve the page I have edit writes for with handle @vallerie
# blocks = [
# AttachmentBlock('pybug.png'), # References image file pybug.png
# MarkdownBlock('**Hello from Python!**') # Example of markdown / text block
# ]
# newPost = project.post('Title of python post!', blocks, tags=['cohost.py', 'python'])
# print('Check out your post at {}'.format(newPost.url))
# for project in user.editedProjects:
# print(project) # Print all pages you have edit permissions for
project = user.getProject('crimsontome427') # replace with your project name
if postNum > feedLength:
blocks = [
# AttachmentBlock('pybug.png'), # References image file pybug.png
MarkdownBlock("["+title+"]("+url+") Posted on: "+date)
]
post = project.post("Blog post #"+str(feedLength),blocks)
print('Check out your post at {}'.format(post.url))
feedLength+=1
environ["LENGTH"] = str(feedLength)
file1 = open("/home/ctome/.bashrc", "a") # append mode, replace with your bashrc location
file1.write('export LENGTH="'+str(feedLength)+'"')
file1.close()

0 comments on commit abfa1f6

Please sign in to comment.