Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reddit: handle subreddits, and u/ & r/ references #1722

Merged
merged 1 commit into from
Nov 23, 2019

Conversation

deathbybandaid
Copy link
Contributor

This PR adds to the functionality of reddit.py by adding:

  • Add a subreddit command as well as url pattern match for subreddits
13:32 <@deathbybandaid> https://reddit.com/r/plex
13:32 <reddit-test> [REDDIT] https://www.reddit.com/r/plex | subscribers (124733) | Created at 2008-08-16 - 06:54:02UTC | For questions and comments about the Plex Media Server. The Plex Media Server is smart software that makes playing Movies, TV Shows and other media on your computer simple.
13:32 <@deathbybandaid> .subreddit plex
13:32 <reddit-test> [REDDIT] https://www.reddit.com/r/plex | subscribers (124733) | Created at 2008-08-16 - 06:54:02UTC | For questions and comments about the Plex Media Server. The Plex Media Server is smart software that makes playing Movies, TV Shows and other media on your computer simple.
  • add r/ and u/ matching.
13:31 <@deathbybandaid> r/plex
13:31 <reddit-test> [REDDIT] https://www.reddit.com/r/plex | subscribers (124732) | Created at 2008-08-16 - 06:54:02UTC | For questions and comments about the Plex Media Server. The Plex Media Server is smart software that makes playing Movies, TV Shows and other media on your computer simple.
13:31 <@deathbybandaid> u/plex
13:31 <reddit-test> [REDDITOR] plex | https://reddit.com/u/PleX | Link: 1 | Comment: 0

@deathbybandaid
Copy link
Contributor Author

deathbybandaid commented Oct 25, 2019

I mainly wanted to introduce some of this layer in this PR and possibly consider using a separate PR to add a subcommand layer for subreddits.

The other layer would be the basics of this:

    if subcommand == 'new':
        submissions = subreddit.new(limit=1)
    elif subcommand == 'top':
        submissions = subreddit.top(limit=1)
    elif subcommand == 'hot':
        submissions = subreddit.hot(limit=1)
    elif subcommand == 'controversial':
        submissions = subreddit.controversial(limit=1)
    elif subcommand == 'gilded':
        submissions = subreddit.gilded(limit=1)
    elif subcommand == 'rising':
        submissions = subreddit.rising(limit=1)
    elif subcommand == 'random':
        submissions = subreddit.hot(limit=50)

Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice little feature. My requested changes are mostly cosmetic or minor.

sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
@Exirel Exirel added this to the 7.0.0 milestone Oct 26, 2019
@deathbybandaid deathbybandaid force-pushed the reddit.update branch 2 times, most recently from 9d9d3c8 to 330d364 Compare October 28, 2019 18:55
@deathbybandaid
Copy link
Contributor Author

I have another tweak that I want to add to this, sometimes a subreddit being banned/locked throws a

403 locked

404 banned

@deathbybandaid deathbybandaid force-pushed the reddit.update branch 2 times, most recently from 330d364 to bfab822 Compare October 28, 2019 19:22
@deathbybandaid
Copy link
Contributor Author

here's the new tweak:

15:23 <@deathbybandaid> r/DarkNetMarkets
15:23 <reddit-test> r/DarkNetMarkets appears to be a banned subreddit!
15:23 <@deathbybandaid> r/sopel
15:23 <reddit-test> r/sopel appears to be a private subreddit!

Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good to me!

@deathbybandaid deathbybandaid force-pushed the reddit.update branch 2 times, most recently from 2b39647 to 430dac8 Compare November 20, 2019 16:12
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't had a chance to test this, but the concept is 👍! There are some issues, though. Not sure if they were present before or were introduced in the rebase you just had to do on account of other reddit stuff getting merged.

sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
@deathbybandaid
Copy link
Contributor Author

All the requested stuff is changed, and pushed,,, I even managed to find a bug that I was able to kill.
(between 3 PRs to a single file, there was bound to be an issue)

Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aside from the specific regex suggestion below, we talked a bit on IRC about whether to specifically handle r/all, which "exists" but isn't a normal subreddit, and therefore the code here will incorrectly report it as "banned". I think we'd never falsely report that state in a perfect world, but here in reality we have to decide if it's worth the potential code smell to handle that special case.

Keeping this as just a neutral "Comment" review, because it's only suggestions and not merge-blockers.

@@ -328,3 +378,37 @@ def get_channel_spoiler_free(bot, trigger):
bot.say('%s is flagged as spoiler-free' % channel)
else:
bot.say('%s is flagged as spoilers-allowed' % channel)


@rule(r'^(?P<prefix>r|u)/(?P<id>[a-zA-Z0-9-_]+)')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played with some stuff and came up with (what I think is) an even better pattern.

Suggested change
@rule(r'^(?P<prefix>r|u)/(?P<id>[a-zA-Z0-9-_]+)')
@rule(r'.*\b(?P<prefix>r|u)/(?P<id>[a-zA-Z0-9-_]+)\b.*')

This will match a user or subreddit reference anywhere in the line, instead of only at the beginning, and properly handles things like r/sopel's moderators or u/deathbybandaid'll get ya!.

It only handles the last reference in the line, though. That's a known issue with the rule decorator, which we'll probably work on for 7.1 (see #1757).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your proposed regex is giving me undesired double results, and not even from the "u/" and "r/", but full URL matching.

image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would, though like suggestions for regex that will help me accomplish my second comment on this PR (for my next PR), which would allow users to type "r/plex hot" and get the first item in the sorting "hot"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed more in #1722 (comment) but here's an improved pattern I came up with for this:

(?i)(?<!\S)\/?(?P<prefix>r|u)\/(?P<id>[a-zA-Z0-9-_]+)\b

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Sopel itself, we don't need the flag ((?i)), but we do need to wrap the whole pattern in .* for it to work.

@rule(r'.*(?<!\S)/?(?P<prefix>r|u)/(?P<id>[a-zA-Z0-9-_]+)\b.*')

@deathbybandaid
Copy link
Contributor Author

deathbybandaid commented Nov 21, 2019

"r/all" now displays as

image

As per a header I found via google. (better than nothing)

@dgw dgw changed the title reddit: treatment for subreddits, add additional regex reddit: handle subreddits, and u/ & r/ references Nov 21, 2019
@dgw
Copy link
Member

dgw commented Nov 21, 2019

Right, so it's not just r/all as the only "special" subreddit. There's also r/popular. Instead of spending time bikeshedding how to handle those now, let's defer any special handling of those to Sopel 7.1.

The only thing (I think) we should worry about in this PR before then is the regex to match the in-text r/ and u/ references. I've played with it a bit just now and come up with this pattern to match just about anything (subject to the limitations of @rule discussed previously):

(?i)(?<!\S)\/?(?P<prefix>r|u)\/(?P<id>[a-zA-Z0-9-_]+)\b

This pattern:

  • is case-insensitive
  • matches at the start of a line, or in the middle
  • does not match inside a URL
  • supports both with leading / and without

I think that's everything! Bunch of test cases at https://regexr.com/4p8dh.

One last thing: I retitled the PR to more accurately reflect what it's doing. If you could update your commit message to match, that'd be 💯% awesome!

@deathbybandaid
Copy link
Contributor Author

deathbybandaid commented Nov 22, 2019

I'm stashing what I had for "all" and "popular" for a later PR as per @dgw

If I get hit by a bus,, the below is what i had planned.

    if match.lower() in ['all', 'popular']:
        message = ('[REDDIT] {link}{nsfw} | {public_description}')
        nsfw = ' ' + bold(color('[Possible NSFW]', colors.ORANGE))
        link = "https://reddit.com/r/" + match.lower()
        public_description = ''
        if match.lower() == 'all':
            public_description = ('Today\'s top content from hundreds of '
                                  'thousands of Reddit communities.')
        elif match.lower() == 'popular':
            public_description = ('The top trending content from some of '
                                  'Reddit\'s most popular communities')
        message = message.format(
            link=link, nsfw=nsfw, public_description=public_description)
        bot.say(message)
        return NOLIMIT

sopel/modules/reddit.py Outdated Show resolved Hide resolved
sopel/modules/reddit.py Outdated Show resolved Hide resolved
@dgw
Copy link
Member

dgw commented Nov 22, 2019

Somehow, processing links appears to be broken in the current version of this PR (all I changed locally was the regex for slash-refs). Ran out of time to debug it right now, so I'm leaving the note as a reminder for one of us to continue that.

@deathbybandaid
Copy link
Contributor Author

I made the suggestions,, and I now see what you mean about links not working too

@deathbybandaid
Copy link
Contributor Author

However, this works (partly)
image

@dgw
Copy link
Member

dgw commented Nov 23, 2019

@deathbybandaid See #1760 about the link-handling problem. It's not related to this. Please try out this PR with my fixup (just pushed), and squash + reword the commit message to match the PR title if you're happy with it. (I'd appreciate a co-author credit, if you would add one for me during the squash. I've spent so much time on this this week! 😹)

@deathbybandaid deathbybandaid force-pushed the reddit.update branch 2 times, most recently from 1ed7d45 to 282ebe7 Compare November 23, 2019 18:55
Co-authored-by: dgw <dgw@technobabbl.es>
Copy link
Member

@dgw dgw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At long last, we've managed to coerce this PR into doing exactly what we want (mostly by playing with regex a lot). Some more tweaks and a regression fix or two (unrelated to these changes) will come from #1760 after this is merged.

Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't check the regex. It looks good, but I haven't checked the rules for reddit's URLs. So, I'd say I trust you. :)

LGTM!

@dgw dgw merged commit 52d735a into sopel-irc:master Nov 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants