Skip to content

Instantly share code, notes, and snippets.

@cmho
Forked from knuxify/post.py
Last active May 4, 2024 02:50
Show Gist options
  • Save cmho/3967ed371a1bd396138d3af3ea2bb2f4 to your computer and use it in GitHub Desktop.
Save cmho/3967ed371a1bd396138d3af3ea2bb2f4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
"""
posts to thoughts.page
"""
import argparse
import re
from bs4 import BeautifulSoup
import sys
from urllib import request, parse
SESSION_ID = 'SESSID'
TIMEZONE = 'whatever'
YOUR_PAGE = 'https://you.thoughts.page'
parser = argparse.ArgumentParser(description='Posts and manages thoughts on thoughts.page.')
parser.add_argument('thought', help='thought you want to post.')
parser.add_argument('--reply-to', default=None, dest='reply_to_id', help='id of post to reply to')
parser.add_argument('--delete', default=None, dest='delete_id', help='id of post to delete. note that selecting this will not allow you to post at the same time')
parser.add_argument('--tag', default=None, dest='tag_name', help='tag to append to front of post')
def get_last_tagged_post(tag):
"""Returns the ID of the post most recently tagged with the given tag."""
with request.urlopen(YOUR_PAGE) as page:
page_soup = BeautifulSoup(page.read(), 'html.parser')
latest_post = filter(lambda ttag : ttag == '#'+tag, page_soup.find('code'))
if len(list(latest_post)) > 0:
return latest_post[0]['data-id']
return None
def get_post_by_id(id, do_cleanup=True, as_text=False):
"""Returns a post by ID."""
# when there's no api, you gotta get creative!
with request.urlopen(YOUR_PAGE) as page:
page_soup = BeautifulSoup(page.read(), 'html.parser')
post = page_soup.find('div', {'data-id': str(id)})
if do_cleanup:
post.attrs.clear()
#details = post.find('details')
#if details:
# details.replaceWith('')
if as_text: #only works on undefined.thoughts.page, whoops. change this to just look for <p> i guess
post_text = post.find('div', {'class': 'thought'})
return str(post_text.text)
return str(post)
def get_latest_post_id():
"""Returns the ID of the latest thought."""
# hacky, works only on my page
with request.urlopen(YOUR_PAGE) as page:
page_soup = BeautifulSoup(page.read(), 'html.parser')
post = page_soup.find('span', {'class': 'thoughtid'})
return str(post.text[1:])
args = parser.parse_args()
thought = args.thought
reply_to = args.reply_to_id
delete_id = args.delete_id
tag_name = args.tag_name
if reply_to == 'latest':
reply_to = get_latest_post_id()
if delete_id == 'latest':
delete_id = get_latest_post_id()
if delete_id:
try:
post = get_post_by_id(delete_id, as_text=True)
except:
print("\033[0;32mnonexistent id \033[0m" + delete_id + "\033[0;32m, or you've lost connection\033[0m")
quit(1)
print("contents:\n" + post)
yesno = input('are you sure you want to remove this post? [y/N] ')
if not yesno.lower() == 'y':
print("keeping post.")
quit(0)
req = request.Request('https://thoughts.page/api/delete_thought/' + delete_id, data={}, headers={'Cookie': 'session_id=' + SESSION_ID})
resp = request.urlopen(req)
print("\033[0;32mdeleted post \033[0m" + delete_id + "\033[0;32m.\033[0m")
quit(0)
if tag_name:
thought = "<code>#"+tag_name+"</code> "+thought
if reply_to:
reply_header = "<span class='replyheader'>reply to <a href='" + YOUR_PAGE + "/#" + str(reply_to) + "'>thought \#" + str(reply_to) + "</a>:</span>"
reply_header = reply_header + "<details><summary>see previous post</summary>" + get_post_by_id(reply_to) + "</details>"
thought = reply_header + "\n" + thought
if tag_name:
next_id = get_last_tagged_post(tag_name)
if next_id:
thought = thought+"<span class='nextlink'>Next in topic: <a href='#"+next_id+"'>#"+next_id+"</a></span>"
try:
data = parse.urlencode({'thought': thought, 'timezone': TIMEZONE}).encode()
req = request.Request('https://thoughts.page/post', data=data, headers={'Cookie': 'session_id=' + SESSION_ID})
resp = request.urlopen(req)
except:
print("\033[0;31mfailed to post!\033[0m")
quit(1)
print("\033[0;32mposted successfully!\033[0m")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment