Skip to content

Instantly share code, notes, and snippets.

@idlethreat
Created March 28, 2015 18:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idlethreat/40f75359d67c7b0c17c7 to your computer and use it in GitHub Desktop.
Save idlethreat/40f75359d67c7b0c17c7 to your computer and use it in GitHub Desktop.
reddit-messages-to-graylog
#!/usr/bin/env python
import praw
from gelfclient import UdpClient
# ++ reddit items
# setting your username and password
myUsername = "yourusername" # your reddit username
myPassword = "yourpassword" # your reddit password
# ++ gelf items
myGraylogServer = 'ip_add_goes_here' # IP address of your graylog instance
def send_gelf_message(r_message_id,r_author,r_subject,r_body,r_link):
gelf_server = myGraylogServer
gelf = UdpClient(gelf_server, port=12201, mtu=8000, source='reddit messages')
data = {}
data['message_id'] = r_message_id
data['author'] = r_author
data['subject'] = r_subject
data['short_message'] = r_body
data['full_message'] = r_body
data['link'] = r_link
gelf.log(data)
r = praw.Reddit(user_agent='PythonPrawProject')
r.login(myUsername, myPassword)
unread = r.get_unread(limit=None)
for msg in unread:
message_id = msg.id
author = msg.author.name
subject = msg.subject
body = msg.body
message_link = "https://www.reddit.com/message/messages/" + message_id
# debugging messages. reenable if you need to see what is being received
# print "id: ", message_id
# print "author: ", author
# print "subject: ", subject
# print "body: ", body
# print "message link: ", message_link
print "Logging the contents of message " + message_id + " in graylog at " + myGraylogServer + " and marking as read."
send_gelf_message(message_id,author,subject,body,message_link)
# this will mark all messages as read in reddit, so should be called last at the end of pass
msg.mark_as_read()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment