Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit fdafa7b
Showing
12 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import praw | ||
import sys | ||
import webbrowser | ||
import connection_manager | ||
import ui_manager | ||
|
||
class CmdParser: | ||
def __init__(self, reddit, default_page_size, default_num_comments): | ||
self.reddit = reddit | ||
self.subreddit = None | ||
self.submission = None | ||
self.current_page = [] | ||
self.page_size = default_page_size | ||
self.num_comments = default_num_comments | ||
|
||
def cmd_help(self): | ||
print '** Welcome to pyreddit **' | ||
print 'h, help\t\t\t\t\t-\tprint this screen.' | ||
print 's, subreddit <a> <b=hot> <c=' + str(self.page_size) + '>\t\t-\tconnect to subreddit <a>, with optional tab <b> (tabs listed below), <c> many posts.' | ||
print '\t\t\t\t\t\tno arguments will reprint the current subreddit.' | ||
print '\t\t\t\t\t\ttabs: hot, controversial, new, rising, top (default hot).' | ||
print 'n, next <a=' + str(self.page_size) + '>\t\t\t\t-\tload the next <a> many posts from the current subreddit.' | ||
print 'o, open <a> <b=' + str(self.num_comments) + '>\t\t\t-\topen post (index <a>) from current subreddit, with <b> many comments.' | ||
print 'url <a>\t\t\t\t\t-\topen the current post\'s url, or index <a>\'s (if given), under the current subreddit.' | ||
print 'q, quit\t\t\t\t\t-\tquits pyreddit' | ||
|
||
def cmd_subreddit(self, title, tab, page_size): | ||
self.subreddit = connection_manager.connect_to_subreddit(self.reddit, title, tab) | ||
self.cmd_next_page(page_size) | ||
ui_manager.print_subreddit(self.reddit, self.current_page) | ||
|
||
def cmd_next_page(self, page_size): | ||
self.current_page = [] | ||
for i in range(page_size): | ||
self.current_page.append(self.subreddit.next()) | ||
|
||
def cmd_open(self, index, num_comments): | ||
if self.subreddit == None: | ||
raise Exception('Not currently connected to a subreddit.') | ||
self.submission = self.current_page[index] | ||
ui_manager.print_submission(self.reddit, self.submission, num_comments) | ||
|
||
def cmd_open_url(self, index=None): | ||
if index is not None: | ||
self.submission = self.current_page[index] | ||
elif self.submission == None: | ||
raise Exception('Not currently connected to a post.') | ||
webbrowser.open_new_tab(self.submission.url) | ||
|
||
def parse_cmd(self, raw_cmd): | ||
if raw_cmd == '': | ||
return | ||
|
||
cmd_tokens = raw_cmd.split() | ||
|
||
cmd = cmd_tokens[0] | ||
if cmd == 'h' or cmd == 'help': | ||
self.cmd_help() | ||
|
||
elif cmd == 's' or cmd == 'subreddit': | ||
self.submission = None | ||
if len(cmd_tokens) == 1: | ||
if self.subreddit == None: | ||
raise Exception('Not currently connected to a subreddit.') | ||
ui_manager.print_subreddit(self.reddit, self.current_page) | ||
elif len(cmd_tokens) == 2: | ||
self.cmd_subreddit(cmd_tokens[1], 'hot', self.page_size) | ||
elif len(cmd_tokens) == 3: | ||
self.cmd_subreddit(cmd_tokens[1], cmd_tokens[2], self.page_size) | ||
elif len(cmd_tokens) == 4: | ||
self.cmd_subreddit(cmd_tokens[1], cmd_tokens[2], int(cmd_tokens[3])) | ||
|
||
elif cmd == 't' or cmd == 'tab': | ||
self.cmd_tab(cmd_tokens[1]) | ||
|
||
elif cmd == 'n' or cmd == 'next': | ||
self.submission = None | ||
if len(cmd_tokens) == 1: | ||
self.cmd_next_page(self.page_size) | ||
elif len(cmd_tokens) == 2: | ||
self.cmd_next_page(int(cmd_tokens[1])) | ||
ui_manager.print_subreddit(self.reddit, self.current_page) | ||
|
||
elif cmd == 'o' or cmd == 'open': | ||
num_comments = self.num_comments | ||
if (len(cmd_tokens) > 2): | ||
num_comments = int(cmd_tokens[2]) | ||
self.cmd_open(int(cmd_tokens[1]), num_comments) | ||
|
||
elif cmd == 'url': | ||
if len(cmd_tokens) == 1: | ||
self.cmd_open_url() | ||
elif len(cmd_tokens) == 2: | ||
self.cmd_open_url(int(cmd_tokens[1])) | ||
|
||
elif cmd == 'q' or cmd == 'quit': | ||
print 'Closing ...' | ||
raise SystemExit | ||
|
||
else: | ||
raise Exception('Error parsing. Type \'h\' or \'help\' for help.') | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import sys | ||
import praw | ||
|
||
""" | ||
main entry point into reddit, connects using OAuth certificate | ||
""" | ||
def connect_to_reddit(client_id, client_secret, user_agent, username, password): | ||
reddit = praw.Reddit( client_id=client_id, | ||
client_secret=client_secret, | ||
user_agent=user_agent, | ||
username=username, | ||
password=password) | ||
return reddit | ||
|
||
""" | ||
attempts to connect to a subreddit given the subreddits name and a praw instance | ||
""" | ||
def connect_to_subreddit(reddit, subreddit_title, tab): | ||
if tab == 'hot': | ||
return reddit.subreddit(subreddit_title).hot() | ||
elif tab == 'controversial': | ||
return reddit.subreddit(subreddit_title).controversial() | ||
elif tab == 'new': | ||
return reddit.subreddit(subreddit_title).new() | ||
elif tab == 'rising': | ||
return reddit.subreddit(subreddit_title).rising() | ||
else: | ||
return reddit.subreddit(subreddit_title).top() | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
default_client_id=XXX | ||
default_client_secret=XXX | ||
default_user_agent=XXX | ||
default_username=XXX | ||
default_password=XXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
|
||
settings_filename = './settings.txt' | ||
credentials_filename = './credentials.txt' | ||
|
||
def load_credentials_from_file(): | ||
global credentials_filename | ||
f = open(credentials_filename, 'r') | ||
client_id = '' | ||
client_secret = '' | ||
user_agent = '' | ||
username = '' | ||
password = '' | ||
|
||
for line in f: | ||
line_bits = line.split('=') | ||
if line_bits[0] == 'default_client_id': | ||
client_id = line_bits[1][:-1] # include [:-1] to get rid of included \n | ||
elif line_bits[0] == 'default_client_secret': | ||
client_secret = line_bits[1][:-1] | ||
elif line_bits[0] == 'default_user_agent': | ||
user_agent = line_bits[1][:-1] | ||
elif line_bits[0] == 'default_username': | ||
username = line_bits[1][:-1] | ||
elif line_bits[0] == 'default_password': | ||
password = line_bits[1][:-1] | ||
else: | ||
raise Exception('Unexpected token <' + line_bits[0] + '> recieved when reading from credentials file.') | ||
|
||
print 'a', 'b', 'c' | ||
print client_id, client_secret, user_agent, username, password | ||
|
||
if client_id == '' or client_secret == '' or user_agent == '' or username == '' or password == '': | ||
raise Exception('Error reading from credentials file.') | ||
|
||
return client_id, client_secret, user_agent, username, password | ||
|
||
def load_settings_from_file(): | ||
global settings_filename | ||
f = open(settings_filename, 'r') | ||
default_page_size = -1 | ||
default_num_comments = -1 | ||
|
||
for line in f: | ||
line_bits = line.split('=') | ||
if line_bits[0] == 'default_page_size': | ||
default_page_size = int(line_bits[1]) | ||
elif line_bits[0] == 'default_num_comments': | ||
default_num_comments = int(line_bits[1]) | ||
else: | ||
raise Exception('Unexpected token <' + line_bits[0] + '> recieved when reading from settings file.') | ||
|
||
if default_page_size == -1 or default_num_comments == -1: | ||
raise Exception('Error reading from settings file.') | ||
|
||
return default_page_size, default_num_comments | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import praw | ||
import sys | ||
from colorama import init, Fore | ||
import connection_manager | ||
import ui_manager | ||
from cmd_parser import CmdParser | ||
import io_manager | ||
|
||
def version(): | ||
return 'pyreddit v0.1' | ||
|
||
def get_cmd(): | ||
sys.stdout.write(Fore.WHITE + '(pyreddit) > ') | ||
return raw_input() | ||
|
||
def main(): | ||
print 'Reading credentials from credentials.txt...' | ||
client_id, client_secret, user_agent, username, password = io_manager.load_credentials_from_file() | ||
print 'Read successfully!' | ||
|
||
print 'Connecting to reddit...' | ||
reddit = connection_manager.connect_to_reddit(client_id, client_secret, user_agent, username, password) | ||
#print 'Connection successful! Logged in as user', reddit.user.me() | ||
|
||
print 'Reading settings from settings.txt...' | ||
default_page_size, default_num_comments = io_manager.load_settings_from_file() | ||
print 'Read successfully!' | ||
|
||
print 'Startting pyreddit...' | ||
cmd_parser = CmdParser(reddit, default_page_size, default_num_comments) | ||
ui_manager.reset_screen(reddit) | ||
cmd_parser.cmd_help() | ||
|
||
while True: | ||
try: | ||
cmd_parser.parse_cmd(get_cmd()) | ||
except SystemExit: | ||
quit() | ||
except Exception, e: | ||
print e | ||
|
||
if __name__ == '__main__': | ||
init() | ||
main() |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
default_num_comments=15 | ||
default_page_size=12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import praw | ||
import os | ||
from colorama import Fore | ||
|
||
comment_counter = 0 | ||
|
||
def reset_screen(reddit): | ||
os.system('cls' if os.name == 'nt' else 'clear') | ||
print Fore.RED + '**' + Fore.WHITE + 'Logged in as user:', Fore.CYAN + str(reddit.user.me()), Fore.RED + '**', Fore.WHITE | ||
|
||
def print_subreddit(reddit, subreddit): | ||
reset_screen(reddit) | ||
|
||
for i, submission in enumerate(subreddit): | ||
print Fore.WHITE + '--[', Fore.RED + str(i), Fore.WHITE + ']----------------------' | ||
print Fore.GREEN + str(submission.score), Fore.WHITE + ':', Fore.BLUE + submission.title | ||
print Fore.WHITE + str(submission.author), Fore.YELLOW + str(submission.num_comments) + ' comments', Fore.BLUE + 'URL:', Fore.CYAN + submission.url | ||
|
||
def print_submission(reddit, submission, max_comments): | ||
global comment_counter | ||
reset_screen(reddit) | ||
|
||
print Fore.BLUE + submission.title, Fore.GREEN + str(submission.score) | ||
print Fore.CYAN + submission.url | ||
|
||
submission_bits = _split_text_body(submission.selftext, 100) | ||
for bit in submission_bits: | ||
print bit | ||
|
||
comment_counter = 0 | ||
submission.comments.replace_more(limit=0) | ||
for comment in submission.comments: | ||
_print_comments(comment, '', max_comments) | ||
|
||
def _print_comments(comment, tab, max_comments): | ||
global comment_counter | ||
|
||
comment_counter += 1 | ||
if comment_counter > max_comments: | ||
return | ||
|
||
print Fore.CYAN + tab + Fore.BLUE + str(comment.author), Fore.GREEN + '[' + str(comment.score) + ']', Fore.WHITE + ':' | ||
|
||
to_print = comment.body.replace('\n', ' ') | ||
comment_bits = _split_text_body(to_print, 100) | ||
|
||
for bit in comment_bits: | ||
print Fore.CYAN + tab + Fore.WHITE + bit | ||
|
||
for reply in comment.replies: | ||
_print_comments(reply, tab + ' | ', max_comments) | ||
|
||
def _split_text_body(body, blocksize): | ||
body_bits = [] | ||
i = 0 | ||
j = 0 | ||
while i < len(body): | ||
j += 100 | ||
if j > len(body): | ||
j = len(body) | ||
else: | ||
while j < len(body) and body[j] != ' ': | ||
j += 1 | ||
body_bits.append(body[i:j]) | ||
i = j + 1 | ||
return body_bits |
Binary file not shown.