-
Notifications
You must be signed in to change notification settings - Fork 23
Added !reddit command. #148
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ public class QuotesAndFacts | |
| // update cache when new quotes/facts added | ||
| private Map<String, Integer> dumpSize = new HashMap<>(); | ||
|
|
||
| private static final String PASTEBIN_API_URL = Vilebot.getConfig().get("pastebinApiUrl"); | ||
| private static final String PASTEBIN_API_URL = Vilebot.getConfig().get( "pastebinApiUrl" ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1 for fixing this formatting, never got around to doing it.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| @Override | ||
| public void onJoin( final JoinEvent event ) // announce fact or quote on join | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| /** | ||
| * Copyright (C) 2019 Oldterns | ||
| * | ||
| * This file may be modified and distributed under the terms | ||
| * of the MIT license. See the LICENSE file for details. | ||
| */ | ||
|
|
||
| package com.oldterns.vilebot.handlers.user; | ||
|
|
||
| import java.net.MalformedURLException; | ||
| import java.net.URL; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| import com.oldterns.vilebot.Vilebot; | ||
| import com.oldterns.vilebot.util.LimitCommand; | ||
| import com.oldterns.vilebot.util.NewsParser; | ||
| import org.apache.commons.lang3.tuple.ImmutablePair; | ||
| import org.pircbotx.hooks.types.GenericMessageEvent; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class Reddit | ||
| extends NewsParser | ||
| { | ||
| private static final Logger logger = LoggerFactory.getLogger( Reddit.class ); | ||
|
|
||
| private static final Pattern REDDIT_PATTERN = Pattern.compile( "^!reddit(?: ([a-zA-Z]+)|)" ); | ||
|
|
||
| private static final Pattern REDDIT_HELP_PATTERN = Pattern.compile( "^!reddit help" ); | ||
|
|
||
| private final String HELP_MESSAGE = generateHelpMessage(); | ||
|
|
||
| private final String HELP_COMMAND = "'!reddit help'"; | ||
|
|
||
| public static LimitCommand limitCommand = new LimitCommand(); | ||
|
|
||
| private static final String RESTRICTED_CHANNEL = Vilebot.getConfig().get( "ircChannel1" ); | ||
|
|
||
| @Override | ||
| public void onGenericMessage( final GenericMessageEvent event ) | ||
| { | ||
| String text = event.getMessage(); | ||
| Matcher matcher = REDDIT_PATTERN.matcher( text ); | ||
| Matcher helpMatcher = REDDIT_HELP_PATTERN.matcher( text ); | ||
|
|
||
| if ( helpMatcher.matches() ) | ||
| { | ||
| for ( String line : HELP_MESSAGE.split( "\n" ) ) | ||
| { | ||
| event.respondPrivateMessage( line ); | ||
| } | ||
| } | ||
| else if ( matcher.matches() ) | ||
| { | ||
| Map<String, ImmutablePair<String, URL>> redditRSSMap = new HashMap<>(); | ||
| String subreddit = matcher.group( 1 ) != null ? matcher.group( 1 ) : ""; | ||
| try | ||
| { | ||
| redditRSSMap.put( subreddit, | ||
| new ImmutablePair<String, URL>( "Subredit", new URL( new StringBuilder( | ||
| "https://reddit.com/" ).append( subreddit.isEmpty() ? subreddit : "r/" + subreddit + "/" ).append( ".rss" ).toString() ) ) ); | ||
| currentNews( event, matcher, redditRSSMap, subreddit, HELP_COMMAND, limitCommand, RESTRICTED_CHANNEL, | ||
| logger ); | ||
| } | ||
| catch ( MalformedURLException e ) | ||
| { | ||
| event.respond( subreddit + " is not a subreddit." ); | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| @Override | ||
| protected String generateHelpMessage() | ||
| { | ||
| StringBuilder sb = new StringBuilder(); | ||
|
|
||
| sb.append( "Reddit (example: !reddit toronto)\n" ); | ||
|
|
||
| return sb.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,12 @@ | |
|
|
||
| package com.oldterns.vilebot.util; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.regex.Matcher; | ||
|
|
||
| import com.sun.syndication.feed.synd.SyndEntry; | ||
| import com.sun.syndication.feed.synd.SyndFeed; | ||
| import com.sun.syndication.io.FeedException; | ||
|
|
@@ -19,12 +25,6 @@ | |
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.IOException; | ||
| import java.net.URL; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.regex.Matcher; | ||
|
|
||
| public abstract class NewsParser | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now that this class is used for something other than news parsing, maybe rename it to something more generic like |
||
| extends ListenerAdapter | ||
| { | ||
|
|
@@ -33,9 +33,8 @@ public abstract class NewsParser | |
| protected static final int NUM_HEADLINES = 3; | ||
|
|
||
| protected void currentNews( GenericMessageEvent event, Matcher matcher, | ||
| LinkedHashMap<String, ImmutablePair<String, URL>> newsFeedsByCategory, | ||
| String defaultCategory, String helpCommand, LimitCommand limitCommand, | ||
| String restrictedChannel, Logger logger ) | ||
| Map<String, ImmutablePair<String, URL>> newsFeedsByCategory, String defaultCategory, | ||
| String helpCommand, LimitCommand limitCommand, String restrictedChannel, Logger logger ) | ||
| { | ||
| String category = matcher.group( 1 ); // The news category | ||
|
|
||
|
|
@@ -52,9 +51,8 @@ protected void currentNews( GenericMessageEvent event, Matcher matcher, | |
| } | ||
| } | ||
|
|
||
| protected void newsLimit( GenericMessageEvent event, | ||
| LinkedHashMap<String, ImmutablePair<String, URL>> newsFeedsByCategory, String category, | ||
| Logger logger, LimitCommand limitCommand, String restrictedChannel ) | ||
| protected void newsLimit( GenericMessageEvent event, Map<String, ImmutablePair<String, URL>> newsFeedsByCategory, | ||
| String category, Logger logger, LimitCommand limitCommand, String restrictedChannel ) | ||
| { | ||
| if ( event instanceof MessageEvent | ||
| && ( (MessageEvent) event ).getChannel().getName().equals( restrictedChannel ) ) | ||
|
|
@@ -76,8 +74,8 @@ protected void newsLimit( GenericMessageEvent event, | |
| } | ||
|
|
||
| protected void printHeadlines( GenericMessageEvent event, | ||
| LinkedHashMap<String, ImmutablePair<String, URL>> newsFeedsByCategory, | ||
| String category, Logger logger ) | ||
| Map<String, ImmutablePair<String, URL>> newsFeedsByCategory, String category, | ||
| Logger logger ) | ||
| { | ||
| SyndFeedInput input = new SyndFeedInput(); | ||
| SyndFeed feed = null; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Y'all, what is this line? Ends on column 1645?? At the very least break it up a little:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The VIleBot1 formatter does that. However, this task should be done in a more generic way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought it was a running joke tbh, which is why I never bothered to reformat it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Running jokes have no place in code, not even in joke code
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then the formatter is busted. Alternatively,