Skip to content

Instantly share code, notes, and snippets.

@gavin19
Created June 24, 2019 14:16
Show Gist options
  • Save gavin19/00d8fb3aca140cc5efcbe89d888a49d7 to your computer and use it in GitHub Desktop.
Save gavin19/00d8fb3aca140cc5efcbe89d888a49d7 to your computer and use it in GitHub Desktop.
Strip text content from Echo posts and submit comment
#!/usr/bin/python3
import os
import sys
import praw
import requests
import simplejson
from bs4 import BeautifulSoup as BSp
import html2text
from time import sleep
class Echo:
def __init__(self):
try:
os.chdir(os.path.dirname(sys.argv[0]))
except FileNotFoundError:
pass
self.done = []
self.login()
def login(self):
self.reddit = praw.Reddit(user_agent='python:/u/some_user:echo_bot:0.1',
client_id='some_id',
client_secret='some_client',
username='some_user',
password='password')
self.get_done()
def get_done(self):
"""Check for already done posts."""
if os.path.exists("done.txt"):
with open("done.txt", 'r') as d:
self.done = simplejson.load(d)
self.get_posts()
def get_posts(self):
"""Fetch posts."""
new = self.reddit.subreddit('liverpool').new(limit=10)
for n in new:
if 'liverpoolecho.co.uk' in n.url and n.id not in self.done:
self.post_summary_echo(n)
sleep(5)
self.clean_up()
def post_summary_echo(self, n):
"""Construct summary for Echo."""
req = requests.get(n.url).text
htm = BSp(req, 'lxml')
if n.url.endswith("amp"):
con = htm.select('.lead-text~article>.body>p')
else:
con = htm.select('.article-body>p')
if con:
h = html2text.HTML2Text()
h.body_width = 0
con = ''.join(str(x) for x in con)
con = h.handle(con)
con = "######Text from the linked article:\n\n" + con
if len(con) > 9990:
con = con[:9940]
con = con + "\n\n... (truncated. Article length exceeds comment limit)"
rep = n.reply(con)
rep.mod.distinguish(how='yes', sticky=True)
self.done.append(n.id)
def clean_up(self):
"""Add done posts to archive."""
with open('done.txt', 'w') as d:
simplejson.dump(self.done, d)
Echo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment