Skip to content
This repository was archived by the owner on Mar 31, 2020. It is now read-only.

Commit 4aeafd5

Browse files
committed
Merge conflicts resolved
2 parents 1f75af4 + 8842847 commit 4aeafd5

File tree

4 files changed

+162
-103
lines changed

4 files changed

+162
-103
lines changed

Pipfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ more-itertools = "*"
1212
pillow = "*"
1313
pygame = "*"
1414
aiohttp = "*"
15+
configparser = "*"
1516

1617
[requires]
1718
python_version = "3.7"

Pipfile.lock

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.py

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import aiohttp
2+
from random import randint, choice, sample
3+
from PIL import Image, ImageTk
4+
import os
5+
import io
6+
import configparser
7+
8+
9+
class Cache:
10+
'''Class used for caching images and data about the cats'''
11+
12+
def __init__(self, root):
13+
# setting root
14+
self.root = root
15+
self.screen_x = self.root.winfo_screenwidth()
16+
self.screen_y = self.root.winfo_screenheight()
17+
18+
# setting class variables for use later
19+
self.cats = list()
20+
self.session = None
21+
22+
# get settings
23+
self.dir = os.path.dirname(os.path.realpath(__file__))
24+
cp = configparser.ConfigParser()
25+
cp.read(os.path.join(self.dir, 'settings.ini'))
26+
cp.read('settings.ini')
27+
28+
# for now, let's just look up the DEV settings
29+
# can change this later
30+
# configparser will use values from DEFAULT section if none provided elsewhere
31+
if 'DEV' in cp.sections():
32+
self.config = cp['DEV']
33+
else:
34+
self.config = cp['DEFAULT']
35+
36+
# getting the directory folder for use later when opening files
37+
38+
async def refill(self):
39+
'''Gets a cache of cat data and adds it to the self.cats list'''
40+
41+
# if we haven't created a session yet, do so
42+
if not self.session:
43+
self.session = aiohttp.ClientSession()
44+
45+
cachesize = int(self.config['cachesize'])
46+
47+
# Run 10 times to get 10 cats
48+
for i in range(cachesize):
49+
# initialize a dict of cat data
50+
cat_data = dict()
51+
52+
# randomly make jumpscares happen, but not on the first image
53+
if randint(1, 10) == 5 and i:
54+
# get a random number for an image
55+
image_number = randint(1, 10)
56+
# open and resize the image using Pillow
57+
im = Image.open(os.path.join(self.dir, os.path.join("res",
58+
os.path.join("images", f"{image_number}.jpg"))))
59+
im = im.resize((self.screen_x, self.screen_y - 150), Image.NEAREST)
60+
# make the image a tkinter image
61+
image = ImageTk.PhotoImage(im)
62+
# update the cat data dict
63+
cat_data.update({"image": image})
64+
cat_data.update({"jumpscare": True})
65+
else:
66+
# set jumpscare to False because it isnt a jumpscare image
67+
cat_data.update({"jumpscare": False})
68+
69+
# get a url from The Cat API
70+
async with self.session.get('https://api.thecatapi.com/v1/images/search') as res:
71+
data = await res.json()
72+
url = data[0]['url']
73+
# get image data from that url
74+
async with self.session.get(url) as res:
75+
image_bytes = await res.read()
76+
# open and the image in pillow
77+
im = Image.open(io.BytesIO(image_bytes))
78+
im = im.resize((400, 440), Image.NEAREST)
79+
# make the image a tkinter image
80+
image = ImageTk.PhotoImage(im)
81+
# update the cat data dict
82+
cat_data.update({"image": image})
83+
84+
# get a random name
85+
async with self.session.get(
86+
"https://www.pawclub.com.au/assets/js/namesTemp.json") as res:
87+
data = await res.json()
88+
# get a random letter for the name
89+
# Note: website doesn't have any b names which is why it is left out here
90+
letter = choice(list('acdefghijklmnopqrstuvwxyz'))
91+
# randomly choose a name from the json with that letter
92+
cat = choice(data[letter])
93+
# update the cat data dict
94+
cat_data.update({"name": cat["name"]})
95+
cat_data.update({"gender": cat["gender"]})
96+
97+
# get 5 random hobbies
98+
async with self.session.get(
99+
"https://gist.githubusercontent.com/mbejda/" +
100+
"453fdb77ef8d4d3b3a67/raw/e8334f09109dc212892406e25fdee03efdc23f56/" +
101+
"hobbies.txt"
102+
) as res:
103+
text = await res.text()
104+
# split the raw text of hobbies into a list
105+
all_hobbies = text.split("\n")
106+
# get 5 of those hobbies
107+
hobby_list = sample(all_hobbies, 5)
108+
# join those 5 hobbies into a bulleted list (string)
109+
list_of_hobbies = "\n •".join(hobby_list)
110+
hobbies = f"Hobbies:\n{list_of_hobbies}"
111+
# update the cat_data dict
112+
cat_data.update({"hobbies": hobbies})
113+
114+
# get a random age between 1 and 15 (avg lifespan of a cat)
115+
age = str(randint(1, 15))
116+
# update the cat data dict
117+
cat_data.update({"age": age})
118+
119+
# get a random number of miles away between 1 and 5
120+
miles = randint(1, 5)
121+
location = f"{miles} miles away"
122+
# update the cat data dict
123+
cat_data.update({"location": location})
124+
self.cats.append(cat_data)

src/mainwindow.py

Lines changed: 12 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,9 @@
11
import configparser
22
import tkinter as tk
3-
from PIL import Image, ImageTk
4-
import io
5-
from random import randint, choice, sample
63
import asyncio
7-
import aiohttp
8-
import os
94
from pygame import mixer
5+
import os
6+
from cache import Cache
107

118

129
class Tinder:
@@ -16,12 +13,10 @@ def __init__(self):
1613
# setup for pygame mixer
1714
mixer.init()
1815

19-
# getting the directory folder for use later when opening files
20-
self.dir = os.path.dirname(os.path.realpath(__file__))
21-
2216
# get settings
17+
self.dir = os.path.dirname(os.path.realpath(__file__))
2318
cp = configparser.ConfigParser()
24-
cp.read('settings.ini')
19+
cp.read(os.path.join(self.dir, 'settings.ini'))
2520

2621
# for now, let's just look up the DEV settings
2722
# can change this later
@@ -47,102 +42,17 @@ def __init__(self):
4742
# setting class variables to be used later
4843
self.jumpscare = False
4944
self.loop = asyncio.get_event_loop()
50-
self.session = None
51-
self.cats = list()
52-
53-
self.frame = tk.Frame()
45+
self.cache = Cache(self.root)
5446

5547
def start(self):
5648
'''Starts the Tinder application'''
5749

5850
# getting a cache of cat info
59-
self.loop.run_until_complete(self.get_cache())
51+
self.loop.run_until_complete(self.cache.refill())
6052

6153
# starting the program loop
6254
self.new_image()
6355

64-
async def get_cache(self):
65-
'''Gets a cache of cat data and adds it to the self.cats list'''
66-
67-
# if we haven't created a session yet, do so
68-
if not self.session:
69-
self.session = aiohttp.ClientSession()
70-
71-
cachesize = int(self.config['cachesize'])
72-
# Run 10 times to get 10 cats
73-
for i in range(cachesize):
74-
# initialize a dict of cat data
75-
cat_data = dict()
76-
77-
# randomly make jumpscares happen, but not on the first image
78-
if randint(1, 10) == 5 and i:
79-
# get a random number for an image
80-
image_number = randint(1, 10)
81-
# open and resize the image using Pillow
82-
im = Image.open(os.path.join(self.dir, os.path.join("res",
83-
os.path.join("images", f"{image_number}.jpg"))))
84-
im = im.resize((self.screen_x, self.screen_y - 150), Image.NEAREST)
85-
# make the image a tkinter image
86-
image = ImageTk.PhotoImage(im)
87-
# update the cat data dict
88-
cat_data.update({"image": image})
89-
cat_data.update({"jumpscare": True})
90-
else:
91-
# set jumpscare to False because it isnt a jumpscare image
92-
cat_data.update({"jumpscare": False})
93-
94-
# get a url from The Cat API
95-
async with self.session.get(self.config['catapi']) as res:
96-
data = await res.json()
97-
url = data[0]['url']
98-
# get image data from that url
99-
async with self.session.get(url) as res:
100-
image_bytes = await res.read()
101-
# open and the image in pillow
102-
im = Image.open(io.BytesIO(image_bytes))
103-
im = im.resize((400, 440), Image.NEAREST)
104-
# make the image a tkinter image
105-
image = ImageTk.PhotoImage(im)
106-
# update the cat data dict
107-
cat_data.update({"image": image})
108-
109-
# get a random name
110-
async with self.session.get(self.config['namefile']) as res:
111-
data = await res.json()
112-
# get a random letter for the name
113-
# Note: website doesn't have any b names which is why it is left out here
114-
letter = choice(list('acdefghijklmnopqrstuvwxyz'))
115-
# randomly choose a name from the json with that letter
116-
cat = choice(data[letter])
117-
# update the cat data dict
118-
cat_data.update({"name": cat["name"]})
119-
cat_data.update({"gender": cat["gender"]})
120-
121-
# get 5 random hobbies
122-
async with self.session.get(self.config['hobbiesfile']) as res:
123-
text = await res.text()
124-
# split the raw text of hobbies into a list
125-
all_hobbies = text.split("\n")
126-
# get 5 of those hobbies
127-
hobby_list = sample(all_hobbies, 5)
128-
# join those 5 hobbies into a bulleted list (string)
129-
list_of_hobbies = "\n •".join(hobby_list)
130-
hobbies = f"Hobbies:\n{list_of_hobbies}"
131-
# update the cat_data dict
132-
cat_data.update({"hobbies": hobbies})
133-
134-
# get a random age between 1 and 15 (avg lifespan of a cat)
135-
age = str(randint(1, 15))
136-
# update the cat data dict
137-
cat_data.update({"age": age})
138-
139-
# get a random number of miles away between 1 and 5
140-
miles = randint(1, 5)
141-
location = f"{miles} miles away"
142-
# update the cat data dict
143-
cat_data.update({"location": location})
144-
self.cats.append(cat_data)
145-
14656
def all_children(self):
14757
'''Used to get all children of the root window
14858
@@ -183,11 +93,11 @@ def new_image(self, cat=None):
18393
# if a dict wasn't passed to the function, get a dict from self.cats
18494
if not cat:
18595
try:
186-
cat = self.cats.pop(0)
96+
cat = self.cache.cats.pop(0)
18797
except IndexError:
18898
# the cache is empty so refill it
189-
self.loop.run_until_complete(self.get_cache())
190-
cat = self.cats.pop(0)
99+
self.loop.run_until_complete(self.cache.refill())
100+
cat = self.cache.cats.pop(0)
191101

192102
# getting base cat variables from the dict
193103
image = cat["image"]
@@ -238,17 +148,17 @@ def new_image(self, cat=None):
238148
# make a button to allow the user to pass through the image
239149
# Note: since everyone likes scary monsters, only make a Like button
240150
tk.Button(
241-
self.frame, text="Like", background="green",
151+
self.frame, text=self.config['like.text'], background="green",
242152
command=self.new_image).pack(side=tk.BOTTOM)
243153

244154
# image was not a jumpscare, don't do jumpscare things
245155
else:
246156
# setting up like and dislike buttons on opposite sides of the screen
247157
tk.Button(
248-
self.frame, text="Like", background="green",
158+
self.frame, text=self.config['like.text'], background="green",
249159
command=self.new_image).pack(side=tk.RIGHT)
250160
tk.Button(
251-
self.frame, text="Dislike", background="red",
161+
self.frame, text=self.config['dislike.text'], background="red",
252162
command=self.new_image).pack(side=tk.LEFT)
253163

254164
# defining button functions

0 commit comments

Comments
 (0)