Merge pull request #126 from obosob/internal_links

Handle internal reddit links internally, including x-posts
This commit is contained in:
michael-lazar
2015-08-05 13:55:30 -07:00
2 changed files with 21 additions and 7 deletions

View File

@@ -2,6 +2,7 @@ import logging
import praw
import requests
import re
from .exceptions import SubmissionError, SubredditError, AccountError
from .helpers import humanize_timestamp, wrap_text, strip_subreddit_url
@@ -105,7 +106,7 @@ class BaseContent(object):
displayed through the terminal.
"""
is_selfpost = lambda s: s.startswith('http://www.reddit.com/r/')
reddit_link = re.compile("https?://(www\.)?(np\.)?redd(it\.com|\.it)/r/.*")
author = getattr(sub, 'author', '[deleted]')
name = getattr(author, 'name', '[deleted]')
flair = getattr(sub, 'link_flair_text', '')
@@ -123,7 +124,19 @@ class BaseContent(object):
data['subreddit'] = strip_subreddit_url(sub.permalink)
data['flair'] = flair
data['url_full'] = sub.url
data['url'] = ('selfpost' if is_selfpost(sub.url) else sub.url)
if data['permalink'].split('/r/')[-1] == data['url_full'].split('/r/')[-1]:
data['url_type'] = 'selfpost'
data['url'] = 'selfpost'
elif reddit_link.match(data['url_full']):
data['url_type'] = 'x-post'
data['url'] = 'x-post via {}'.format(strip_subreddit_url(data['url_full']))
else:
data['url_type'] = 'external'
data['url'] = data['url_full']
data['likes'] = sub.likes
data['gold'] = sub.gilded > 0
data['nsfw'] = sub.over_18
@@ -154,7 +167,7 @@ class SubmissionContent(BaseContent):
try:
with loader():
submission = reddit.get_submission(url, comment_sort='hot')
submission = reddit.get_submission(url.replace("http:","https:"), comment_sort='hot')
except praw.errors.APIException:
raise SubmissionError(url)

View File

@@ -9,7 +9,7 @@ from .exceptions import SubredditError, AccountError
from .page import BasePage, Navigator, BaseController
from .submission import SubmissionPage
from .content import SubredditContent
from .helpers import open_browser, open_editor
from .helpers import open_browser, open_editor, strip_subreddit_url
from .docs import SUBMISSION_FILE
from .history import load_history, save_history
from .curses_helpers import (Color, LoadScreen, add_line, get_arrow, get_gold,
@@ -108,10 +108,11 @@ class SubredditPage(BasePage):
"Open a link with the webbrowser"
data = self.content.get(self.nav.absolute_index)
if data['url'] == 'selfpost':
self.open_submission()
url = data['url_full']
if data['url_type'] in ['x-post', 'selfpost']:
page = SubmissionPage(self.stdscr, self.reddit, url=url)
page.loop()
else:
url = data['url_full']
open_browser(url)
global history
history.add(url)