Skip to content

Commit eb4dead

Browse files
hdk5python273
authored andcommitted
Add VkUpload.story (#119)
1 parent c9dff98 commit eb4dead

1 file changed

Lines changed: 70 additions & 0 deletions

File tree

vk_api/upload.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
"""
99

1010

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+
1119
class VkUpload(object):
1220
""" Загрузка файлов через API (https://vk.com/dev/upload_files) """
1321

@@ -385,6 +393,68 @@ def photo_cover(self, photo, group_id,
385393

386394
return response
387395

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+
388458

389459
def open_files(paths, key_format='file{}'):
390460
if not isinstance(paths, list):

0 commit comments

Comments
 (0)