-
Notifications
You must be signed in to change notification settings - Fork 0
/
quzin.py
executable file
·260 lines (220 loc) · 9.68 KB
/
quzin.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/python
import clickablelabel
import configparser
import datetime
import json
import keyboardlineedit
import logging
import os
import sqlite3
import sys
import webbrowser
import requests
import resources
from PyQt5 import QtCore, uic
from PyQt5.QtCore import QSize, QCoreApplication
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import *
from darksky import forecast
from googleapiclient.discovery import build
from lxml import html
if getattr(sys, 'frozen', False):
# we are running in a bundle
bundle_dir = sys._MEIPASS
else:
# we are running in a normal Python environment
bundle_dir = os.path.dirname(os.path.abspath(__file__))
configpath = 'config'
configParser = configparser.RawConfigParser()
configParser.read(configpath)
guipath = os.path.join(bundle_dir, 'qtcreator/mainwindow.ui')
dialogpath = os.path.join(bundle_dir, 'qtcreator/dialog.ui')
jsonpath = os.path.join(bundle_dir, 'season_items.json')
iconspath = os.path.join(bundle_dir, 'icons/')
translationpath = os.path.join(bundle_dir, 'translations/' + configParser.get('general', 'lang') + '.qm')
dbpath = os.path.join(bundle_dir, 'database/recipes.sqlite')
logging.basicConfig(filename=os.path.join(bundle_dir, 'quzin.log'), level=logging.WARNING)
class RecipeDialog(QDialog):
curr = None
curr_recipe = (None, None, None)
def show_recipe(self):
if self.curr is None:
return
meta = self.curr.data(32)
if meta == self.curr.text():
return self.view_saved_recipe()
page = requests.get(self.curr.data(32))
tree = html.fromstring(page.text)
ingredientSections = tree.cssselect(".ingredients-list p")
ingredients = tree.cssselect(".ingredients-list ul")
method_steps = tree.cssselect(".recipe-main .method .text ul")
title = tree.cssselect(".recipe .title")
ingredient_text = ""
method_text = ""
title_text = title[0].text
if ingredientSections:
if len(ingredients) > len(ingredientSections):
for ingredient in ingredients[0]:
ingredient_text += ingredient.text_content()
ingredient_text += "\n"
for idx, section in enumerate(ingredientSections):
ingredient_text += "\n"
ingredient_text += ingredientSections[idx].text_content()
ingredient_text += "\n"
for ingredient in ingredients[idx + 1]:
ingredient_text += ingredient.text_content()
ingredient_text += "\n"
else:
for idx, section in enumerate(ingredientSections):
ingredient_text += "\n"
ingredient_text += ingredientSections[idx].text_content()
ingredient_text += "\n"
for ingredient in ingredients[idx]:
ingredient_text += ingredient.text_content()
ingredient_text += "\n"
else:
for ingredient in ingredients[0]:
ingredient_text += ingredient.text_content()
ingredient_text += "\n"
for step in method_steps[0]:
method_text += step.text_content()
method_text += "\n"
self.curr_recipe = (title_text, ingredient_text, method_text)
self.fill_recipe_parts(title_text, ingredient_text, method_text)
def view_saved_recipe(self):
conn = sqlite3.connect(dbpath)
c = conn.cursor()
c.execute("SELECT * FROM recipes WHERE name=?", (self.curr.text(),))
recipe = c.fetchone()
conn.commit()
conn.close()
self.fill_recipe_parts(recipe[0], recipe[1], recipe[2], False)
def save_recipe(self):
if self.curr_recipe is None:
return
conn = sqlite3.connect(dbpath)
c = conn.cursor()
try:
c.execute("INSERT INTO recipes VALUES (?, ?, ?)", (self.curr_recipe[0], self.curr_recipe[1], self.curr_recipe[2]))
except sqlite3.IntegrityError:
print('ERROR: Recipe already exists with name {}'.format(self.curr_recipe[0]))
conn.commit()
conn.close()
def fill_recipe_parts(self, title, ingredients, instructions, show_save_btn=True):
self.recipeIngredients.setPlainText(ingredients)
self.recipeIngredients.verticalScrollBar().triggerAction(QScrollBar.SliderToMinimum)
self.recipeInstructions.setPlainText(instructions)
self.recipeIngredients.verticalScrollBar().triggerAction(QScrollBar.SliderToMinimum)
self.setWindowTitle(title)
if not show_save_btn:
self.saveRecipeBtn.hide()
def set_recipe(self, curr):
self.curr = curr
def __init__(self):
super(self.__class__, self).__init__()
uic.loadUi(dialogpath, self)
self.saveRecipeBtn.clicked.connect(self.save_recipe)
class MainWindow(QMainWindow):
def set_datetime(self):
current_time = str(datetime.datetime.now().strftime("%H:%M\n%a, %d %b"))
self.timeLabel.setText(current_time)
def search_recipes(self):
self.searchResultsList.clear()
search_value = self.searchInput.text()
service = build("customsearch", "v1", developerKey=configParser.get('recipes', 'developer_key'))
response = service.cse().list(
q=search_value,
cx=configParser.get('recipes', 'cx'),
lr="lang_" + configParser.get('general', 'lang')
).execute()
items = response.get("items")
if items is None:
listItem = QListWidgetItem(QCoreApplication.translate("main", "No recipes found"))
self.searchResultsList.addItem(listItem)
else:
for item in items:
listItem = QListWidgetItem(item.get("title"))
listItem.setData(32, item.get("link"))
listItem.setSizeHint(QSize(50, 50))
self.searchResultsList.addItem(listItem)
def view_recipes(self):
self.searchResultsList.clear()
conn = sqlite3.connect(dbpath)
c = conn.cursor()
c.execute("SELECT * FROM recipes ORDER BY name ASC")
recipes = c.fetchall()
conn.close()
if recipes is None:
listItem = QListWidgetItem(QCoreApplication.translate("main", "No recipes found"))
self.searchResultsList.addItem(listItem)
else:
for recipe in recipes:
listItem = QListWidgetItem(recipe[0])
listItem.setData(32, recipe[0])
listItem.setSizeHint(QSize(50, 50))
self.searchResultsList.addItem(listItem)
def show_recipe(self, curr):
dialog = RecipeDialog()
dialog.showMaximized()
dialog.set_recipe(curr)
dialog.show_recipe()
dialog.exec_()
def show_season_items(self, index):
data = json.load(open(jsonpath))
self.seasonItems.setPlainText(data[index])
def set_weather(self):
weather = forecast(configParser.get('weather', 'key'), configParser.get('weather', 'latitude'),
configParser.get('weather', 'longitude'), units="si",
lang=configParser.get('general', 'lang'))
weather_text = "{:.1f}".format(weather.temperature) + "°C\n"
weather_icon = QPixmap(iconspath + weather.icon + ".png")
self.weatherIconLabel.setPixmap(weather_icon)
self.weatherIconLabel.setScaledContents(True)
self.weatherLabel.setText(weather_text)
self.weatherSummaryLabel.setText(weather.daily[0].summary)
def set_season_items(self):
month = datetime.datetime.now().month - 1
self.show_season_items(month)
def open_weather(self):
lat = configParser.get('weather', 'latitude')
long = configParser.get('weather', 'longitude')
webbrowser.open_new_tab("https://darksky.net/forecast/" + lat + "," + long + "/ca12/en")
def __init__(self):
super(self.__class__, self).__init__()
try:
uic.loadUi(guipath, self)
self.set_datetime()
self.set_weather()
self.set_season_items()
self.datetime_timer = QtCore.QTimer(self)
self.datetime_timer.setInterval(60000)
self.datetime_timer.timeout.connect(lambda: self.set_datetime())
self.datetime_timer.start()
self.weather_timer = QtCore.QTimer(self)
self.weather_timer.setInterval(3600000)
self.weather_timer.timeout.connect(lambda: self.set_weather())
self.weather_timer.start()
self.seasons_timer = QtCore.QTimer(self)
self.seasons_timer.setInterval(86400000)
self.seasons_timer.timeout.connect(lambda: self.set_season_items())
self.seasons_timer.start()
self.searchBtn.clicked.connect(self.search_recipes)
self.searchBtn.setAutoDefault(True)
self.searchInput.returnPressed.connect(self.searchBtn.click)
self.searchResultsList.itemClicked.connect(self.show_recipe)
self.viewRecipesBtn.clicked.connect(self.view_recipes)
self.seasonMonthSelect.currentIndexChanged.connect(self.show_season_items)
self.seasonMonthSelect.setCurrentIndex(datetime.datetime.now().month - 1)
self.weatherIconLabel.clicked.connect(self.open_weather)
except Exception as e:
logging.exception(datetime.datetime.now().strftime("%H:%M\n%a, %d %b") + " " + e)
def main():
app = QApplication(["Quzin"])
translator = QtCore.QTranslator()
translator.load(translationpath)
app.installTranslator(translator)
form = MainWindow()
form.showMaximized()
sys.exit(app.exec_())
if __name__ == "__main__":
main()