-
-
Save atr000/150942 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# pyprowl.py - Post to Prowl (https://prowl.weks.net) from Python. | |
# | |
# Copyright (c) 2009 Zachary Voase | |
# | |
# 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 optparse | |
import urllib2 | |
from urlobject import URLObject | |
class ProwlEvent(object): | |
"""Class representing a single prowl event.""" | |
DEFAULT_USER_AGENT = 'PyProwl/1.0' | |
def __init__(self, application, event, description): | |
self.application = application | |
self.event = event | |
self.description = description | |
@property | |
def url(self): | |
url = URLObject(scheme='https', host='prowl.weks.net') | |
url = url / 'api' / 'add_notification.php' | |
url |= { | |
'application': self.application, | |
'event': self.event, | |
'description': self.description | |
} | |
return url | |
def request(self, username, password, user_agent=DEFAULT_USER_AGENT): | |
request = urllib2.Request(self.url) | |
request.headers['Authorization'] = 'Basic %s' % ( | |
(username + ':' + password).encode('base64').strip(),) | |
request.headers['User-agent'] = user_agent | |
return request | |
OPTION_PARSER = optparse.OptionParser(version='1.0') | |
OPTION_PARSER.add_option('-u', '--username', help='Prowl username') | |
OPTION_PARSER.add_option('-p', '--password', help='Prowl password') | |
OPTION_PARSER.add_option('-a', '--application', default="PyProwl", | |
help='Name of source application (default "PyProwl")') | |
OPTION_PARSER.add_option('-e', '--event', help='Name of event') | |
OPTION_PARSER.add_option('-d', '--description', help='Description of event') | |
OPTION_PARSER.add_option('-g', '--user-agent', | |
default=ProwlEvent.DEFAULT_USER_AGENT, | |
help='User-agent string (default "PyProwl/1.0")') | |
def main(): | |
options, args = OPTION_PARSER.parse_args() | |
event = ProwlEvent(options.application, options.event, | |
options.description) | |
request = event.request(options.username, options.password, | |
user_agent=options.user_agent) | |
# Connection | |
conn = None | |
try: | |
conn = urllib2.urlopen(request) | |
result = conn.read() | |
except urllib2.HTTPError, exc: | |
if exc.code == 401: | |
print 'Notification posting failed (invalid username or password)' | |
raise SystemExit(1) | |
else: | |
raise | |
finally: | |
getattr(conn, 'close', lambda: None)() | |
# If we got this far, the request must have been successful. | |
print 'Notification successfully posted.' | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment