Skip to content

Instantly share code, notes, and snippets.

@angelworm
Last active December 14, 2015 08:18
Show Gist options
  • Save angelworm/5056447 to your computer and use it in GitHub Desktop.
Save angelworm/5056447 to your computer and use it in GitHub Desktop.
Mac OS 10.8 Notification For Tumblr
"""
Copyright (c) 2013 @Angelworm_
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import pync
import os
import mechanize
import urllib
import urllib2
import json
import cookielib
import logging
import time
import sys
def mkbr(email, pas):
br = mechanize.Browser(factory=mechanize.RobustFactory())
br.set_handle_robots(False)
cj = mechanize.LWPCookieJar()
br.set_cookiejar(cj)
br.set_handle_refresh(mechanize._http.HTTPRefreshProcessor(),
max_time=1)
br.open("https://www.tumblr.com/login")
br.select_form(nr=0)
br['user[email]'] = email
br['user[password]'] = pas
url, data, hdrs = br.form.click_request_data()
re = br.open("https://www.tumblr.com/login", data)
return br, cj
def fetch(cj, nf=0):
req = {
'notifications_next': 50000,
'magick': nf + 1,
'notifications': 'true',
'from': nf,
'unread': 'true',
'inbox': 'true'
}
opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj))
mechanize.install_opener(opener)
res = mechanize.urlopen("http://www.tumblr.com/svc/poll?%s"
% urllib.urlencode(req))
return json.loads(res.read())
def notification(cj):
js = fetch(cj)
while True:
js = fetch(cj, js[u'next_from'])
for i in js[u'notifications']:
title = " %s by %s" % (i['type'], i['from_tumblelog_name'])
pync.Notifier.notify(i[u'summary'], title=title)
time.sleep(2)
br, cj = mkbr("mail", "password")
notification(cj)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment