Do not crash with malformed visitors.json files
If the visitors.json file does not contain a valid JSON file, reset it, and act as if it was empty.
This commit is contained in:
		
							parent
							
								
									f243391418
								
							
						
					
					
						commit
						acea6018a6
					
				
							
								
								
									
										32
									
								
								botany.py
									
									
									
									
									
								
							
							
						
						
									
										32
									
								
								botany.py
									
									
									
									
									
								
							| @ -229,20 +229,24 @@ class Plant(object): | ||||
|         visitors_this_check = [] | ||||
|         if os.path.isfile(visitor_filepath): | ||||
|             with open(visitor_filepath, 'r') as visitor_file: | ||||
|                 data = json.load(visitor_file) | ||||
|                 if data: | ||||
|                     for element in data: | ||||
|                         if element['user'] not in self.visitors: | ||||
|                             self.visitors.append(element['user']) | ||||
|                         if element['user'] not in visitors_this_check: | ||||
|                             visitors_this_check.append(element['user']) | ||||
|                         # prevent users from manually setting watered_time in the future | ||||
|                         if element['timestamp'] <= int(time.time()): | ||||
|                             guest_timestamps.append(element['timestamp']) | ||||
|                     try: | ||||
|                        self.update_visitor_db(visitors_this_check) | ||||
|                     except: | ||||
|                         pass | ||||
|                 try: | ||||
|                     data = json.load(visitor_file) | ||||
|                     if data: | ||||
|                         for element in data: | ||||
|                             if element['user'] not in self.visitors: | ||||
|                                 self.visitors.append(element['user']) | ||||
|                             if element['user'] not in visitors_this_check: | ||||
|                                 visitors_this_check.append(element['user']) | ||||
|                             # prevent users from manually setting watered_time in the future | ||||
|                             if element['timestamp'] <= int(time.time()): | ||||
|                                 guest_timestamps.append(element['timestamp']) | ||||
|                         try: | ||||
|                            self.update_visitor_db(visitors_this_check) | ||||
|                         except: | ||||
|                             pass | ||||
|                         with open(visitor_filepath, 'w') as visitor_file: | ||||
|                             visitor_file.write('[]') | ||||
|                 except: | ||||
|                     with open(visitor_filepath, 'w') as visitor_file: | ||||
|                         visitor_file.write('[]') | ||||
|         else: | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user