def parse(response): root = ET.fromstring(response) movies = [] schedules = [] updates = [] for program in root \ .find('sapo:GetChannelByDateIntervalResult', ns) \ .find('sapo:Programs', ns) \ .findall('sapo:Program', ns): sapo_id = program.find('sapo:Id', ns).text sapo_title = program.find('sapo:Title', ns).text sapo_title = sapo_title.replace('(V.O.)', '').replace('(V.P.)', '').strip() sapo_description = program.find('sapo:Description', ns).text if _validate_movie(sapo_title): # Check if movie is saved under other movie entry movie = _resolve_movie(sapo_id, sapo_title, sapo_description) # Movie was successfully resolved if movie is not None: sapo_id = movie.sapo_id # Adding id alias if movie.sapo_id != sapo_id and sapo_id not in movie.alias_ids: movie.alias_ids.append(sapo_id) updates.append(movie) # Adding title alias if movie.sapo_title != sapo_title and sapo_title not in movie.alias_titles: movie.alias_titles.append(sapo_title) updates.append(movie) # Movie already in the list of movies to be added elif any(m.sapo_id == sapo_id for m in movies): pass # Getting alias_ids for movies not yet in persisted elif any(sapo_title.lower() == m.sapo_title.lower() for m in movies): movie = next(m for m in movies if sapo_title.lower() == m.sapo_title.lower()) movie.alias_ids.append(sapo_id) # Otherwise add new one else: movie = Movie() movie.sapo_id = sapo_id movie.sapo_title = sapo_title movie.sapo_description = sapo_description movies.append(movie) schedule = Schedule() schedule.sapo_id = sapo_id schedule.sapo_channel = root \ .find('sapo:GetChannelByDateIntervalResult', ns) \ .find('sapo:Sigla', ns).text schedule.sapo_start_datetime = datetime.datetime.strptime( program.find('sapo:StartTime', ns).text, '%Y-%m-%d %H:%M:%S') schedule.sapo_end_datetime = datetime.datetime.strptime( program.find('sapo:EndTime', ns).text, '%Y-%m-%d %H:%M:%S') schedule.sapo_duration = program.find('sapo:Duration', ns).text schedules.append(schedule) return movies, schedules, updates
# sapo_duration = schedule_json['duration'] # # if not sapo_duration: # raise Exception('Invalid option') # else: # if 'duration' in schedule_json: # db.schedule.update({'_id': schedule._id}, {'$unset': {'duration': ""}}) # # print(sapo_start_datetime) # # print(sapo_end_datetime) # # print(sapo_duration) # db.schedule.update({'_id': schedule._id}, {'$set': {'sapo_start_datetime': sapo_start_datetime}}) # db.schedule.update({'_id': schedule._id}, {'$set': {'sapo_end_datetime': sapo_end_datetime}}) # db.schedule.update({'_id': schedule._id}, {'$set': {'sapo_duration': sapo_duration}}) schedule = Schedule() schedule.sapo_id = '123' schedule.sapo_channel = 'ABC' schedule.sapo_start_datetime = datetime.datetime.now() schedule.sapo_end_datetime = datetime.datetime.now() schedule.sapo_duration = '12345' serialized = json_util.loads(json_util.dumps(schedule.__dict__)) print(serialized) # db.schedule.insert(serialized) s = Schedule( json.loads(json_util.dumps( db.schedule.find_one({'sapo_id': schedule.sapo_id})), object_hook=json_util.object_hook)) print( ms.exists_schedule_in_db(s.sapo_id, s.sapo_channel, s.sapo_start_datetime))