From 8a943f0e21d59255e1d4374afb3c903b1122d565 Mon Sep 17 00:00:00 2001 From: Nikolay Kasyanov <65883674+Unbewohnte@users.noreply.github.com> Date: Thu, 27 May 2021 09:02:22 +0300 Subject: [PATCH] Add files via upload --- vkImageSender/LICENSE | 21 ++++++++++++ vkImageSender/README.md | 9 +++++ vkImageSender/VkImageSender.py | 61 ++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 vkImageSender/LICENSE create mode 100644 vkImageSender/README.md create mode 100644 vkImageSender/VkImageSender.py diff --git a/vkImageSender/LICENSE b/vkImageSender/LICENSE new file mode 100644 index 0000000..374be12 --- /dev/null +++ b/vkImageSender/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2021 Unbewohnte | Nikolay Kasyanov + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/vkImageSender/README.md b/vkImageSender/README.md new file mode 100644 index 0000000..c5c0d3a --- /dev/null +++ b/vkImageSender/README.md @@ -0,0 +1,9 @@ +# VkImageSender +## A tiny script to send images to a user in VK + +# Usage +## there is only one core function `sendImages` that takes 4 parameters: +1. img_dir (str) - path to the directory with images that you want to send +2. ID (int) - id of a person/group chat; the script will send images there +3. IS_GROUP_CHAT (bool) - self-explanatory, api has a little difference in handling group chats +4. TOKEN (str) - your API token; (see: https://vk.com/dev/access_token) \ No newline at end of file diff --git a/vkImageSender/VkImageSender.py b/vkImageSender/VkImageSender.py new file mode 100644 index 0000000..707b450 --- /dev/null +++ b/vkImageSender/VkImageSender.py @@ -0,0 +1,61 @@ +import vk_api,os,random +from vk_api import VkUpload +from time import sleep + + +def has_image_extention(filename): + filename = filename.lower() + + image_extentions = ["jpg","png","bmp","jpeg"] + for image_extention in image_extentions: + if image_extention in filename: + return True + + return False + +def sendImages(img_dir = ".", ID = 0, IS_GROUP_CHAT = False, TOKEN = ""): + vk = vk_api.VkApi(token = TOKEN).get_api() + + files = os.listdir(img_dir) + counter = 1 + for filename in files: + + if has_image_extention(filename) == False: + # file is probably not an image, so skipping + continue + + # full path to the image + path_to_image = img_dir + filename + + # upload via API + upload = VkUpload(vk) + upload_img = upload.photo_messages(photos = path_to_image)[0] + + # each message will contain "Counter : {number_of_}" + MESSAGE = "Counter : {}".format(counter) + # sending + print("{}: Sending {}...".format(counter,filename)) + + if IS_GROUP_CHAT == False: + vk.messages.send(user_id = ID ,message=MESSAGE, + attachment = 'photo{}_{}'.format(upload_img['owner_id'],upload_img['id']), + random_id = 0) + elif IS_GROUP_CHAT == True: + vk.messages.send(chat_id = ID, message=MESSAGE, + attachment='photo{}_{}'.format(upload_img['owner_id'],upload_img['id']), + random_id = 0) + counter += 1 + + # giving a bit of a rest to VK + sleep(random.uniform(3.0,4.0)) + pass + + +if __name__ == '__main__': + images = '' + token = '' + id = 0 + + sendImages(img_dir = images, ID = id, IS_GROUP_CHAT = False, TOKEN = token) + + pass