Browse Source

Moved to a separate repo; added command line arguments support

main
Unbewohnte 3 years ago
commit
6a074005c3
  1. 8
      LICENSE
  2. 16
      README.md
  3. 90
      vis.py

8
LICENSE

@ -0,0 +1,8 @@
Copyright © 2021,2022 Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz))
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.

16
README.md

@ -0,0 +1,16 @@
# vis - VkImageSender
## A tiny script to send images to a VK user or a group chat
# Usage
`./vis.py [ARGS]`
or
`python3 vis.py [ARGS]`
## Arguments
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)

90
vis.py

@ -0,0 +1,90 @@
#!/usr/bin/env python3
'''
Copyright © 2021,2022 Kasyanov Nikolay Alexeevich (Unbewohnte (me@unbewohnte.xyz))
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.
'''
import vk_api, os, random, datetime, argparse
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)
files.sort()
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:
try:
vk.messages.send(user_id = ID ,message=MESSAGE,
attachment = 'photo{}_{}'.format(upload_img['owner_id'],upload_img['id']),
random_id = 0)
except Exception as e:
print(e)
continue
elif IS_GROUP_CHAT == True:
try:
vk.messages.send(chat_id = ID, message=MESSAGE,
attachment='photo{}_{}'.format(upload_img['owner_id'],upload_img['id']),
random_id = 0)
except Exception as e:
print(e)
continue
counter += 1
# giving API a bit of rest
sleep(random.uniform(3.0,4.0))
pass
if __name__ == '__main__':
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument("imgs_dir_path", type=str, default=".")
arg_parser.add_argument("id", type=int, default=0)
arg_parser.add_argument("is_group_chat", type=bool, default=False)
arg_parser.add_argument("token", type=str, default="")
args = arg_parser.parse_args()
sendImages(img_dir = args.imgs_dir_path, ID = args.id, IS_GROUP_CHAT = args.is_group_chat, TOKEN = args.token)
pass
Loading…
Cancel
Save