Skip to content

Instantly share code, notes, and snippets.

@matael
Created February 23, 2013 17:57
Show Gist options
  • Select an option

  • Save matael/5020665 to your computer and use it in GitHub Desktop.

Select an option

Save matael/5020665 to your computer and use it in GitHub Desktop.
Simple way to know if you have new messages on reddit
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from urllib.request import Request, build_opener, HTTPCookieProcessor, HTTPHandler
from urllib.parse import urlencode
from http.cookiejar import CookieJar
import json
url = "http://www.reddit.com/"
data = bytes(urlencode({
'user': 'login',
'rem': False,
'passwd': 'passwd'
}),'utf8')
#Create a CookieJar object to hold the cookies
cj = CookieJar()
#Create an opener to open pages using the http protocol and to process cookies.
opener = build_opener(HTTPCookieProcessor(cj), HTTPHandler())
#create a request object to be used to get the page.
req = Request(url+"api/login",data)
opener.open(req)
req = Request(url+"message/unread.json")
result = opener.open(req).read()
messages = json.loads(result.decode('utf8'))
messages = messages['data']['children']
if len(messages):
print("Nouveaux messages")
else:
print("Pas de nouveau messages")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment