Skip to content

Instantly share code, notes, and snippets.

@knuxify
Created Sep 16, 2021
Embed
What would you like to do?
#!/usr/bin/env python3
"""
posts to thoughts.page
"""
import argparse
from bs4 import BeautifulSoup
import sys
from urllib import request, parse
SESSION_ID = 'get the session-id from the session_id cookie'
TIMEZONE = 'Europe/Warsaw'
YOUR_PAGE = 'https://linktoyour.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')
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', {'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': 'thoughtcontent'})
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
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 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
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