PageRenderTime 63ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/run.py

https://bitbucket.org/skywalking/loginparttimesystem
Python | 118 lines | 92 code | 16 blank | 10 comment | 18 complexity | cc9cb3e9bdec976ce288cd8fe1f8cea4 MD5 | raw file
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import cookielib, optparse, setting, urllib, urllib2, sys
  4. from BeautifulSoup import BeautifulSoup
  5. from datetime import datetime
  6. from time import sleep
  7. interval = 10
  8. reload(sys)
  9. sys.setdefaultencoding('utf-8')
  10. # for debug
  11. def getCookieValue(cj):
  12. for cookie in cj:
  13. print cookie
  14. def post(url, opener, post_params = None):
  15. if post_params is None:
  16. response = opener.open(url)
  17. else:
  18. post_params = urllib.urlencode(post_params)
  19. response = opener.open(url, post_params)
  20. return response.read()
  21. def parse_signin(content, project = 1):
  22. info = ()
  23. bs = BeautifulSoup(content).findAll('tr')[project]
  24. v = bs.findAll('td')
  25. k = bs.find('input', {'name': 'signin'})
  26. info = (k['value'], v[5].text)
  27. return info
  28. def parse_signout(content):
  29. bs = BeautifulSoup(content).find('div', {'id': 'body'})
  30. if bs.text == '您沒有簽到記錄,無法進行簽退 ....':
  31. return 0
  32. else:
  33. info = ()
  34. v = BeautifulSoup(content).findAll('td')
  35. k = BeautifulSoup(content).find('input', {'name': 'signout'})
  36. info = (k['value'], v[16].text)
  37. return info
  38. def main():
  39. # init setting
  40. login_params = setting.login_params
  41. header = setting.header
  42. op = optparse.OptionParser("[用法] => python run.py -p <#project> -m <#minute>")
  43. op.add_option("-p", dest="project", type="int", help="指定計畫")
  44. op.add_option("-m", dest="minute", type="int", help="登錄多長")
  45. (options, args) = op.parse_args()
  46. if options.project is None:
  47. print op.usage
  48. print '[例子] => python run.py -p 3 -m 60'
  49. exit(0)
  50. else:
  51. project = options.project
  52. if options.minute is None:
  53. minute = 60
  54. else:
  55. minute = options.minute
  56. print '系統設定: 計畫%s,登錄%s分鐘' %(project, minute)
  57. # save cookie
  58. cj = cookielib.LWPCookieJar()
  59. opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
  60. opener.addheaders = header
  61. # 登入系統
  62. login_url = "http://wallaby.cc.ncu.edu.tw/j_spring_security_check"
  63. ret = post(login_url, opener, login_params)
  64. if ret:
  65. print '登入系統'
  66. # 進行簽到
  67. signin_url = "http://140.115.182.62/PartTime/parttime.php/signin"
  68. content = post(signin_url, opener)
  69. ret = parse_signin(content, project)
  70. if ret:
  71. signin_params = { 'signin': ret[0], 'submit': '送出' }
  72. post(signin_url, opener, signin_params)
  73. print '進行簽到: ' + ret[1]
  74. print '簽到時間: ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  75. # keep session alive
  76. if minute <= interval:
  77. sleep(minute * 60)
  78. else:
  79. rounds = minute / interval
  80. remain = minute % interval
  81. for i in range(rounds):
  82. signin_url = "http://140.115.182.62/PartTime/parttime.php/signin"
  83. content = post(signin_url, opener)
  84. sleep(interval * 60) # sleep 10 mins
  85. sleep(remain * 60)
  86. # 進行簽退
  87. signout_url = "http://140.115.182.62/PartTime/parttime.php/signout"
  88. content = post(signout_url, opener)
  89. ret = parse_signout(content)
  90. if ret == 0:
  91. print '您沒有簽到記錄,無法進行簽退 ....'
  92. else:
  93. signout_params = { 'signout': ret[0], 'submit': '送出' }
  94. post(signout_url, opener, signout_params)
  95. print '進行簽退: ' + ret[1]
  96. print '簽退時間: ' + datetime.now().strftime('%Y-%m-%d %H:%M:%S')
  97. # 登出系統
  98. loginout_url = 'http://wallaby.cc.ncu.edu.tw/logout'
  99. if post(loginout_url, opener):
  100. print '登出系統'
  101. if __name__ == '__main__':
  102. main()