def upload_image(self, image: str, caption: str, publish_date: Optional[date]) -> int: data = { 'file': open(image, 'rb'), } payload = { 'caption': caption, 'title': caption, } if publish_date: payload['date'] = datetime(year=publish_date.year, month=publish_date.month, day=publish_date.day).isoformat() resp = self._session.post(url=self._api_url + '/media', files=data, data=payload) resp.raise_for_status() image_id = resp.json()['id'] notice('Imported image {}. {}: {}'.format(image_id, image, caption)) return image_id
def remove_year_items(self, year: int) -> None: date_from, date_to = self._api.get_year_date_range(year=year) for item_type in ['posts', 'pages']: items = self._api.get_items(item_type=item_type, date_from=date_from, date_to=date_to) notice('{} items to remove: {}'.format(item_type, len(items))) for item_id in [item['id'] for item in items]: self._api.remove_items(item_type=item_type, item_id=item_id)
def create_year_page(self, year: int) -> None: date_from, date_to = self._api.get_year_date_range(year=year) posts = self._api.get_items(item_type='posts', date_from=date_from, date_to=date_to) posts = sorted(posts, key=lambda item: item['date']) categories = self._api.get_items(item_type='categories', search='{}_'.format(year)) category_planned = _get_planned_category(categories=categories) category_not_planned = _get_not_planned_category(categories=categories) posts_planned = [ post for post in posts if category_planned in post['categories'] ] posts_not_planned = [] if not category_not_planned \ else [post for post in posts if category_not_planned in post['categories']] events_planned = [ EventLink(event_number=i + 1, title=post['title']['rendered'], link=post['link'], date=format_iso_date(post['date'])) for i, post in enumerate(posts_planned) ] events_not_planned = [ EventLink(event_number=i + 1, title=post['title']['rendered'], link=post['link'], date=format_iso_date(post['date'])) for i, post in enumerate(posts_not_planned) ] page = WordPressPage() page.post_status = 'publish' page.title = 'Akcie {}'.format(year) page.content = get_year_page_content( events_planned=events_planned, events_non_planned=events_not_planned) page.date = datetime(year=year, month=1, day=1) page_id = self._client.call(NewPost(page)) notice('Created page {} for year {}'.format(page_id, year))
def create_event_post(self, event: Event) -> None: images = self.get_event_photos(year=event.year, event_number=event.event_number, is_planned=event.is_planned) post = WordPressPost() post.post_status = 'publish' post.title = event.title post.content = event.get_content(images=images) post_date = event.date if post_date: post.date = datetime(year=post_date.year, month=post_date.month, day=post_date.day) post.terms_names = _get_tags(year=event.year, is_planned=event.is_planned) post_id = self._client.call(NewPost(post)) notice('Imported post {}. {}'.format(post_id, event))
def remove_items(self, item_type, item_id) -> None: resp = self._session.delete(url=self._api_url + '/{}/{}'.format(item_type, item_id)) resp.raise_for_status() notice('removed {} with id {}'.format(item_type, item_id))