Last active
August 29, 2015 14:04
-
-
Save siriusjack/a2fe7c8785dbc8908706 to your computer and use it in GitHub Desktop.
auto-attendance
This file contains 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
#!/usr/bin/env python | |
# using: utf-8 | |
import os | |
import sys | |
# constant | |
plist_name = 'net.camph.auto-attend.plist' | |
def main(): | |
if (len(sys.argv) < 2): | |
print 'usage: python auto-attendance-setup.py [option]' | |
print '[options]:' | |
print ' install : install auto-attend' | |
print ' uninstall : uninstall auto-attend' | |
quit() | |
if (sys.argv[1] == 'install'): | |
# facebookID: | |
# visit http://wwww.attend.camph.net/house | |
# find your facebook image and inspect the src element | |
# you'll find graph.facebook.com/*****/picture?... in the argument | |
# and the number embeded in ***** is your facebookID | |
# | |
# databaseID: | |
# the p_value of the img is your databaseID | |
facebookID = input('facebookID > ') | |
databaseID = input('databaseID > ') | |
make(facebookID, databaseID) | |
install() | |
elif (sys.argv[1] == 'uninstall'): | |
uninstall() | |
else: | |
print 'option is invalid' | |
def create_plist(facebookID, databaseID): | |
command = ''' | |
ssid=`/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'` | |
if [[ $ssid = 'camphor_kyoto' || $ssid = 'camphor_tokyo' ]]; then | |
num_attendance=`curl https://attend.camph.net/ | grep %(facebookID)d | wc -l` | |
if [ $num_attendance -lt 1 ]; then | |
curl https://attend.camph.net/php/syusseki.php -X POST -d id=%(databaseID)d | |
fi | |
fi | |
''' %locals() | |
template = '''<?xml version="1.0" encoding="UTF-8"?> | |
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | |
<plist version="1.0"> | |
<dict> | |
<key>Label</key> | |
<string>net.camph.auto-attend</string> | |
<key>ProgramArguments</key> | |
<array> | |
<string>bash</string> | |
<string>-c</string> | |
<string>%s</string> | |
</array> | |
<key>StartInterval</key> | |
<integer>900</integer> | |
<key>RunAtLoad</key> | |
<true/> | |
</dict> | |
</plist>''' %(command) | |
return template | |
def make(facebookID, databaseID): | |
f = open(plist_name, 'w') | |
f.write(create_plist(facebookID, databaseID)) | |
def install(): | |
home_dir = os.path.expanduser("~") | |
plist_path = '%s/Library/LaunchAgents/%s' %(home_dir, plist_name) | |
os.system('mv %s %s'%(plist_name, plist_path)) | |
os.system('launchctl load %s'%(plist_path)) | |
def uninstall(): | |
home_dir = os.path.expanduser("~") | |
plist_path = '%s/Library/LaunchAgents/%s' %(home_dir, plist_name) | |
os.system('launchctl unload %s'%(plist_path)) | |
os.system('rm %s' %(plist_path)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment