签到检测程序,解析github提供的api内的json,解决了服务器和本地时间不同步的问题(时差+8H),实现按日期更新当前签到表。下一步是从api获取organization的信息,求出未签到的成员ID。(女朋友在写啦~)
1 #coding=utf-8 2 import urllib 3 import re 4 import json 5 import time 6 7 # 记录签到情况的set 8 check = set() 9 10 # 日期 11 class Date: 12 year = 0 13 month = 0 14 day = 0 15 hour = 0 16 minute = 0 17 second = 0 18 def __init__(self, year, month, day, hour, minute, second, cflag): 19 # 处理本地与服务器所在地的时差问题 +8h 20 if cflag == 1: 21 common = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 22 leap = [0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] 23 hour += 8 24 if hour > 24: 25 hour -= 24 26 day += 1 27 if year % 400 == 0 or (year % 4 == 0 and year % 100 != 0): 28 if day > leap[month]: 29 day -= leap[month] 30 month += 1 31 else: 32 if day > common[month]: 33 day -= common[month] 34 month += 1 35 if month > 12: 36 year += 1 37 month -= 12 38 self.year = year 39 self.month = month 40 self.day = day 41 self.hour = hour 42 self.minute = minute 43 self.second = second 44 45 46 # 存储用户信息 47 class User: 48 name = '' 49 date = Date(0, 0, 0, 0, 0, 0, 0) 50 def __init__(self, name, date): 51 self.name = name 52 self.date = date 53 54 # GitHub Api URL 55 gitHubCommitsApi = 'https://api.github.com/repos/aplusb/Let-the-bass-kick/commits' 56 57 # 获取json 58 def getJson(url): 59 page = urllib.urlopen(url) 60 html = page.read() 61 return html 62 63 # 保存json, commits.json 64 def saveJSON(html, fileNeme): 65 fileWrite = open(fileNeme, 'w') 66 fileWrite.write(html) 67 fileWrite.close() 68 69 # 处理json 70 def processJSON(commits): 71 users = [] 72 pr = json.loads(commits) 73 for each in pr: 74 name = each["commit"]["author"]["name"] 75 date = each["commit"]["author"]["date"] 76 yy = int(date[:4]) 77 mm = int(date[5:7]) 78 dd = int(date[8:10]) 79 hh = int(date[11:13]) 80 mi = int(date[14:16]) 81 ss = int(date[17:19]) 82 users.append(User(name, Date(yy, mm, dd, hh, mi, ss, 1))) 83 return users 84 85 # 获取当天信息 86 def getToday(): 87 ISOTIMEFORMAT = '%Y-%m-%dT%XZ' 88 curDate = time.strftime(ISOTIMEFORMAT, time.localtime()) 89 yy = int(curDate[:4]) 90 mm = int(curDate[5:7]) 91 dd = int(curDate[8:10]) 92 hh = int(curDate[11:13]) 93 mi = int(curDate[14:16]) 94 ss = int(curDate[17:19]) 95 return Date(yy, mm, dd, hh, mi, ss, 0) 96 97 # 统计当天的commit情况 98 def checkTimes(users): 99 today = getToday()100 for each in users:101 if today.year == each.date.year and today.month == each.date.month and today.day == each.date.day:102 check.add(each.name)103 104 # 展示当天签到情况105 def show(users, check):106 for each in users:107 print '%-20s %s-%s-%s %s:%s:%s' % (each.name, str(each.date.year).zfill(4),108 str(each.date.month).zfill(2), str(each.date.day).zfill(2),109 str(each.date.hour).zfill(2), str(each.date.minute).zfill(2),110 str(each.date.second).zfill(2))111 print '----------------------Today checked----------------------'112 for each in check:113 print '%s' % each114 115 # 读取之前的签到情况116 def readStatus():117 fileRead = open('checked.txt', 'r')118 ISOTIMEFORMAT = '%Y-%m-%dT%XZ'119 today = time.strftime(ISOTIMEFORMAT, time.localtime())120 curDate = fileRead.readline()121 done = 0122 # 初始化新文件123 if curDate == '':124 return125 # 判断是不是today保存过的签到情况,如果是则读取信息126 if int(curDate[:4]) == int(today[:4]) and int(curDate[5:7]) == int(today[5:7]) and int(curDate[8:10]) == int(today[8:10]):127 while not done:128 entry = fileRead.readline()129 # 去空行130 entry = entry.strip('\n')131 if entry != '':132 check.add(entry)133 else:134 done = 1135 fileRead.close() 136 137 # 保存统计结果138 def saveStatus():139 fileWrite = open('checked.txt', 'w')140 # 打时间戳141 ISOTIMEFORMAT = '%Y-%m-%dT%XZ'142 today = time.strftime(ISOTIMEFORMAT, time.localtime())143 fileWrite.write(today)144 fileWrite.write('\n')145 # 保存信息146 for each in check:147 fileWrite.write(each)148 fileWrite.write('\n')149 fileWrite.close()150 151 152 # 保存当前仍未签到的ID153 154 def __main__():155 commits = getJson(gitHubCommitsApi)156 fileName = 'commits.json'157 readStatus()158 saveJSON(commits, fileName)159 users = processJSON(commits)160 checkTimes(users)161 saveStatus()162 show(users, check)163 164 165 __main__()