Refactor reddit module to be more efficient.
This commit is contained in:
parent
9a96b92f68
commit
b9cc06e310
@ -1,10 +1,12 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
import re
|
||||
|
||||
import praw
|
||||
|
||||
from i3pystatus import IntervalModule
|
||||
from i3pystatus.core.util import internet, require, user_open
|
||||
|
||||
import praw
|
||||
|
||||
|
||||
class Reddit(IntervalModule):
|
||||
"""
|
||||
@ -70,83 +72,118 @@ class Reddit(IntervalModule):
|
||||
_permalink = ""
|
||||
_url = ""
|
||||
|
||||
subreddit_pattern = re.compile("\{submission_\w+\}")
|
||||
message_pattern = re.compile("\{message_\w+\}")
|
||||
user_pattern = re.compile("\{comment_karma\}|\{link_karma\}")
|
||||
|
||||
reddit_session = None
|
||||
|
||||
@require(internet)
|
||||
def run(self):
|
||||
r = praw.Reddit(user_agent='i3pystatus')
|
||||
reddit = self.connect()
|
||||
fdict = {}
|
||||
|
||||
if self.password:
|
||||
r.login(self.username, self.password, disable_warning=True)
|
||||
unread_messages = sum(1 for i in r.get_unread())
|
||||
if unread_messages:
|
||||
d = vars(next(r.get_unread()))
|
||||
fdict = {
|
||||
"message_unread": unread_messages,
|
||||
"message_author": d["author"],
|
||||
"message_subject": d["subject"],
|
||||
"message_body": d["body"].replace("\n", " "),
|
||||
"status": self.status["new_mail"]
|
||||
}
|
||||
else:
|
||||
fdict = {
|
||||
"message_unread": "",
|
||||
"status": self.status["no_mail"]
|
||||
}
|
||||
if self.message_pattern.search(self.format):
|
||||
fdict.update(self.get_messages(reddit))
|
||||
if self.subreddit_pattern.search(self.format):
|
||||
fdict.update(self.get_subreddit(reddit))
|
||||
if self.user_pattern.search(self.format):
|
||||
fdict.update(self.get_redditor(reddit))
|
||||
|
||||
if self.subreddit:
|
||||
s = r.get_subreddit(self.subreddit)
|
||||
else:
|
||||
s = r
|
||||
if self.sort_by == 'hot':
|
||||
if not self.subreddit:
|
||||
d = vars(next(s.get_front_page(limit=1)))
|
||||
else:
|
||||
d = vars(next(s.get_hot(limit=1)))
|
||||
elif self.sort_by == 'new':
|
||||
d = vars(next(s.get_new(limit=1)))
|
||||
elif self.sort_by == 'rising':
|
||||
d = vars(next(s.get_rising(limit=1)))
|
||||
elif self.sort_by == 'controversial':
|
||||
d = vars(next(s.get_controversial(limit=1)))
|
||||
elif self.sort_by == 'top':
|
||||
d = vars(next(s.get_top(limit=1)))
|
||||
|
||||
fdict["submission_title"] = d["title"]
|
||||
fdict["submission_author"] = d["author"]
|
||||
fdict["submission_points"] = d["ups"]
|
||||
fdict["submission_comments"] = d["num_comments"]
|
||||
fdict["submission_permalink"] = d["permalink"]
|
||||
fdict["submission_url"] = d["url"]
|
||||
fdict["submission_domain"] = d["domain"]
|
||||
fdict["submission_subreddit"] = d["subreddit"]
|
||||
|
||||
self._permalink = fdict["submission_permalink"]
|
||||
self._url = fdict["submission_url"]
|
||||
|
||||
if self.colorize and fdict["message_unread"]:
|
||||
if self.colorize and fdict.get("message_unread", False):
|
||||
color = self.color_orangered
|
||||
if self.mail_brackets:
|
||||
fdict["message_unread"] = "[{}]".format(unread_messages)
|
||||
fdict["message_unread"] = "[{}]".format(fdict["message_unread"])
|
||||
else:
|
||||
color = self.color
|
||||
|
||||
if len(fdict["submission_title"]) > self.title_maxlen:
|
||||
title = fdict["submission_title"][:(self.title_maxlen - 3)] + "..."
|
||||
fdict["submission_title"] = title
|
||||
|
||||
if self.username:
|
||||
u = r.get_redditor(self.username)
|
||||
fdict["link_karma"] = u.link_karma
|
||||
fdict["comment_karma"] = u.comment_karma
|
||||
else:
|
||||
fdict["link_karma"] = ""
|
||||
fdict["comment_karma"] = ""
|
||||
|
||||
full_text = self.format.format(**fdict)
|
||||
self.output = {
|
||||
"full_text": full_text,
|
||||
"color": color,
|
||||
}
|
||||
|
||||
def connect(self):
|
||||
if not self.reddit_session:
|
||||
self.reddit_session = praw.Reddit(user_agent='i3pystatus')
|
||||
return self.reddit_session
|
||||
|
||||
def get_redditor(self, reddit):
|
||||
redditor_info = {}
|
||||
if self.username:
|
||||
u = reddit.get_redditor(self.username)
|
||||
redditor_info["link_karma"] = u.link_karma
|
||||
redditor_info["comment_karma"] = u.comment_karma
|
||||
else:
|
||||
redditor_info["link_karma"] = ""
|
||||
redditor_info["comment_karma"] = ""
|
||||
return redditor_info
|
||||
|
||||
def get_messages(self, reddit):
|
||||
message_info = {
|
||||
"message_unread": "",
|
||||
"status": self.status["no_mail"],
|
||||
"message_author": "",
|
||||
"message_subject": "",
|
||||
"message_body": ""
|
||||
}
|
||||
if self.password:
|
||||
self.log_in(reddit)
|
||||
unread_messages = sum(1 for i in reddit.get_unread())
|
||||
if unread_messages:
|
||||
d = vars(next(reddit.get_unread()))
|
||||
message_info = {
|
||||
"message_unread": unread_messages,
|
||||
"message_author": d["author"],
|
||||
"message_subject": d["subject"],
|
||||
"message_body": d["body"].replace("\n", " "),
|
||||
"status": self.status["new_mail"]
|
||||
}
|
||||
|
||||
return message_info
|
||||
|
||||
def log_in(self, reddit):
|
||||
if not reddit.is_logged_in():
|
||||
reddit.login(self.username, self.password, disable_warning=True)
|
||||
|
||||
def get_subreddit(self, reddit):
|
||||
fdict = {}
|
||||
subreddit_dict = {}
|
||||
if self.subreddit:
|
||||
s = reddit.get_subreddit(self.subreddit)
|
||||
else:
|
||||
s = reddit
|
||||
if self.sort_by == 'hot':
|
||||
if not self.subreddit:
|
||||
subreddit_dict = vars(next(s.get_front_page(limit=1)))
|
||||
else:
|
||||
subreddit_dict = vars(next(s.get_hot(limit=1)))
|
||||
elif self.sort_by == 'new':
|
||||
subreddit_dict = vars(next(s.get_new(limit=1)))
|
||||
elif self.sort_by == 'rising':
|
||||
subreddit_dict = vars(next(s.get_rising(limit=1)))
|
||||
elif self.sort_by == 'controversial':
|
||||
subreddit_dict = vars(next(s.get_controversial(limit=1)))
|
||||
elif self.sort_by == 'top':
|
||||
subreddit_dict = vars(next(s.get_top(limit=1)))
|
||||
fdict["submission_title"] = subreddit_dict["title"]
|
||||
fdict["submission_author"] = subreddit_dict["author"]
|
||||
fdict["submission_points"] = subreddit_dict["ups"]
|
||||
fdict["submission_comments"] = subreddit_dict["num_comments"]
|
||||
fdict["submission_permalink"] = subreddit_dict["permalink"]
|
||||
fdict["submission_url"] = subreddit_dict["url"]
|
||||
fdict["submission_domain"] = subreddit_dict["domain"]
|
||||
fdict["submission_subreddit"] = subreddit_dict["subreddit"]
|
||||
|
||||
if len(fdict["submission_title"]) > self.title_maxlen:
|
||||
title = fdict["submission_title"][:(self.title_maxlen - 3)] + "..."
|
||||
fdict["submission_title"] = title
|
||||
|
||||
self._permalink = fdict["submission_permalink"]
|
||||
self._url = fdict["submission_url"]
|
||||
|
||||
return fdict
|
||||
|
||||
def open_mail(self):
|
||||
user_open('https://www.reddit.com/message/unread/')
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user