11import configparser
22import tkinter as tk
3- from PIL import Image , ImageTk
4- import io
5- from random import randint , choice , sample
63import asyncio
7- import aiohttp
84from pygame import mixer
5+ import os
6+ from cache import Cache
97
108from . import SETTINGS , IMAGES , SOUNDS
119
@@ -17,10 +15,10 @@ def __init__(self):
1715 # setup for pygame mixer
1816 mixer .init ()
1917
20- # getting the directory folder for use later when opening files
2118 # get settings
19+ self .dir = os .path .dirname (os .path .realpath (__file__ ))
2220 cp = configparser .ConfigParser ()
23- cp .read (str ( SETTINGS ))
21+ cp .read (os . path . join ( self . dir , 'settings.ini' ))
2422
2523 # for now, let's just look up the DEV settings
2624 # can change this later
@@ -46,102 +44,17 @@ def __init__(self):
4644 # setting class variables to be used later
4745 self .jumpscare = False
4846 self .loop = asyncio .get_event_loop ()
49- self .session = None
50- self .cats = list ()
51-
52- self .frame = tk .Frame ()
47+ self .cache = Cache (self .root )
5348
5449 def start (self ):
5550 '''Starts the Tinder application'''
5651
5752 # getting a cache of cat info
58- self .loop .run_until_complete (self .get_cache ())
53+ self .loop .run_until_complete (self .cache . refill ())
5954
6055 # starting the program loop
6156 self .new_image ()
6257
63- async def get_cache (self ):
64- '''Gets a cache of cat data and adds it to the self.cats list'''
65-
66- # if we haven't created a session yet, do so
67- if not self .session :
68- self .session = aiohttp .ClientSession ()
69-
70- cachesize = int (self .config ['cachesize' ])
71- # Run 10 times to get 10 cats
72- for i in range (cachesize ):
73- # initialize a dict of cat data
74- cat_data = dict ()
75-
76- # randomly make jumpscares happen, but not on the first image
77- if randint (1 , 10 ) == 5 and i :
78- # get a random number for an image
79- image_number = randint (1 , 10 )
80- # open and resize the image using Pillow
81- imagepath = IMAGES / f'{ image_number } .jpg'
82- im = Image .open (str (imagepath ))
83- im = im .resize ((self .screen_x , self .screen_y - 150 ), Image .NEAREST )
84- # make the image a tkinter image
85- image = ImageTk .PhotoImage (im )
86- # update the cat data dict
87- cat_data .update ({"image" : image })
88- cat_data .update ({"jumpscare" : True })
89- else :
90- # set jumpscare to False because it isnt a jumpscare image
91- cat_data .update ({"jumpscare" : False })
92-
93- # get a url from The Cat API
94- async with self .session .get (self .config ['catapi' ]) as res :
95- data = await res .json ()
96- url = data [0 ]['url' ]
97- # get image data from that url
98- async with self .session .get (url ) as res :
99- image_bytes = await res .read ()
100- # open and the image in pillow
101- im = Image .open (io .BytesIO (image_bytes ))
102- im = im .resize ((400 , 440 ), Image .NEAREST )
103- # make the image a tkinter image
104- image = ImageTk .PhotoImage (im )
105- # update the cat data dict
106- cat_data .update ({"image" : image })
107-
108- # get a random name
109- async with self .session .get (self .config ['namefile' ]) as res :
110- data = await res .json ()
111- # get a random letter for the name
112- # Note: website doesn't have any b names which is why it is left out here
113- letter = choice (list ('acdefghijklmnopqrstuvwxyz' ))
114- # randomly choose a name from the json with that letter
115- cat = choice (data [letter ])
116- # update the cat data dict
117- cat_data .update ({"name" : cat ["name" ]})
118- cat_data .update ({"gender" : cat ["gender" ]})
119-
120- # get 5 random hobbies
121- async with self .session .get (self .config ['hobbiesfile' ]) as res :
122- text = await res .text ()
123- # split the raw text of hobbies into a list
124- all_hobbies = text .split ("\n " )
125- # get 5 of those hobbies
126- hobby_list = sample (all_hobbies , 5 )
127- # join those 5 hobbies into a bulleted list (string)
128- list_of_hobbies = "\n •" .join (hobby_list )
129- hobbies = f"Hobbies:\n •{ list_of_hobbies } "
130- # update the cat_data dict
131- cat_data .update ({"hobbies" : hobbies })
132-
133- # get a random age between 1 and 15 (avg lifespan of a cat)
134- age = str (randint (1 , 15 ))
135- # update the cat data dict
136- cat_data .update ({"age" : age })
137-
138- # get a random number of miles away between 1 and 5
139- miles = randint (1 , 5 )
140- location = f"{ miles } miles away"
141- # update the cat data dict
142- cat_data .update ({"location" : location })
143- self .cats .append (cat_data )
144-
14558 def all_children (self ):
14659 '''Used to get all children of the root window
14760
@@ -182,11 +95,11 @@ def new_image(self, cat=None):
18295 # if a dict wasn't passed to the function, get a dict from self.cats
18396 if not cat :
18497 try :
185- cat = self .cats .pop (0 )
98+ cat = self .cache . cats .pop (0 )
18699 except IndexError :
187100 # the cache is empty so refill it
188- self .loop .run_until_complete (self .get_cache ())
189- cat = self .cats .pop (0 )
101+ self .loop .run_until_complete (self .cache . refill ())
102+ cat = self .cache . cats .pop (0 )
190103
191104 # getting base cat variables from the dict
192105 image = cat ["image" ]
@@ -235,17 +148,17 @@ def new_image(self, cat=None):
235148 # make a button to allow the user to pass through the image
236149 # Note: since everyone likes scary monsters, only make a Like button
237150 tk .Button (
238- self .frame , text = "Like" , background = "green" ,
151+ self .frame , text = self . config [ 'like.text' ] , background = "green" ,
239152 command = self .new_image ).pack (side = tk .BOTTOM )
240153
241154 # image was not a jumpscare, don't do jumpscare things
242155 else :
243156 # setting up like and dislike buttons on opposite sides of the screen
244157 tk .Button (
245- self .frame , text = "Like" , background = "green" ,
158+ self .frame , text = self . config [ 'like.text' ] , background = "green" ,
246159 command = self .new_image ).pack (side = tk .RIGHT )
247160 tk .Button (
248- self .frame , text = "Dislike" , background = "red" ,
161+ self .frame , text = self . config [ 'dislike.text' ] , background = "red" ,
249162 command = self .new_image ).pack (side = tk .LEFT )
250163
251164 # defining button functions
0 commit comments