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