Skip to content

Instantly share code, notes, and snippets.

@humiaozuzu
Created February 26, 2016 07:39
Show Gist options
  • Save humiaozuzu/15e498b94dbbc9c9d4aa to your computer and use it in GitHub Desktop.
Save humiaozuzu/15e498b94dbbc9c9d4aa to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
import Foundation
import objc
import logging
from time import sleep
LOC = {
'work': [131, '4828339.000000', '12962322.000000'],
'home': [131, '4824181.344814', '12965961.290962'],
}
REFRESH_INTERVAL = 30
NSUserNotification = objc.lookUpClass('NSUserNotification')
NSUserNotificationCenter = objc.lookUpClass('NSUserNotificationCenter')
logger = logging.getLogger()
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.INFO)
def get_surge(city_code, latitude, longitude):
url = 'https://rentcar.baidu.com/zuche'
payload = {
'start_latitude': latitude,
'start_longitude': longitude,
'qt': 'uberproduct',
'city_code': city_code,
'v': '12.0.0',
}
r = requests.get(url, params=payload)
surge = r.json()['data']['product_info'][0]['surge']
return float(surge)
def notify(title, subtitle, info_text, delay=0, sound=False, userInfo={}):
notification = NSUserNotification.alloc().init()
notification.setTitle_(title)
notification.setSubtitle_(subtitle)
notification.setInformativeText_(info_text)
notification.setUserInfo_(userInfo)
if sound:
notification.setSoundName_("NSUserNotificationDefaultSoundName")
notification.setDeliveryDate_(Foundation.NSDate.dateWithTimeInterval_sinceDate_(delay, Foundation.NSDate.date()))
NSUserNotificationCenter.defaultUserNotificationCenter().scheduleNotification_(notification)
def main():
loc, surge = sys.argv[1:]
surge = float(surge)
city_code, latitude, longitude = LOC[loc]
last_surge = 0
while 1:
current_surge = get_surge(city_code, latitude, longitude)
logger.info('Current surge: %s' % current_surge)
if (current_surge <= surge) and (current_surge != last_surge):
notify('Uber Surge Alert', None, 'Current surge %s' % current_surge, 0, True)
last_surge = current_surge
sleep(REFRESH_INTERVAL)
if __name__ == '__main__':
main()
# TODO:
# 1. set start time and end time
# 2. determine location without cli param
# 3. add more logging
@humiaozuzu
Copy link
Author

run with python uber.py home 1.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment