def add_date_words(): # 日期 now_year = datetime.datetime.now().year today = time.strftime("%Y.%m.%d") # 周几 weekday = get_weekday() # 几号 num = str(int(today.split('.')[-1])) # 农历 # ct = datetime.datetime(2015,2,19,13,0,15) # ln = Lunar(ct) # print('公历 {} 北京时间 {}'.format(ln.localtime.date(), ln.localtime.time())) # print('{} 【{}】 {}年 {}日 {}时'.format(ln.ln_date_str(), ln.gz_year(), ln.sx_year(), ln.gz_day(), ln.gz_hour())) # print('节气:{}'.format(ln.ln_jie())) ln = Lunar() lc_day = ln.gz_year() + '年' + ' ' + ln.ln_date_str()[2:] # 已过多少天 d1 = datetime.datetime.strptime('{}.01.01'.format(now_year), '%Y.%m.%d') d2 = datetime.datetime.strptime(today, "%Y.%m.%d") delta = (d1 - d2).days percent = round(-delta / get_year_days(now_year) * 100, 2) delta_days = '第{}天,进度已消耗{}%'.format(-delta, percent) font_size = 40 text_words(font_style, font_size, 40, 40, today) text_words(font_style, font_size, w - font_size * 2 - 40, 40, weekday) # 两个字符 font_size = 130 temp_height = h * (1 - 0.618) - font_size temp_x = get_center_x(1, font_size) text_words(font_style, font_size, temp_x, temp_height, num) temp_height += font_size font_size = 20 temp_x = get_center_x(7.5, font_size) text_words(font_style, font_size, temp_x, temp_height + 10, lc_day) # 7.5字符 temp_x = get_center_x(11 + len(str(delta)) * 0.5 - 0.5, font_size) text_words(font_style, font_size, temp_x, temp_height + font_size + 15, delta_days) # 11 + 0.5/1/1.5字符 # 进度条 draw.rectangle((40, 400, w - 40, 440), 'darkgray', 'darkgray') draw.rectangle((40, 400, (w - 40 * 2) * percent * 0.01 + 40, 440), color, color) gushi = requests.get('https://api.gushi.ci/all.json') try: data = gushi.json() title = data['origin'] author = data['author'] content = data['content'] temp_x = get_center_x(len(title), font_size) text_words(font_style, font_size, temp_x, (h - 40 - 500) * 0.2 + 500, title) temp_x = get_center_x(len(content), font_size + 2) text_words(font_style, font_size + 5, temp_x, 600, content) text_words(font_style, font_size, w - (len(author) + 2 + 1) * font_size - 40, 700, '——' + author) except: print('获取古诗词失败')
def query(self, query): result = [] try: if not query or query == "now": ln = Lunar() else: # 兼容时间戳,如 1473905472000 if len(query.split(" ")) == 1 and len(query) == 12 or len( query) == 13: try: timenumber = float(query) / 1e3 query = str( datetime.datetime.fromtimestamp(timenumber)) except ValueError: pass ln = Lunar(parse(query)) except ValueError: result.append({ "Title": 'Can not parse input string', "SubTitle": 'Please input standard date and time format strings', "IcoPath": "Images/app.png", }) else: jieri = ln.ln_jie() # 阳历节日 date = ln.localtime.strftime('%Y-%m-%d %H:%M:%S') # 当前时间 notime = False if date[11:] == "00:00:00": notime = True time1 = date[0:11] if notime else date result.append({ "Title": time1 + (' 【' + jieri + '】' if jieri else ""), "SubTitle": ln.localtime.strftime("%A") + " " + week_day_dict[ln.localtime.weekday()], "IcoPath": "Images/app.png", "JsonRPCAction": { "method": "add_to_clipboard", "parameters": [time1], "dontHideAfterAction": False } }) if datetime.datetime(1901, 1, 1) < ln.localtime: jieqi = ln.ln_jieqi() # 节气 jieri_lu = ln.ln_jie(True) # 农历节日 lutime1 = '{} {}'.format( ln.ln_date_str(), '【' + jieri_lu + '】' if jieri_lu else "") lutime2 = '【{}】 {}年-{}日{} {}'.format( ln.sx_year(), ln.gz_year(), ln.gz_day(), ("-" + ln.gz_hour() + "时") if not notime else "", ' 节气: ' + jieqi if jieqi else "") result.append({ "Title": lutime1, "SubTitle": lutime2, "IcoPath": "Images/app.png", "JsonRPCAction": { "method": "add_to_clipboard", "parameters": [lutime1], "dontHideAfterAction": False } }) return result