-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0cc5748
Showing
3 changed files
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
config.json | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
const reddit = require('concierge/reddit'); | ||
|
||
class FreedarModule { | ||
notify(title, url) { | ||
const apis = this.platform.getIntegrationApis(); | ||
for (let int in this.config.listeners) { | ||
const api = apis[int]; | ||
for (let thread of this.config.listeners[int]) { | ||
LOG.debug(`Notifying "${int}:${thread}" about a free game.`); | ||
api.sendMessage(title, thread); | ||
api.sendUrl(url, thread); | ||
} | ||
} | ||
} | ||
|
||
async checkForGames() { | ||
LOG.debug('Checking for free games.'); | ||
const results = await reddit('gamedeals', 200); | ||
const searchTerms = [/[^0-9]100%/i, /free($|[^a-z])/i]; | ||
const ignoreTerms = [/\[PSN\]/i, /XBOX/i, /playstation/i, /free (weekend|play|shipping)/i, /[a-z](-)?free/i, /rent/i]; | ||
for (let result of results) { | ||
LOG.silly(`Checking title "${result.data.title}".`); | ||
const includeCheck = searchTerms.some(s => s.test(result.data.title)); | ||
const excludeCheck = ignoreTerms.some(s => s.test(result.data.title)); | ||
if (includeCheck && !excludeCheck) { | ||
if (this.config.notified[result.data.id]) { | ||
LOG.silly('Title already matched.'); | ||
} | ||
else { | ||
LOG.silly('Title was a match!'); | ||
this.notify(result.data.title, result.data.url); | ||
this.config.notified[result.data.id] = true; | ||
} | ||
} | ||
} | ||
this.timer = setTimeout(this.checkForGames.bind(this), this.config.checkFrequency); | ||
} | ||
|
||
load() { | ||
const defaultConfig = { | ||
notified: {}, | ||
listeners: {}, | ||
checkFrequency: 3600000 | ||
}; | ||
for (let key in defaultConfig) { | ||
if (!this.config[key]) { | ||
this.config[key] = defaultConfig[key]; | ||
} | ||
} | ||
this.checkForGames(); | ||
} | ||
|
||
unload() { | ||
clearTimeout(this.timer); | ||
} | ||
|
||
run(api, event) { | ||
ensure(this.config.listeners, event.event_source, []); | ||
if (!this.config.listeners[event.event_source].includes(event.thread_id)) { | ||
this.config.listeners[event.event_source].push(event.thread_id); | ||
api.sendMessage('Listening for free games.', event.thread_id); | ||
} | ||
else { | ||
this.config.listeners[event.event_source].splice(this.config.listeners[event.event_source].indexOf(event.thread_id), 1); | ||
api.sendMessage('No longer listening for free games.', event.thread_id); | ||
} | ||
} | ||
} | ||
|
||
module.exports = new FreedarModule(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"name": "freedar", | ||
"version": "1.0.0", | ||
"startup": "freedar.js", | ||
"command": "freedar", | ||
"help": [ | ||
["{{commandPrefix}}freedar", "Subscribe/unsubscribe to free game notifications."] | ||
] | ||
} |