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

Commit

Permalink
Merge pull request #29 from valknight/feature/asks
Browse files Browse the repository at this point in the history
Add the ability to ask asks
  • Loading branch information
valknight authored Jul 5, 2023
2 parents 4947677 + 554332c commit f0ae0be
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
10 changes: 10 additions & 0 deletions cohost/models/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,16 @@ def getPosts(self, page=0) -> list:
posts.append(Post(post, self))
return posts

def ask(self, content, sourceProject, anon=False):
from cohost.models.project import EditableProject
if type(sourceProject) != EditableProject:
raise TypeError("sourceProject must be an editable project")
sourceProject = sourceProject # EditableProject
fetchTrpc('asks.send', sourceProject.user.cookie, {
"toProjectHandle": self.handle,
"content": content,
"anon": anon}, methodType='postjson')


class EditableProject(Project):
def __init__(self, user, projectId):
Expand Down
12 changes: 7 additions & 5 deletions cohost/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,15 @@ def fetchTrpc(methods: list, cookie: str,
m = methods
else:
m = ','.join(methods)
cacheData = get_cache_data(cookie, m)
if cacheData is not None:
logger.debug('Cache hit!')
return cacheData
if methodType == "get":
cacheData = get_cache_data(cookie, m)
if cacheData is not None:
logger.debug('Cache hit!')
return cacheData
data = fetch(methodType, "/trpc/{}".format(m), data=data,
cookies=generate_login_cookies(cookie))
set_cache_data(cookie, m, data)
if methodType == "get":
set_cache_data(cookie, m, data)
return data


Expand Down
Binary file added demos/releaseNotes/0.3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
61 changes: 61 additions & 0 deletions demos/releaseNotes/0.3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import os
from cohost.models.user import User
from cohost.models.block import AttachmentBlock, MarkdownBlock

def generateWarningNote(message: str, title = "Heads up! :eggbug-pleading:"):
# stolen from https://cohost.org/aristurtle/post/176671-admonitions-on-cohos
return """<div style="background-color: rgb(255, 255, 255); border: 0.05rem solid #DBAF24; border-radius: 0.2rem; box-shadow: rgba(0, 0, 0, 0.05) 0px 4.4px 11px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1.1px 0px; color: rgba(0, 0, 0, 0.87); margin: 1.5em 0; padding: 0 0.6rem; line-height: 1.6;">
<p style="background-color: rgba(219, 175, 36, 0.1); padding-left: 2rem; padding-right: 0.6rem; border: none; font-weight: 700; margin: 0 -0.6rem; padding-bottom: 0.4rem; padding-top: 0.4rem; position: relative; box-sizing: border-box; line-height: 1.6;">
<span style="position: absolute; left: 0.6rem">⚠️</span>&nbsp;{}
</p>
<p style="margin-bottom: 0.6rem; box-sizing: border-box; color: rgba(0, 0, 0, 0.87); line-height: 1.6">
{}
</p>
</div>""".format(title, message)

def generateDangerNote(message: str, title = "Danger! :eggbug-shocked:"):
# stolen from https://cohost.org/aristurtle/post/176671-admonitions-on-cohos
return """<div style="background-color: rgb(255, 255, 255); border: 0.05rem solid #A00D24; border-radius: 0.2rem; box-shadow: rgba(0, 0, 0, 0.05) 0px 4.4px 11px 0px, rgba(0, 0, 0, 0.1) 0px 0px 1.1px 0px; color: rgba(0, 0, 0, 0.87); margin: 1.5em 0; padding: 0 0.6rem; line-height: 1.6;">
<p style="background-color: rgba(210, 28, 57, 0.1); padding-left: 2rem; padding-right: 0.6rem; border: none; font-weight: 700; margin: 0 -0.6rem; padding-bottom: 0.4rem; padding-top: 0.4rem; position: relative; box-sizing: border-box; line-height: 1.6;">
<span style="position: absolute; left: 0.6rem">🔺</span>&nbsp;{}
</p>
<p style="margin-bottom: 0.6rem; box-sizing: border-box; color: rgba(0, 0, 0, 0.87); line-height: 1.6">
{}
</p>
</div>""".format(title, message)

def main():
username = os.environ.get('cohostUser')
password = os.environ.get('cohostPass')
handle = os.environ.get('cohostHandle')
if username is None:
username = input('username: ')
if password is None:
password = input('password: ')
if handle is None:
handle = input('handle: ')

blocks = [
AttachmentBlock("0.3.png", alt_text="Two python-eggbugs photoshopped ontop of the two sickos from the sickos meme.\
Labels say \"Sicko to sicko communication\" and \"Sicko to sicko conversation\""),
MarkdownBlock('<div style="position: relative; top: 3px; display: inline-block; animation: 1s ease-in-out 0s infinite normal none running bounce;">cohost.py 0.3 is out!!</div>'),
MarkdownBlock('this update contains the ability to send other pages asks via the API. reading your asks is not yet supported, but, is coming soon(TM)'),
MarkdownBlock('as always you can install cohost.py with `pip install cohost` - enjoy chosting!'),
MarkdownBlock(generateWarningNote("Cohost.py is still experimental, etc etc etc... you get the picture. Please submit bugs, feature requests and PRs to the <a href=\"https://github.com/valknight/cohost.py\">GitHub repo</a>!")),
MarkdownBlock(generateDangerNote('If you\'re making a bot, please honor <a href="https://cohost.org/jkap/post/201002-small-request-that-i">@jkap\'s little notes on bots</a>! And, as always, don\'t make this site awful :)')),
MarkdownBlock('<hr>'),
MarkdownBlock('check out <a href="https://github.com/valknight/Cohost.py/blob/main/demos/releaseNotes/0.2.6.py">the source for this post</a>'),
MarkdownBlock("<hr>"),
MarkdownBlock('<small><i>ps: feature requests, bug reports and PRs? super welcome. <a href="https://github.com/valknight/Cohost.py">check out our github.</a> pybug is waiting.</small>')

]
# woah !!! logging in !!! that's so cool !!!
user = User.login(username, password)
project = user.getProject(handle)
otherProject = user.getProject("vallerie")
otherProject.ask("woah hello from cohost.py", project, True)
newPost = project.post('cohost.py 0.3 - the ask-y update', blocks, tags=['cohost.py', 'python', 'development', 'cohost api'], draft=False)
print('Check out your post at {}'.format(newPost.url))

if __name__ == '__main__':
main()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "cohost"
version = "0.2.6"
version = "0.3.0"
description = "Unofficial Python API wrapper for Cohost.org - the fourth website!"
readme = "README.md"
requires-python = ">=3.9"
Expand Down

0 comments on commit f0ae0be

Please sign in to comment.