|
8 | 8 | """ |
9 | 9 |
|
10 | 10 |
|
| 11 | +STORY_ALLOWED_LINK_TEXTS = [ |
| 12 | + 'to_store', 'vote', 'more', 'book', 'order', |
| 13 | + 'enroll', 'fill', 'signup', 'buy', 'ticket', |
| 14 | + 'write', 'open', 'learn_more', 'view', 'go_to', |
| 15 | + 'contact', 'watch', 'play', 'install', 'read' |
| 16 | +] |
| 17 | + |
| 18 | + |
11 | 19 | class VkUpload(object): |
12 | 20 | """ Загрузка файлов через API (https://vk.com/dev/upload_files) """ |
13 | 21 |
|
@@ -385,6 +393,68 @@ def photo_cover(self, photo, group_id, |
385 | 393 |
|
386 | 394 | return response |
387 | 395 |
|
| 396 | + def story(self, file, file_type, add_to_news=True, user_ids=None, |
| 397 | + reply_to_story=None, link_text=None, |
| 398 | + link_url=None, group_id=None): |
| 399 | + """ Загрузка истории |
| 400 | +
|
| 401 | + :param file: путь к изображению, гифке или видео или file-like объект |
| 402 | + :param file_type: тип истории (photo или video) |
| 403 | + :param add_to_news: размещать ли историю в новостях |
| 404 | + :param user_ids: идентификаторы пользователей, которые будут видеть историю |
| 405 | + :param reply_to_story: идентификатор истории, в ответ на которую создается новая |
| 406 | + :param link_text: текст ссылки для перехода из истории |
| 407 | + :param link_url: адрес ссылки для перехода из истории |
| 408 | + :param group_id: идентификатор сообщества, в которое должна быть загружена история |
| 409 | + """ |
| 410 | + |
| 411 | + if user_ids is None: |
| 412 | + user_ids = [] |
| 413 | + |
| 414 | + if file_type == 'photo': |
| 415 | + method = 'stories.getPhotoUploadServer' |
| 416 | + elif file_type == 'video': |
| 417 | + method = 'stories.getVideoUploadServer' |
| 418 | + else: |
| 419 | + raise ValueError('type should be either photo or video') |
| 420 | + |
| 421 | + if not add_to_news and not user_ids: |
| 422 | + raise ValueError('Either add_to_news or user_ids param is required') |
| 423 | + |
| 424 | + if (link_text or link_url) and not group_id: |
| 425 | + raise ValueError('Link params available only for communities') |
| 426 | + |
| 427 | + if (not link_text) != (not link_url): |
| 428 | + raise ValueError('Either both link_text and link_url or neither one are required') |
| 429 | + |
| 430 | + if link_text and link_text not in STORY_ALLOWED_LINK_TEXTS: |
| 431 | + raise ValueError('Invalid link_text') |
| 432 | + |
| 433 | + if link_url and not link_url.startswith('https://vk.com'): |
| 434 | + raise ValueError('Only internal https://vk.com links are allowed for link_url') |
| 435 | + |
| 436 | + if link_url and len(link_url) > 2048: |
| 437 | + raise ValueError('link_url is too long. Max length - 2048') |
| 438 | + |
| 439 | + values = { |
| 440 | + 'add_to_news': int(add_to_news), |
| 441 | + 'user_ids': ','.join(map(str,user_ids)), |
| 442 | + 'reply_to_story': reply_to_story, |
| 443 | + 'link_text': link_text, |
| 444 | + 'link_url': link_url, |
| 445 | + 'group_id': group_id |
| 446 | + } |
| 447 | + |
| 448 | + url = self.vk.method( |
| 449 | + method, values |
| 450 | + )['upload_url'] |
| 451 | + |
| 452 | + photo_files = open_files(file, key_format='file') |
| 453 | + response = self.vk.http.post(url, files=photo_files) |
| 454 | + close_files(photo_files) |
| 455 | + |
| 456 | + return response |
| 457 | + |
388 | 458 |
|
389 | 459 | def open_files(paths, key_format='file{}'): |
390 | 460 | if not isinstance(paths, list): |
|
0 commit comments