Browse Source

Add files via upload

main
Nikolay Kasyanov 3 years ago committed by GitHub
parent
commit
8a943f0e21
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 21
      vkImageSender/LICENSE
  2. 9
      vkImageSender/README.md
  3. 61
      vkImageSender/VkImageSender.py

21
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.

9
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)

61
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
Loading…
Cancel
Save