11import tkinter as tk
22from PIL import Image , ImageTk
33import io
4- from random import randint , choice
4+ from random import randint , choice , sample
55import asyncio
66import time
77import aiohttp
@@ -35,44 +35,47 @@ async def get_cache(self):
3535 if not self .session :
3636 self .session = aiohttp .ClientSession ()
3737 for _ in range (10 ):
38+ cat_data = dict ()
3839 if randint (0 ,9 ) == 9 :
3940 image_number = randint (1 ,10 )
4041 im = Image .open (os .path .join (self .dir , os .path .join ("res" , os .path .join ("images" , f"{ image_number } .jpg" ))))
4142 im = im .resize ((400 , 400 ), Image .NEAREST )
4243 image = ImageTk .PhotoImage (im )
43- async with self .session .get ("https://www.pawclub.com.au/assets/js/namesTemp.json" ) as res :
44- data = await res .json ()
45- names = choice (data ["a" ])
46- name = names ["name" ]
47- self .images .append ([image , True , name ])
44+ cat_data .update ({"image" : image })
45+ cat_data .update ({"jumpscare" : True })
4846 else :
47+ cat_data .update ({"jumpscare" : False })
4948 async with self .session .get ('https://api.thecatapi.com/v1/images/search' ) as res :
5049 data = await res .json ()
5150 url = data [0 ]['url' ]
5251 async with self .session .get (url ) as res :
5352 image_bytes = await res .read ()
5453 im = Image .open (io .BytesIO (image_bytes ))
55- im = im .resize ((400 , 400 ), Image .NEAREST )
54+ im = im .resize ((400 , 450 ), Image .NEAREST )
5655 image = ImageTk .PhotoImage (im )
57- async with self .session .get ("https://www.pawclub.com.au/assets/js/namesTemp.json" ) as res :
58- data = await res .json ()
59- letter = choice (list ('acdefghijklmnopqrstuvwxyz' ))
60- names = choice (data [letter ])
61- name = names ["name" ]
62- self .images .append ([image , False , name ])
56+ cat_data .update ({"image" :image })
57+ async with self .session .get ("https://www.pawclub.com.au/assets/js/namesTemp.json" ) as res :
58+ data = await res .json ()
59+ letter = choice (list ('acdefghijklmnopqrstuvwxyz' ))
60+ cat = choice (data [letter ])
61+ cat_data .update ({"name" :cat ["name" ]})
62+ cat_data .update ({"gender" :cat ["gender" ]})
63+ async with self .session .get ("https://gist.githubusercontent.com/mbejda/453fdb77ef8d4d3b3a67/raw/e8334f09109dc212892406e25fdee03efdc23f56/hobbies.txt" ) as res :
64+ text = await res .text ()
65+ all_hobbies = text .split ("\n " )
66+ hobby_list = sample (all_hobbies , 5 )
67+ list_of_hobbies = "\n •" .join (hobby_list )
68+ hobbies = f"Hobbies:\n •{ list_of_hobbies } "
69+ cat_data .update ({"hobbies" :hobbies })
70+ age = str (randint (1 ,15 ))
71+ cat_data .update ({"age" :age })
72+ miles = randint (1 ,5 )
73+ location = f"{ miles } miles away"
74+ cat_data .update ({"location" :location })
75+ self .images .append (cat_data )
6376 print (time .time ()- t )
6477
6578
66- async def get_bio (self ):
67- #code to get bio goes here
68- #probably want to return a dict of some sort
69- pass
70-
71-
72- def bio (self ):
73- self .loop .run_until_complete (self .get_bio ())
74-
75-
7679 def all_children (self ):
7780 children = self .root .winfo_children ()
7881 for item in children :
@@ -81,36 +84,57 @@ def all_children(self):
8184 return children
8285
8386
84- def new_image (self ):
87+ def new_image (self , cat = None ):
8588 widget_list = self .all_children ()
8689 for item in widget_list :
8790 item .pack_forget ()
8891 self .frame = tk .Frame (self .root , bg = "black" )
89- try :
90- image , jumpscare , cat_name = self .images .pop (0 )
91- except IndexError :
92- self .loop .run_until_complete (self .get_cache ())
93- image , jumpscare , cat_name = self .images .pop (0 )
94- name = tk .Text (self .frame , width = 40 , height = 1 )
95- name .tag_configure ("center" , justify = tk .CENTER )
96- name .insert ("1.0" , cat_name )
97- name .tag_add ("center" , "1.0" , tk .END )
98- name .configure (state = "disabled" )
99- name .pack (side = tk .TOP )
92+ if not cat :
93+ try :
94+ cat = self .images .pop (0 )
95+ except IndexError :
96+ self .loop .run_until_complete (self .get_cache ())
97+ cat = self .images .pop (0 )
98+ cat_name = cat ["name" ]
99+ image = cat ["image" ]
100+ jumpscare = cat ["jumpscare" ]
101+ gender = cat ["gender" ].capitalize ()
102+ hobbies = cat ["hobbies" ]
103+ age = cat ["age" ]
104+ location = cat ["location" ]
105+ if not jumpscare :
106+ name = tk .Text (self .frame , width = 40 , height = 1 )
107+ name .tag_configure ("center" , justify = tk .CENTER )
108+ name .insert ("1.0" , cat_name )
109+ name .tag_add ("center" , "1.0" , tk .END )
110+ name .configure (state = "disabled" )
111+ name .pack (side = tk .TOP )
100112 tk .Label (self .frame , image = image ).pack (side = tk .TOP )
101- tk .Button (self .frame , text = "Like" , background = "green" , command = self .new_image ).pack (side = tk .RIGHT )
102- tk .Button (self .frame , text = "Dislike" , background = "red" , command = self .new_image ).pack (side = tk .LEFT )
103- def get_bio ():
104- widget_list = self .all_children ()
105- for item in widget_list :
106- item .pack_forget ()
107- self .frame = tk .Frame (self .root , bg = "black" )
108- full_bio = self .loop .run_until_complete (self .get_bio ())
109- bio = tk .Text (self .frame )
110- tk .Button (self .frame , text = "Bio" , background = "blue" , command = get_bio ).pack (side = tk .BOTTOM )
111113 if jumpscare :
112114 mixer .music .load (os .path .join (self .dir , os .path .join ("res" , os .path .join ("sounds" , "jumpscare.mp3" ))))
113115 mixer .music .play ()
116+ tk .Button (self .frame , text = "Like" , background = "green" , command = self .new_image ).pack (side = tk .BOTTOM )
117+ else :
118+ tk .Button (self .frame , text = "Like" , background = "green" , command = self .new_image ).pack (side = tk .RIGHT )
119+ tk .Button (self .frame , text = "Dislike" , background = "red" , command = self .new_image ).pack (side = tk .LEFT )
120+ def back_to_photo ():
121+ self .new_image (cat )
122+ def get_bio ():
123+ widget_list = self .all_children ()
124+ for item in widget_list :
125+ item .pack_forget ()
126+ self .frame = tk .Frame (self .root , bg = "black" )
127+ bio = tk .Text (self .frame )
128+ bio .insert (tk .END , f"Name: { cat_name } \n " )
129+ bio .insert (tk .END , f"Age: { age } \n " )
130+ bio .insert (tk .END , f"Gender: { gender } \n " )
131+ bio .insert (tk .END , f"Location: { location } \n " )
132+ bio .insert (tk .END , f"{ hobbies } \n " )
133+ bio .configure (state = "disabled" )
134+ bio .pack (side = tk .TOP )
135+ tk .Button (self .frame , text = "Back To Photo" , background = "blue" , command = back_to_photo ).pack (side = tk .BOTTOM )
136+ self .frame .pack ()
137+ tk .Button (self .frame , text = "Bio" , background = "blue" , command = get_bio ).pack (side = tk .BOTTOM )
114138 self .frame .pack ()
115139 self .root .mainloop ()
116140
0 commit comments