-
Notifications
You must be signed in to change notification settings - Fork 0
/
ci.py
127 lines (105 loc) · 4.53 KB
/
ci.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import rumps
from AppKit import NSScreen
from PIL import Image, ImageFont, ImageDraw
from time import time, sleep
import json
import random
import os
import subprocess
class CiApp(object):
def __init__(self):
self.config = {
"app_name": "Ci",
"generate_poem": "Generate Ci",
"interval": 600,
}
self.app = rumps.App(self.config["app_name"])
self.set_up_menu()
self.generate_poem_button = rumps.MenuItem(
title=self.config["generate_poem"], callback=self.generate_poem)
self.app.menu = [self.generate_poem_button]
self.interval = self.config["interval"]
self.timer = rumps.Timer(self.on_tick, 1)
self.start_timer()
def set_up_menu(self):
self.app.title = "词"
def on_tick(self, sender):
time_left = sender.end - sender.count
mins = time_left // 60 if time_left >= 0 else time_left // 60 + 1
secs = time_left % 60 if time_left >= 0 else (-1 * time_left) % 60
if mins == 0 and time_left < 0:
self.generate_poem()
self.reset_timer()
sender.count += 1
def start_timer(self):
self.timer.count = 0
self.timer.end = self.interval
self.timer.start()
def reset_timer(self):
self.timer.stop()
self.timer.count = 0
self.start_timer()
def generate_poem(self, sender=None):
__location__ = os.path.realpath(os.path.join(
os.getcwd(), os.path.dirname(__file__)))
self.set_up_menu()
n = random.randint(1, 320) # generate random poem id
print(n)
# with open('poems.json') as f:
# data = json.load(f)
with open(os.path.join(os.path.dirname(__file__), 'poems.json'), 'r') as f:
data = json.load(f)
def find_poem(n): # extract poem from json
for keyval in data:
if n == keyval['id']:
return keyval['contents']
if (find_poem(n) != None):
title_text = find_poem(n)
def find_title(n): # extract poem title from json
for keyval in data:
if n == keyval['id']:
return keyval['title']
if (find_title(n) != None):
title = find_title(n)
# base = Image.open(os.path.join(
# __location__, 'base.jpeg')) # open base image
# base = Image.open(
# "/System/Library/Desktop Pictures/Solid Colors/Ocher.png")
screenHeight = int(NSScreen.mainScreen().frame().size.height)
screenWidth = int(NSScreen.mainScreen().frame().size.width)
base = Image.new('RGB', (screenWidth, screenHeight), (random.randint(0, 255) # generate random poem id
, random.randint(0, 255), random.randint(0, 255)))
image_editable = ImageDraw.Draw(base) # make it editable
ttf = ImageFont.truetype(
'qiji-combo.ttf', 64) # load font
# get the size of the poem with font
w, h = image_editable.textsize(title_text, font=ttf)
image_editable.text(((screenWidth-w)/2, (screenHeight-h)/2), title_text,
(49, 27, 8, 64), font=ttf) # center the poem itself
# get the size of the title with font
w2, h2 = image_editable.textsize(title, font=ttf)
# center the title horizontally, keep it above the poem vertically
image_editable.text(((screenWidth-w2)/2, ((screenHeight-h)/2)-h),
title, (49, 27, 8, 64), font=ttf)
outputPath = os.path.expanduser("~/Pictures/Ci-Wallpaper.png")
# base.save('output.png') # save output
base.save(outputPath)
print(title_text)
print("Current screen resolution: %dx%d" %
(screenWidth, screenHeight))
# change macos wallpaper
output_location = os.path.join(
os.path.expanduser("~/Pictures/"), 'Ci-Wallpaper.png')
command = "osascript -e 'tell application \"System Events\" to tell every desktop to set picture to " + \
output_location + " as POSIX file'"
print(command)
# refresh dock to show new wallpaper
# TODO: instead of killing dock after setting new wallpaper, add transitionary empty wallpaper to show for a split second before setting new wallpaper
subprocess.call(['/usr/bin/killall', 'Dock'])
os.system(command)
self.reset_timer()
def run(self):
self.app.run()
if __name__ == '__main__':
app = CiApp()
app.run()