import requests

import re

def select_sub ( ) :

user_in = ''

while user_in == '' :

user_in = input ( "View: /r/" )

if user_in. lower ( ) == 'q' or user_in. lower ( ) == 'quit' or user_in. lower ( ) == 'e' or user_in. lower ( ) == 'exit' :

exit ( )

try :

threads = requests. get ( "https://www.reddit.com/r/%s.json" % user_in , headers = { 'User-agent' : 'kyaabo' } ) . json ( ) [ 'data' ] [ 'children' ]

if not len ( threads ) :

print ( "Subreddit does not exist or has no threads." )

user_in = ''

except ( requests. exceptions . RequestException , KeyError ) as error:

if type ( error ) is requests. exceptions . RequestException :

print ( "Access denied." )

elif type ( error ) is KeyError :

print ( "Invalid subreddit entered." )

user_in = ''

return threads

def select_thread ( threads ) :

current_thread = 0

thread_sel = - 1

while not 0 <= thread_sel < len ( threads ) :

for n in range ( 5 ) :

if current_thread < len ( threads ) :

print ( "[#%d] %s ::%d Karma::" % ( current_thread , threads [ current_thread+n ] [ 'data' ] [ 'title' ] , threads [ n ] [ 'data' ] [ 'ups' ] ) )

current_thread + = 1

if current_thread == len ( threads ) :

print ( "There are no more threads." )

user_in = input ( "COMMANDS: [Thread #] | [ENTER] View more threads | [b] Go back | [q] Quit program

Command: " )

if user_in == '' :

pass

elif user_in. lower ( ) == 'b' :

return False

elif user_in. lower ( ) == 'q' :

exit ( )

else :

try :

if user_in [ 0 ] == '#' :

user_in = user_in [ 1 : ]

if 0 <= int ( user_in ) < len ( threads ) :

thread_sel = int ( user_in )

else :

print ( "Invalid thread selected." )

except ValueError :

print ( "Invalid thread selected." )

print ( "TITLE: %s" % threads [ thread_sel ] [ 'data' ] [ 'title' ] )

print ( "AUTHOR: %s" % threads [ thread_sel ] [ 'data' ] [ 'author' ] )

re . sub ( r '

+' , '

' , threads [ thread_sel ] [ 'data' ] [ 'selftext' ] )

print ( "SELFTEXT: %s" % threads [ thread_sel ] [ 'data' ] [ 'selftext' ] )

print ( "--------" )

comments = requests. get ( "%s.json" % threads [ thread_sel ] [ 'data' ] [ 'url' ] , headers = { 'User-agent' : 'kyaabo' } ) . json ( ) [ 1 ] [ 'data' ] [ 'children' ]

return comments

def view_replies ( replies , lvl ) :

re . sub ( r '

+' , '

' , replies [ 0 ] [ 'data' ] [ 'body' ] )

print ( "--------

<+%s>[%s]: %s

--------" % ( lvl , replies [ 0 ] [ 'data' ] [ 'author' ] , replies [ 0 ] [ 'data' ] [ 'body' ] ) )

user_in = input ( "COMMANDS: [ENTER] View next reply | [+] View comment replies | [b] Go back | [q] Quit program

Command: " )

if user_in == '' :

replies = replies [ 1 : ]

for reply in replies:

try :

if user_in == '' :

re . sub ( r '

+' , '

' , reply [ 'data' ] [ 'body' ] )

print ( "--------

<+%s>[%s]: %s

--------" % ( lvl , reply [ 'data' ] [ 'author' ] , reply [ 'data' ] [ 'body' ] ) )

elif user_in == '+' :

pass

elif user_in == 'b' :

return

elif user_in == 'q' :

exit ( )

else :

print ( 'Invalid input.' )

except KeyError :

pass

if user_in != '+' :

user_in = input ( "COMMANDS: [ENTER] View next reply | [+] View comment replies | [b] Go back | [q] Quit program

Command: " )

while user_in == '+' :

if len ( reply [ 'data' ] [ 'replies' ] [ 'data' ] [ 'children' ] ) and 'body' in reply [ 'data' ] [ 'replies' ] [ 'data' ] [ 'children' ] [ 0 ] [ 'data' ] :

view_replies ( reply [ 'data' ] [ 'replies' ] [ 'data' ] [ 'children' ] , lvl + 1 )

else :

print ( "No replies." )

print ( "--------

<+%s>[%s]: %s

--------" % ( lvl , reply [ 'data' ] [ 'author' ] , reply [ 'data' ] [ 'body' ] ) )

user_in = input ( "COMMANDS: [ENTER] View next reply | [+] View comment replies | [b] Go back | [q] Quit program

Command: " )

return

def view_comments ( comments ) :

user_in = input ( "COMMANDS: [ENTER] View comments | [b] Go back | [q] Quit program

Command: " )

while user_in != '' and user_in. lower ( ) != 'b' and user_in. lower ( ) != 'q' :

user_in = input ( "Invalid command.

COMMANDS: [ENTER] View comments | [b] Go back | [q] Quit program

Command: " )

for comment in comments:

try :

if user_in == '' :

re . sub ( r '

+' , '

' , comment [ 'data' ] [ 'body' ] )

print ( "--------

[%s]: %s

--------" % ( comment [ 'data' ] [ 'author' ] , comment [ 'data' ] [ 'body' ] ) )

elif user_in. lower ( ) == 'b' :

return

elif user_in == 'q' :

exit ( )

else :

print ( 'Invalid input.' )

except KeyError :

pass

user_in = input ( "COMMANDS: [ENTER] View next comment | [+] View comment replies | [b] Go back | [q] Quit program

Command: " )

while user_in == '+' :

if len ( comment [ 'data' ] [ 'replies' ] [ 'data' ] [ 'children' ] ) :

view_replies ( comment [ 'data' ] [ 'replies' ] [ 'data' ] [ 'children' ] , 1 )

else :

print ( "No replies." )

print ( "--------

[%s]: %s

--------" % ( comment [ 'data' ] [ 'author' ] , comment [ 'data' ] [ 'body' ] ) )

user_in = input ( "COMMANDS: [ENTER] View next comment | [+] View comment replies | [b] Go back | [q] Quit program

Command: " )

return

try :

sel_sub = True

while True :

if sel_sub:

threads = select_sub ( )

sel_sub = True

comments = select_thread ( threads )

if comments:

sel_sub = view_comments ( comments )

except ( KeyboardInterrupt , SystemExit ) :