Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- import praw
- import sys
- import html.parser
- from datetime import datetime as time
- import OAuth2Util
- def main():
- subr = "Everton"
- start = '[](/start)'
- end = '[](/end)'
- ua = 'Sidebar updater for /r/Everton'
- top = []
- tbl_top = '|Top recent discussions|[](/comm "No. of comments")\n:-:|:-:\n'
- tbl_row = '|[{0}]({1})|[{2}]({3})\n'
- content = start + '\n\n'
- # login
- r = praw.Reddit(user_agent=ua)
- o = OAuth2Util.OAuth2Util(r)
- o.refresh()
- target_sub = r.get_subreddit(subr)
- # get top 30 posts from 'new'
- listing = r.get_subreddit(subr).get_new(limit=30)
- for post in listing:
- top.append(post)
- # sort submission objects by number of comments (most first)
- top.sort(key=lambda x: x.num_comments, reverse=True)
- listing = []
- for i in range(3):
- top[i].title = top[i].title.replace('|', '|')
- top[i].url = top[i].url.replace('|', '|')
- listing.append([top[i].title,
- top[i].url,
- top[i].num_comments,
- top[i].short_link])
- print(top[i].title)
- print(top[i].url)
- # build markdown table and populate with post info
- content += tbl_top
- for i in listing:
- content += tbl_row.format(i[0], i[1], i[2], i[3])
- print(content)
- print(time.now().strftime('%Y-%m-%d %H:%M:%S'))
- desc = target_sub.get_settings()['description']
- desc = html.parser.HTMLParser().unescape(desc)
- before = desc.split(start)
- after = desc.split(end)
- new_desc = before[0] + content + end + after[1]
- target_sub.update_settings(description=new_desc)
- sys.exit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment