[每天一道A+B]签到检测程序
生活随笔
收集整理的這篇文章主要介紹了
[每天一道A+B]签到检测程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
簽到檢測程序,解析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' % each 114 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 = 0 122 # 初始化新文件 123 if curDate == '': 124 return 125 # 判斷是不是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 = 1 135 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 # 保存當前仍未簽到的ID 153 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__()?
轉載于:https://www.cnblogs.com/kirai/p/5469055.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的[每天一道A+B]签到检测程序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【VS开发】PCIe体系结构的组成部件
- 下一篇: spark 编译