PRAW: can you receive data from two seperate keyword lists in the same message?












0















I currently have a reddit bot that receives keywords from a subreddit and then sends me a notification on Slack.



The current code for sending me the notification is



for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
already_alerted_submissions.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}


So it is currently getting a list of keywords from



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive


I was wondering if it was possible to have two separate keyword lists in the file such as the following



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive
keywords_color = ['red', 'blue', 'green', 'black'] # case insensitive


And if the word "camera" was detected in a thread, it would post a message just like it does currently.



But if in the same comment, it detected a keyword from both
keywords AND and keywords_color



It could post the same message in slack but with another line saying something similar to "Color was detected"



So in the examples above the message in slack would look like the following.



1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is awesome


Or if it detected both "keywords" and "keywords_color" it would look like the following



2. 1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is a RED one and its awesome
**Colour was detected**


Would this be possible? any help would be appreciated!



The full scrip for the file is here:



def main():

alerted_comments = get_list_from_pickle('alerted_comments.pickle')

try:

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.id)

while len(alerted_comments) > 100:
del alerted_comments[0]

with open('alerted_comments.pickle', 'wb') as fp:
pickle.dump(alerted_comments, fp)

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post('https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78',
data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError('Request to slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

except Exception as e:
print('There was an error: ' + str(e))
sleep(60) # wait for 60 seconds before restarting
main()

if __name__ == '__main__':
main()









share|improve this question

























  • We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

    – Cyphase
    Nov 21 '18 at 8:12











  • Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

    – Vestipial
    Nov 21 '18 at 8:18











  • I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

    – Cyphase
    Nov 21 '18 at 8:19











  • Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

    – Vestipial
    Nov 21 '18 at 8:22











  • Its not a massive issue, since the keywords mostly will not be as generic as that.

    – Vestipial
    Nov 21 '18 at 8:23
















0















I currently have a reddit bot that receives keywords from a subreddit and then sends me a notification on Slack.



The current code for sending me the notification is



for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
already_alerted_submissions.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}


So it is currently getting a list of keywords from



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive


I was wondering if it was possible to have two separate keyword lists in the file such as the following



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive
keywords_color = ['red', 'blue', 'green', 'black'] # case insensitive


And if the word "camera" was detected in a thread, it would post a message just like it does currently.



But if in the same comment, it detected a keyword from both
keywords AND and keywords_color



It could post the same message in slack but with another line saying something similar to "Color was detected"



So in the examples above the message in slack would look like the following.



1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is awesome


Or if it detected both "keywords" and "keywords_color" it would look like the following



2. 1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is a RED one and its awesome
**Colour was detected**


Would this be possible? any help would be appreciated!



The full scrip for the file is here:



def main():

alerted_comments = get_list_from_pickle('alerted_comments.pickle')

try:

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.id)

while len(alerted_comments) > 100:
del alerted_comments[0]

with open('alerted_comments.pickle', 'wb') as fp:
pickle.dump(alerted_comments, fp)

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post('https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78',
data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError('Request to slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

except Exception as e:
print('There was an error: ' + str(e))
sleep(60) # wait for 60 seconds before restarting
main()

if __name__ == '__main__':
main()









share|improve this question

























  • We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

    – Cyphase
    Nov 21 '18 at 8:12











  • Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

    – Vestipial
    Nov 21 '18 at 8:18











  • I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

    – Cyphase
    Nov 21 '18 at 8:19











  • Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

    – Vestipial
    Nov 21 '18 at 8:22











  • Its not a massive issue, since the keywords mostly will not be as generic as that.

    – Vestipial
    Nov 21 '18 at 8:23














0












0








0








I currently have a reddit bot that receives keywords from a subreddit and then sends me a notification on Slack.



The current code for sending me the notification is



for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
already_alerted_submissions.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}


So it is currently getting a list of keywords from



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive


I was wondering if it was possible to have two separate keyword lists in the file such as the following



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive
keywords_color = ['red', 'blue', 'green', 'black'] # case insensitive


And if the word "camera" was detected in a thread, it would post a message just like it does currently.



But if in the same comment, it detected a keyword from both
keywords AND and keywords_color



It could post the same message in slack but with another line saying something similar to "Color was detected"



So in the examples above the message in slack would look like the following.



1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is awesome


Or if it detected both "keywords" and "keywords_color" it would look like the following



2. 1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is a RED one and its awesome
**Colour was detected**


Would this be possible? any help would be appreciated!



The full scrip for the file is here:



def main():

alerted_comments = get_list_from_pickle('alerted_comments.pickle')

try:

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.id)

while len(alerted_comments) > 100:
del alerted_comments[0]

with open('alerted_comments.pickle', 'wb') as fp:
pickle.dump(alerted_comments, fp)

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post('https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78',
data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError('Request to slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

except Exception as e:
print('There was an error: ' + str(e))
sleep(60) # wait for 60 seconds before restarting
main()

if __name__ == '__main__':
main()









share|improve this question
















I currently have a reddit bot that receives keywords from a subreddit and then sends me a notification on Slack.



The current code for sending me the notification is



for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
already_alerted_submissions.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}


So it is currently getting a list of keywords from



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive


I was wondering if it was possible to have two separate keyword lists in the file such as the following



keywords = ['camera', 'nikon', 'canon', 'campus']  # case insensitive
keywords_color = ['red', 'blue', 'green', 'black'] # case insensitive


And if the word "camera" was detected in a thread, it would post a message just like it does currently.



But if in the same comment, it detected a keyword from both
keywords AND and keywords_color



It could post the same message in slack but with another line saying something similar to "Color was detected"



So in the examples above the message in slack would look like the following.



1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is awesome


Or if it detected both "keywords" and "keywords_color" it would look like the following



2. 1. [Keyword *camera* detected]
(http://www.reddit.com/r/camera/comments/9yg8mt/goodcameras
I just got a great CAMERA today, it is a RED one and its awesome
**Colour was detected**


Would this be possible? any help would be appreciated!



The full scrip for the file is here:



def main():

alerted_comments = get_list_from_pickle('alerted_comments.pickle')

try:

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.id)

while len(alerted_comments) > 100:
del alerted_comments[0]

with open('alerted_comments.pickle', 'wb') as fp:
pickle.dump(alerted_comments, fp)

for kw in keywords:
if kw.lower() in comment.body.lower(): # case insensitive check
alerted_comments.append(comment.submission.id)
msg = '[Keyword *{0}* detected](http://www.reddit.com{1})'.format(
kw, comment.permalink)
slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post('https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78',
data=json.dumps(slack_data), headers={'Content-Type': 'application/json'})
if response.status_code != 200:
raise ValueError('Request to slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

except Exception as e:
print('There was an error: ' + str(e))
sleep(60) # wait for 60 seconds before restarting
main()

if __name__ == '__main__':
main()






python python-3.x






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 13:20







Vestipial

















asked Nov 20 '18 at 1:58









VestipialVestipial

86




86













  • We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

    – Cyphase
    Nov 21 '18 at 8:12











  • Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

    – Vestipial
    Nov 21 '18 at 8:18











  • I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

    – Cyphase
    Nov 21 '18 at 8:19











  • Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

    – Vestipial
    Nov 21 '18 at 8:22











  • Its not a massive issue, since the keywords mostly will not be as generic as that.

    – Vestipial
    Nov 21 '18 at 8:23



















  • We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

    – Cyphase
    Nov 21 '18 at 8:12











  • Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

    – Vestipial
    Nov 21 '18 at 8:18











  • I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

    – Cyphase
    Nov 21 '18 at 8:19











  • Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

    – Vestipial
    Nov 21 '18 at 8:22











  • Its not a massive issue, since the keywords mostly will not be as generic as that.

    – Vestipial
    Nov 21 '18 at 8:23

















We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

– Cyphase
Nov 21 '18 at 8:12





We were chatting about this in the Python IRC channel (#python on FreeNode), but I'm commenting here for the record. @Vestipial, I'm going to rewrite this as you asked. Also, what do you want it to do if multiple keywords match?

– Cyphase
Nov 21 '18 at 8:12













Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

– Vestipial
Nov 21 '18 at 8:18





Hi mate, thank you so much, I really do appreciate it! So currently, if it detects a word from the keyword list, it sends a notification with the following: (On 3 separate lines) 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. So what I was hoping for, if it found a keyword from both keyword lists. Instead of doing the above, it would do the following: 1.Keyword camera Detected 2.Then the permalink 3. Then the comment the keyword was found in. 4. 'Colour was detected' Or whatever text I change that 4th line to. Thanks!

– Vestipial
Nov 21 '18 at 8:18













I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

– Cyphase
Nov 21 '18 at 8:19





I mean, what if multiple keywords are found, e.g' camera' and 'nikon'?

– Cyphase
Nov 21 '18 at 8:19













Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

– Vestipial
Nov 21 '18 at 8:22





Ahh, I get it. If multiple keywords are found, it can just do as it does currently and post it twice. Unless you have a better idea

– Vestipial
Nov 21 '18 at 8:22













Its not a massive issue, since the keywords mostly will not be as generic as that.

– Vestipial
Nov 21 '18 at 8:23





Its not a massive issue, since the keywords mostly will not be as generic as that.

– Vestipial
Nov 21 '18 at 8:23












1 Answer
1






active

oldest

votes


















0














This is untested, but here you go:



import json
import time

import requests


class SlackError(Exception):
# This isn't quite how I would handle this, so for now it's
# just a hint of more to learn.
pass


MSG_TEMPLATE = """[Keyword *{keyword}* detected]
(https://www.reddit.com{permalink})
{comment_body}"""

SLACK_WEBHOOK = 'https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78'


def main(*, save_path):
keywords = ...
color_keywords = ...
ignore_users = ...

comment_stream = ...

with open(save_path, 'r') as fp:
alerted_comments = json.load(fp)

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

if any(kw.lower() in comment.body.lower() for kw in keywords):
found_kws = [kw for kw in keywords if kw.lower() in comment.body.lower()]

msg = MSG_TEMPLATE.format(
keyword=found_kws[0],
permalink=permalink,
comment_body=comment.body
)

if any(kw.lower() in comment.body.lower() for kw in color_keywords):
msg += "nColour was detected"

slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post(
SLACK_WEBHOOK,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
# Moving this here so you don't miss a comment
# because of an error. It does mean other errors
# could potentially cause a repeat message. There
# are ways to handle that.

alerted_comments.append(comment.id)

if len(alerted_comments) > 100:
alerted_comments = alerted_comments[-100:]

with open(save_path, 'w') as fp:
json.dump(alerted_comments, fp)
else:
# You'll probably want to be more discerning than "not 200",
# but that's fine for now.
raise SlackError(
'Request to Slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

if __name__ == '__main__':
while True:
try:
main(save_path='alerted_comments.json')
except Exception as e:
print('There was an error: {}'.format(str(e)))
time.sleep(60) # wait for 60 seconds before restarting


Let me know if you have any questions.






share|improve this answer
























  • Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

    – Vestipial
    Nov 21 '18 at 9:48











  • @Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

    – Cyphase
    Nov 21 '18 at 9:57











  • Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

    – Vestipial
    Nov 21 '18 at 10:26











  • raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

    – Vestipial
    Nov 21 '18 at 10:27











  • Did you copy the semi-colon with the brackets? It's not ;.

    – Cyphase
    Nov 21 '18 at 10:45













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385167%2fpraw-can-you-receive-data-from-two-seperate-keyword-lists-in-the-same-message%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














This is untested, but here you go:



import json
import time

import requests


class SlackError(Exception):
# This isn't quite how I would handle this, so for now it's
# just a hint of more to learn.
pass


MSG_TEMPLATE = """[Keyword *{keyword}* detected]
(https://www.reddit.com{permalink})
{comment_body}"""

SLACK_WEBHOOK = 'https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78'


def main(*, save_path):
keywords = ...
color_keywords = ...
ignore_users = ...

comment_stream = ...

with open(save_path, 'r') as fp:
alerted_comments = json.load(fp)

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

if any(kw.lower() in comment.body.lower() for kw in keywords):
found_kws = [kw for kw in keywords if kw.lower() in comment.body.lower()]

msg = MSG_TEMPLATE.format(
keyword=found_kws[0],
permalink=permalink,
comment_body=comment.body
)

if any(kw.lower() in comment.body.lower() for kw in color_keywords):
msg += "nColour was detected"

slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post(
SLACK_WEBHOOK,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
# Moving this here so you don't miss a comment
# because of an error. It does mean other errors
# could potentially cause a repeat message. There
# are ways to handle that.

alerted_comments.append(comment.id)

if len(alerted_comments) > 100:
alerted_comments = alerted_comments[-100:]

with open(save_path, 'w') as fp:
json.dump(alerted_comments, fp)
else:
# You'll probably want to be more discerning than "not 200",
# but that's fine for now.
raise SlackError(
'Request to Slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

if __name__ == '__main__':
while True:
try:
main(save_path='alerted_comments.json')
except Exception as e:
print('There was an error: {}'.format(str(e)))
time.sleep(60) # wait for 60 seconds before restarting


Let me know if you have any questions.






share|improve this answer
























  • Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

    – Vestipial
    Nov 21 '18 at 9:48











  • @Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

    – Cyphase
    Nov 21 '18 at 9:57











  • Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

    – Vestipial
    Nov 21 '18 at 10:26











  • raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

    – Vestipial
    Nov 21 '18 at 10:27











  • Did you copy the semi-colon with the brackets? It's not ;.

    – Cyphase
    Nov 21 '18 at 10:45


















0














This is untested, but here you go:



import json
import time

import requests


class SlackError(Exception):
# This isn't quite how I would handle this, so for now it's
# just a hint of more to learn.
pass


MSG_TEMPLATE = """[Keyword *{keyword}* detected]
(https://www.reddit.com{permalink})
{comment_body}"""

SLACK_WEBHOOK = 'https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78'


def main(*, save_path):
keywords = ...
color_keywords = ...
ignore_users = ...

comment_stream = ...

with open(save_path, 'r') as fp:
alerted_comments = json.load(fp)

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

if any(kw.lower() in comment.body.lower() for kw in keywords):
found_kws = [kw for kw in keywords if kw.lower() in comment.body.lower()]

msg = MSG_TEMPLATE.format(
keyword=found_kws[0],
permalink=permalink,
comment_body=comment.body
)

if any(kw.lower() in comment.body.lower() for kw in color_keywords):
msg += "nColour was detected"

slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post(
SLACK_WEBHOOK,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
# Moving this here so you don't miss a comment
# because of an error. It does mean other errors
# could potentially cause a repeat message. There
# are ways to handle that.

alerted_comments.append(comment.id)

if len(alerted_comments) > 100:
alerted_comments = alerted_comments[-100:]

with open(save_path, 'w') as fp:
json.dump(alerted_comments, fp)
else:
# You'll probably want to be more discerning than "not 200",
# but that's fine for now.
raise SlackError(
'Request to Slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

if __name__ == '__main__':
while True:
try:
main(save_path='alerted_comments.json')
except Exception as e:
print('There was an error: {}'.format(str(e)))
time.sleep(60) # wait for 60 seconds before restarting


Let me know if you have any questions.






share|improve this answer
























  • Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

    – Vestipial
    Nov 21 '18 at 9:48











  • @Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

    – Cyphase
    Nov 21 '18 at 9:57











  • Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

    – Vestipial
    Nov 21 '18 at 10:26











  • raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

    – Vestipial
    Nov 21 '18 at 10:27











  • Did you copy the semi-colon with the brackets? It's not ;.

    – Cyphase
    Nov 21 '18 at 10:45
















0












0








0







This is untested, but here you go:



import json
import time

import requests


class SlackError(Exception):
# This isn't quite how I would handle this, so for now it's
# just a hint of more to learn.
pass


MSG_TEMPLATE = """[Keyword *{keyword}* detected]
(https://www.reddit.com{permalink})
{comment_body}"""

SLACK_WEBHOOK = 'https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78'


def main(*, save_path):
keywords = ...
color_keywords = ...
ignore_users = ...

comment_stream = ...

with open(save_path, 'r') as fp:
alerted_comments = json.load(fp)

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

if any(kw.lower() in comment.body.lower() for kw in keywords):
found_kws = [kw for kw in keywords if kw.lower() in comment.body.lower()]

msg = MSG_TEMPLATE.format(
keyword=found_kws[0],
permalink=permalink,
comment_body=comment.body
)

if any(kw.lower() in comment.body.lower() for kw in color_keywords):
msg += "nColour was detected"

slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post(
SLACK_WEBHOOK,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
# Moving this here so you don't miss a comment
# because of an error. It does mean other errors
# could potentially cause a repeat message. There
# are ways to handle that.

alerted_comments.append(comment.id)

if len(alerted_comments) > 100:
alerted_comments = alerted_comments[-100:]

with open(save_path, 'w') as fp:
json.dump(alerted_comments, fp)
else:
# You'll probably want to be more discerning than "not 200",
# but that's fine for now.
raise SlackError(
'Request to Slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

if __name__ == '__main__':
while True:
try:
main(save_path='alerted_comments.json')
except Exception as e:
print('There was an error: {}'.format(str(e)))
time.sleep(60) # wait for 60 seconds before restarting


Let me know if you have any questions.






share|improve this answer













This is untested, but here you go:



import json
import time

import requests


class SlackError(Exception):
# This isn't quite how I would handle this, so for now it's
# just a hint of more to learn.
pass


MSG_TEMPLATE = """[Keyword *{keyword}* detected]
(https://www.reddit.com{permalink})
{comment_body}"""

SLACK_WEBHOOK = 'https://hooks.slack.com/services/BE72P09A9/xxxxxxxxx78'


def main(*, save_path):
keywords = ...
color_keywords = ...
ignore_users = ...

comment_stream = ...

with open(save_path, 'r') as fp:
alerted_comments = json.load(fp)

for comment in comment_stream:
if comment.id in alerted_comments:
continue

if comment.author: # if comment author hasn't deleted
if comment.author.name in ignore_users:
continue

if any(kw.lower() in comment.body.lower() for kw in keywords):
found_kws = [kw for kw in keywords if kw.lower() in comment.body.lower()]

msg = MSG_TEMPLATE.format(
keyword=found_kws[0],
permalink=permalink,
comment_body=comment.body
)

if any(kw.lower() in comment.body.lower() for kw in color_keywords):
msg += "nColour was detected"

slack_data = {'text': msg, 'mrkdwn': True}

response = requests.post(
SLACK_WEBHOOK,
data=json.dumps(slack_data),
headers={'Content-Type': 'application/json'}
)

if response.status_code == 200:
# Moving this here so you don't miss a comment
# because of an error. It does mean other errors
# could potentially cause a repeat message. There
# are ways to handle that.

alerted_comments.append(comment.id)

if len(alerted_comments) > 100:
alerted_comments = alerted_comments[-100:]

with open(save_path, 'w') as fp:
json.dump(alerted_comments, fp)
else:
# You'll probably want to be more discerning than "not 200",
# but that's fine for now.
raise SlackError(
'Request to Slack returned an error %s, the response is:n%s' % (
response.status_code, response.text))

if __name__ == '__main__':
while True:
try:
main(save_path='alerted_comments.json')
except Exception as e:
print('There was an error: {}'.format(str(e)))
time.sleep(60) # wait for 60 seconds before restarting


Let me know if you have any questions.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 21 '18 at 9:22









CyphaseCyphase

8,35011627




8,35011627













  • Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

    – Vestipial
    Nov 21 '18 at 9:48











  • @Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

    – Cyphase
    Nov 21 '18 at 9:57











  • Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

    – Vestipial
    Nov 21 '18 at 10:26











  • raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

    – Vestipial
    Nov 21 '18 at 10:27











  • Did you copy the semi-colon with the brackets? It's not ;.

    – Cyphase
    Nov 21 '18 at 10:45





















  • Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

    – Vestipial
    Nov 21 '18 at 9:48











  • @Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

    – Cyphase
    Nov 21 '18 at 9:57











  • Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

    – Vestipial
    Nov 21 '18 at 10:26











  • raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

    – Vestipial
    Nov 21 '18 at 10:27











  • Did you copy the semi-colon with the brackets? It's not ;.

    – Cyphase
    Nov 21 '18 at 10:45



















Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

– Vestipial
Nov 21 '18 at 9:48





Thank you so much for doing that, wow, you rewrote the whole thing. thats much more than you needed to do! but thank you. So i guess to add my reddit praw login details I would import praw, then add the login details.. the script I got it updated from was here. pastebin.com/d8akemm6 So would I just add those top details as well the keywords and subreddit details? I totally missed adding that in my SO post I just noticed. I did add them and tried to run the script, but initially got a There was an error: [Errno 2] No such file or directory: 'alerted_comments.json' error

– Vestipial
Nov 21 '18 at 9:48













@Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

– Cyphase
Nov 21 '18 at 9:57





@Vestipial That's the gist of it; I don't know PRAW offhand. As for that error, the code is assuming alerted_comments.json already exists. You can fix that, or for now, you can just create it containing just ; an empty list of comments.

– Cyphase
Nov 21 '18 at 9:57













Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

– Vestipial
Nov 21 '18 at 10:26





Ahh ok, cool. I have done that. now I am getting There was an error: Extra data: line 1 column 3 (char 2)

– Vestipial
Nov 21 '18 at 10:26













raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

– Vestipial
Nov 21 '18 at 10:27





raise JSONDecodeError("Extra data", s, end) json.decoder.JSONDecodeError: Extra data: line 1 column 3 (char 2) During handling of the above exception, another exception occurred: I am guessing I need to do something else apart from "import praw" and those reddit details, ill have to have a sift through, maybe I need to rename some things to match your code

– Vestipial
Nov 21 '18 at 10:27













Did you copy the semi-colon with the brackets? It's not ;.

– Cyphase
Nov 21 '18 at 10:45







Did you copy the semi-colon with the brackets? It's not ;.

– Cyphase
Nov 21 '18 at 10:45






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53385167%2fpraw-can-you-receive-data-from-two-seperate-keyword-lists-in-the-same-message%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)