-
Notifications
You must be signed in to change notification settings - Fork 0
/
photo.py
68 lines (47 loc) · 1.71 KB
/
photo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import json
import time
import os
import random
import requests
import cv2
PHOTO_PATH = "images/ph0"
def take_photo():
"""
Take photo and save this in 'image' folder
"""
cap = cv2.VideoCapture(0) # Open the default camera
if not cap.isOpened():
cap.open()
ret, img = cap.read()
if ret and img is not None:
cv2.imwrite(PHOTO_PATH + ".jpg", img)
def send_photo(vk, target_id):
"""
Send a photo to target
:param vk: the VkApiMethod(self) for using methods of API VK
:param target_id: user's id to send a photo
"""
data = vk.photos.getMessagesUploadServer()
upload_url = data["upload_url"]
files = {'photo': open(PHOTO_PATH + ".jpg", 'rb')}
response = requests.post(upload_url, files=files)
result = json.loads(response.text)
upload_result = vk.photos.saveMessagesPhoto(server=result["server"],
photo=result["photo"],
hash=result["hash"])
vk.messages.send(user_id=target_id,
attachment="photo" + str(vk.users.get()[0]["id"]) + "_" + str(upload_result[0]["id"]),
random_id=int(random.randint(1, 999999999)))
os.remove(PHOTO_PATH + ".jpg")
print("Фото отправлено")
def start_send_photos(vk, target_id, frequency_min):
"""
send photo in the infinite loop
:param vk: the VkApiMethod(self) for using methods of API VK
:param target_id: user's id to send a photo
:param frequency_min: frequency of sending photos (indicated in minutes)
"""
while True:
take_photo()
send_photo(vk, target_id)
time.sleep(60*int(frequency_min))