def publish_items_db(media_items, lang, headers): """Publish all items but it check if the post exists, in this case, it will update the post. :param media_items: List of items to will publish """ """Define all variables""" """Create session""" _session = app.apps.get("db_sqlalchemy")() """For each novels do to the following""" for _item in media_items: _published_episodes = [] """For each new episode do to the following""" for _episode in _item['new_episodes']: """If the posts doesn't exists, create the post""" """Upload cover""" _cover = images.upload_images_from_urls( urls=[_episode['cover']], # watermark_code=watermark_code ) """Get media type""" media_type = media.get_media_type(_episode['media_type']) """Prepare all chapters""" _media_db = Media( media_id_reference=_episode['media_id'], collection_id_reference=_episode['collection_id'], number=_episode['episode_number'], title=_episode['title'], url=_episode['url'], cover=_cover['data']['images'][-1]['link'], serie=_item['serie'], media_type=media_type, status=constants.STATUS['initial'], ) """Save chapters in database""" _session.add(_media_db) _session.flush() _published_episodes.append(_media_db.to_dict(), ) _item['published'] = _published_episodes """Save in database""" _session.commit() """Close session""" _session.close() """Return the posts list""" return media_items