1414from PyMatcha .models .user import User
1515from PyMatcha .utils .errors import ConflictError
1616from randomuser import RandomUser
17+ from tqdm import tqdm
1718
1819FRANCE_GEOHASH_START = ("u0" , "gb" , "ez" , "sp" )
1920NATIONALITIES_PARAMS = {"nat" : "fr" }
2021
22+ # LATVIA_GEOHASH_START = ("d0", "d1", "d4", "d4")
23+ # NATIONALITIES_PARAMS = {"nat": "lv"}
24+
2125
2226def gen_datetime (min_year : int = 1900 , max_year : int = datetime .datetime .now ().year ) -> datetime .datetime :
2327 # generate a datetime in format yyyy-mm-dd hh:mm:ss.000000
@@ -27,6 +31,19 @@ def gen_datetime(min_year: int = 1900, max_year: int = datetime.datetime.now().y
2731 return start + (end - start ) * random .random ()
2832
2933
34+ def get_unsplash_image (gender ):
35+ link = None
36+ while True :
37+ r = requests .get (f"https://source.unsplash.com/featured/?{ gender } " )
38+ if r .status_code == 403 :
39+ sleep (1 )
40+ continue
41+ link = r .url
42+ if not Image .get (link = link ):
43+ break
44+ return link
45+
46+
3047def populate_users (amount = 150 , drop_user_table = False ):
3148 if drop_user_table :
3249 User .drop_table ()
@@ -36,7 +53,7 @@ def populate_users(amount=150, drop_user_table=False):
3653 except HTTPError :
3754 sleep (10 )
3855 users = RandomUser .generate_users (amount = amount , get_params = NATIONALITIES_PARAMS )
39- for user in users :
56+ for user in tqdm ( users ) :
4057 gender = random .choice (["male" , "female" , "other" ])
4158 if gender != "other" :
4259 if gender == user .get_gender ():
@@ -87,7 +104,14 @@ def populate_users(amount=150, drop_user_table=False):
87104
88105 for tag in tags :
89106 Tag .create (name = tag , user_id = u .id )
90- image_url = requests .get (f"https://picsum.photos/seed/{ username } /1000/1000" ).url
107+
108+ if gender == "other" :
109+ # Change the gender because the profile picture needs to be of a person
110+ gender = random .choice (["male" , "female" ])
111+ image_url = get_unsplash_image ("man" if gender == "male" else "woman" )
112+ if not image_url :
113+ raise ValueError ("ERROR ON GETTING IMAGE FROM UNSPLASH" )
114+
91115 Image .create (user_id = u .id , link = image_url , is_primary = True )
92116 except ConflictError :
93117 pass # Pass on the conflict error, this user wont be created because the username is taken. Who cares ?
0 commit comments