自动更新Vkontakte的个人资料照片

使用VK API更新Vkontakte的个人资料照片的代码。 考虑使用验证码并将代码上传到服务器以自动更新照片。

图片

所需的图书馆


安装vk_api库以与VK API for Python一起使用:pip install vk_api

Vk_api工作示例
import vk_api # ( )      vk_session = vk_api.VkApi('login', 'password') vk_session.auth() vk = vk_session.get_api() #       'Hello world!' vk.wall.post(message='Hello world!') 


导入必要的库

 import os import time import random import requests import vk_api from vk_api.utils import get_random_id from urllib import urlretrieve 

登入


使用API​​在VK中进行授权

 vk_session = vk_api.VkApi('+7999132****', '*********') vk_session.auth() vk = vk_session.get_api() 

上传图片


将图像上传到VKontakte服务器。
photos.getOwnerPhotoUploadServer()返回用于将主照片上传到用户页面的服务器地址。

 url = vk.photos.getOwnerPhotoUploadServer()['upload_url'] 

要将图像上传到VKontakte服务器,您需要通过生成带有photo字段的POST请求,将文件传输到上一段中获得的upload_url地址。

 #    images images = os.listdir("images") photo = [] for image in images: request = requests.post(url, files={'photo': open('images/'+image, 'rb')}).json() photo.append(request['photo']) 

保存服务器和哈希值。 您将需要他们来更新您的个人资料照片。

 server = request['server'] hash = request['hash'] 

个人资料照片更新


photos.saveOwnerPhoto()保存用户的照片。

 #       x = random.randint(0, len(photo)-1) vk.photos.saveOwnerPhoto(server = server, hash = hash, photo = photo[x]) 

更新照片后,带有此照片的帖子将添加到墙上。 如果您不删除这些帖子,则好友供稿将被有关您头像更新的帖子所阻塞。

 posts = vk.wall.get() post_id = posts["items"][0]["id"] vk.wall.delete(post_id = post_id) 

如果我们运行代码,那么我们个人资料的照片将被更新。
为了实现自动化,只需添加一个无休止的循环并在每次更新照片后稍加延迟即可。

 y = 0 while(True): x = random.randint(0, len(photo)-1) while(x == y): x = random.randint(0, len(photo)-1) y = x #x  y   ,      2   vk.photos.saveOwnerPhoto(server = server, hash = hash, photo = photo[x]) posts = vk.wall.get() post_id = posts["items"][0]["id"] vk.wall.delete(post_id = post_id) #   photos = vk.photos.getAll() if (photos['count']>1): photo_id = photos["items"][1]["id"] vk.photos.delete(photo_id = photo_id) time.sleep(60) # 60 . 

完整代码
 import os import time import random import requests import vk_api from vk_api.utils import get_random_id from urllib import urlretrieve vk_session = vk_api.VkApi('+7999132****', '***********') vk_session.auth() vk = vk_session.get_api() images = os.listdir("images") url = vk.photos.getOwnerPhotoUploadServer()['upload_url'] photo = [] for image in images: request = requests.post(url, files={'photo': open('images/'+image, 'rb')}).json() photo.append(request['photo']) server = request['server'] hash = request['hash'] y = 0 while(True): x = random.randint(0, len(photo)-1) while(x == y): x = random.randint(0, len(photo)-1) y = x vk.photos.saveOwnerPhoto(server = server, hash = hash, photo = photo[x]) posts = vk.wall.get() post_id = posts["items"][0]["id"] vk.wall.delete(post_id = post_id) photos = vk.photos.getAll() if (photos['count']>1): photo_id = photos["items"][1]["id"] vk.photos.delete(photo_id = photo_id) time.sleep(60) 


但是几十张个人资料照片更新后,将发生错误
验证码:需要验证码

让我们看看如何在VK API中使用验证码。

使用验证码


vk_api.VkApi()方法已经实现了验证码的工作。 除了登录名和密码外,还必须传输验证码处理功能captcha_handler。

更改vk_session

 vk_session = vk_api.VkApi('+7999132****', '**********', captcha_handler=captcha_handler) 

我们添加了captcha_handler(captcha)函数,该函数获取验证码地址,将验证码图像发送到用户的消息,并等待用户的验证码。

 def captcha_handler(captcha): #   captcha_url = captcha.get_url() #     urlretrieve(captcha_url, "captcha.jpg") #  (send_captcha() . ) key = send_captcha(captcha_url) #  ()  return captcha.try_again(key) 

向用户发送带有验证码图像的消息的功能。

用户不能从服务器访问消息方法,因此您需要创建一个组并获取令牌。

  1. 建立群组/公开页面
  2. 管理=>使用API​​ =>创建密钥
  3. 选择:
    • 允许应用访问社区消息
    • 允许应用访问社区照片
  4. 复制密钥
    您还必须在组设置中启用消息(控制=>消息)并启用消息(在组菜单中)

     def send_captcha(captcha_url): #token ()  token = "" vk_session = vk_api.VkApi(token = token) vk = vk_session.get_api() #   url = vk.photos.getMessagesUploadServer()['upload_url'] #     request = requests.post(url, files={'photo': open("captcha.jpg", 'rb')}).json() #  photo = vk.photos.saveMessagesPhoto(server=request['server'], photo = request['photo'], hash = request['hash']) attachment = 'photo{}_{}'.format(photo[0]['owner_id'], photo[0]['id']) #  vk.messages.send( user_id = _ID, attachment = attachment, random_id=get_random_id()) #  os.remove("captcha.jpg") #  key = '' while (key == ''): #     messages = vk.messages.getDialogs()['items'][0] #     ,     if 'attachments' not in messages['message'].keys(): key = messages['message']['body'] return key 

    如果我们运行代码,它将一直执行到我们中断其工作为止。 当需要输入验证码时,验证码图像将被发送到私人消息,并且在从该图像发送字符后,将继续更新个人资料照片。

    完整代码
     import os import time import random import requests import vk_api from vk_api.utils import get_random_id from urllib import urlretrieve def captcha_handler(captcha): captcha_url = captcha.get_url() urlretrieve(captcha_url, "captcha.jpg") key = send_captcha(captcha_url) print(key) return captcha.try_again(key) def send_captcha(captcha_url): token = "" vk_session = vk_api.VkApi(token = token) vk = vk_session.get_api() url = vk.photos.getMessagesUploadServer()['upload_url'] request = requests.post(url, files={'photo': open("captcha.jpg", 'rb')}).json() photo = vk.photos.saveMessagesPhoto(server=request['server'], photo = request['photo'], hash = request['hash']) attachment = 'photo{}_{}'.format(photo[0]['owner_id'], photo[0]['id']) vk.messages.send( user_id=_ID, attachment = attachment, random_id=get_random_id()) os.remove("captcha.jpg") key = '' while (key == ''): messages = vk.messages.getDialogs()['items'][0] if 'attachments' not in messages['message'].keys(): key = messages['message']['body'] return key vk_session = vk_api.VkApi('+7999132****', '*********', captcha_handler=captcha_handler) vk_session.auth() vk = vk_session.get_api() images = os.listdir("images") url = vk.photos.getOwnerPhotoUploadServer()['upload_url'] photo = [] for image in images: request = requests.post(url, files={'photo': open('images/'+image, 'rb')}).json() photo.append(request['photo']) server = request['server'] hash = request['hash'] y = 0 while(True): x = random.randint(0, len(photo)-1) while(x == y): x = random.randint(0, len(photo)-1) y = x vk.photos.saveOwnerPhoto(server = server, hash = hash, photo = photo[x]) posts = vk.wall.get() post_id = posts["items"][0]["id"] vk.wall.delete(post_id = post_id) photos = vk.photos.getAll() if (photos['count']>1): photo_id = photos["items"][1]["id"] vk.photos.delete(photo_id = photo_id) print("Successfully", x) time.sleep(60) 


    对于全天候代码执行,我使用VPS托管。 我加载图像并在服务器上执行代码。

    Github代码

    如有疑问,请在评论中或项目经理中写下。

Source: https://habr.com/ru/post/zh-CN464285/


All Articles