Fix for unicode pull request #33
This commit is contained in:
@@ -8,31 +8,19 @@ import requests
|
||||
|
||||
from .errors import SubmissionURLError, SubredditNameError
|
||||
|
||||
is_utf8 = True
|
||||
FORCE_ASCII = True
|
||||
|
||||
def clean(string):
|
||||
|
||||
def clean(unicode_string):
|
||||
"""
|
||||
Checks if -a was given, if it was it
|
||||
converts unicode string into ascii-safe characters.
|
||||
"""
|
||||
if not is_utf8:
|
||||
if six.PY2:
|
||||
ascii_string = unicode_string.encode('ascii', 'replace')
|
||||
else:
|
||||
ascii_string = unicode_string.encode().decode('ascii', 'replace')
|
||||
encoding = 'ascii' if FORCE_ASCII else 'utf-8'
|
||||
|
||||
ascii_string = ascii_string.replace('\\', '')
|
||||
return ascii_string
|
||||
if six.PY2:
|
||||
out = string.encode(encoding, 'replace')
|
||||
else:
|
||||
if six.PY2:
|
||||
utf8_string = unicode_string.encode('utf-8', 'replace')
|
||||
else:
|
||||
utf8_string = unicode_string.encode().decode('utf-8', 'replace')
|
||||
|
||||
utf8_string = utf8_string.replace('\\', '')
|
||||
return utf8_string
|
||||
out = string.encode().decode(encoding, 'replace')
|
||||
|
||||
out = out.replace('\\', '')
|
||||
return out
|
||||
|
||||
def split_text(big_text, width):
|
||||
return [
|
||||
@@ -374,6 +362,7 @@ class SubredditContent(BaseContent):
|
||||
try:
|
||||
content.get(0)
|
||||
except:
|
||||
# TODO: Trap specific errors
|
||||
raise SubredditNameError(display_name)
|
||||
|
||||
return content
|
||||
|
||||
29
rtv/main.py
29
rtv/main.py
@@ -1,19 +1,26 @@
|
||||
import argparse
|
||||
import locale
|
||||
|
||||
import praw
|
||||
from requests.exceptions import ConnectionError, HTTPError
|
||||
from praw.errors import InvalidUserPass
|
||||
|
||||
from . import content
|
||||
from .errors import SubmissionURLError, SubredditNameError
|
||||
from .utils import curses_session, load_config, HELP
|
||||
from .subreddit import SubredditPage
|
||||
from .submission import SubmissionPage
|
||||
import locale
|
||||
import rtv.content
|
||||
|
||||
# Debugging
|
||||
# import logging
|
||||
# logging.basicConfig(level=logging.DEBUG, filename='rtv.log')
|
||||
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
||||
AGENT = """
|
||||
desktop:https://github.com/michael-lazar/rtv:(by /u/civilization_phaze_3)
|
||||
"""
|
||||
|
||||
DESCRIPTION = """
|
||||
Reddit Terminal Viewer is a lightweight browser for www.reddit.com built into a
|
||||
terminal window.
|
||||
@@ -39,7 +46,7 @@ def main():
|
||||
parser.add_argument('-s', dest='subreddit', help='subreddit name')
|
||||
parser.add_argument('-l', dest='link', help='full link to a submission')
|
||||
parser.add_argument('--force-ascii', dest='force_ascii',
|
||||
help='forces ascii (disables unicode)', action='store_true')
|
||||
help='disable unicode', action='store_true')
|
||||
|
||||
group = parser.add_argument_group(
|
||||
'authentication (optional)',
|
||||
@@ -57,15 +64,14 @@ def main():
|
||||
if getattr(args, key) is None:
|
||||
setattr(args, key, val)
|
||||
|
||||
content.FORCE_ASCII = args.force_ascii
|
||||
|
||||
if args.subreddit is None:
|
||||
args.subreddit = 'front'
|
||||
|
||||
if args.force_ascii:
|
||||
rtv.content.is_utf8 = False
|
||||
locale.setlocale(locale.LC_ALL, '')
|
||||
|
||||
try:
|
||||
reddit = praw.Reddit(user_agent='desktop:https://github.com/michael-lazar/rtv:(by /u/civilization_phaze_3)')
|
||||
print('Connecting...')
|
||||
reddit = praw.Reddit(user_agent=AGENT)
|
||||
reddit.config.decode_html_entities = True
|
||||
|
||||
if args.username:
|
||||
@@ -83,9 +89,6 @@ def main():
|
||||
except InvalidUserPass:
|
||||
print('Invalid password for username: {}'.format(args.username))
|
||||
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
|
||||
except ConnectionError:
|
||||
print('Connection timeout: Could not connect to http://www.reddit.com')
|
||||
|
||||
@@ -97,3 +100,7 @@ def main():
|
||||
|
||||
except SubredditNameError as e:
|
||||
print('Could not reach subreddit: {}'.format(e.name))
|
||||
|
||||
except KeyboardInterrupt:
|
||||
return
|
||||
|
||||
|
||||
Reference in New Issue
Block a user