Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import requests
- import json
- from PIL import Image, ImageDraw, ImageFont
- import textwrap
- import re
- import vk
- from vk.exceptions import VkAPIError
- import os
- from io import BytesIO
- import sys
- class VkAdvance:
- api = None
- graffiti = None
- def __init__(self, api):
- self.api = api
- @classmethod
- def login(cls, as_mobile=False, auth_fp='auth.txt', token_fp='token.txt'):
- if not os.path.exists(token_fp):
- open(token_fp, 'w').close()
- with open(token_fp) as token_file:
- vk_token = token_file.read()
- session = vk.Session(vk_token)
- api = vk.API(session, v=5.65)
- try:
- api.messages.get(count=1)
- return cls(api=api)
- except VkAPIError:
- with open(auth_fp, 'r') as auth_file:
- auth = auth_file.read()
- login, password = tuple(map(lambda x: x.strip(), auth.split('|')))
- if as_mobile:
- token = cls.get_token_as_mobile(login, password)
- with open(token_fp, 'w') as token_file:
- token_file.write(token)
- session = vk.Session(token)
- else:
- session = vk.AuthSession(user_password=password,
- user_login=login,
- app_id=5726518,
- scope='wall,docs,messages,photos,music')
- with open(token_fp, 'w') as token_file:
- token_file.write(session.access_token)
- api = vk.API(session, v=5.65)
- return cls(api=api)
- def save_graffiti(self, graffiti):
- u = self.api.docs.getUploadServer(type='graffiti')['upload_url']
- r = requests.post(u, files={'file': ('graffiti.png', graffiti.getvalue(), 'image/png')})
- print(r.text)
- j = json.loads(r.text)
- print(j)
- g = self.api.docs.save(file=j['file'])[0]
- return 'doc%s_%s' % (g['owner_id'], g['id'])
- def send_text(self, peer_id, text='', font_color=(5, 5, 200), font='f1.ttf', font_size=50, post_text=''):
- if font[-3:] == 'ttf':
- font = ImageFont.truetype(font, font_size)
- else:
- font = ImageFont.FreeTypeFont(font, font_size)
- text = textwrap.wrap(text, width=20)
- x_text = 0
- y_text = 10
- for line in text:
- (x, y) = font.getsize(line)
- y_text += y
- if x > x_text:
- x_text = x
- text = '\n'.join(text)
- image = Image.new('RGBA', (x_text, y_text), (0, 0, 0, 0))
- draw = ImageDraw.Draw(image)
- draw.multiline_text((0, 6), text, spacing=10, font=font, fill=font_color)
- img_bytes = BytesIO()
- image.save(img_bytes, 'PNG')
- doc = self.save_graffiti(img_bytes)
- self.api.messages.send(peer_id=peer_id, message=post_text, attachment=doc)
- def send_to_comment(self, wall, graffiti):
- post = re.search(r'-?\d+_\d+', wall).group(0)
- owner_id, post_id = post.split('_')
- self.api.wall.createComment(owner_id=owner_id, post_id=post_id, attachments=graffiti)
- def send_to_chat(self, chat_id, graffiti):
- self.api.messages.send(chat_id=chat_id, attachment=graffiti)
- def send_to_im(self, user_id, graffiti):
- self.api.messages.send(user_id=user_id, attachment=graffiti)
- @staticmethod
- def get_token_as_mobile(login, password):
- print(login, password)
- auth = requests.post(
- "https://oauth.vk.com/token?scope=all&client_id=2274003&"
- "client_secret=hHbZxrka2uZ6jB1inYsH&2fa_supported=1&"
- "lang=ru&"
- "device_id=3b95e297b6eb8852&"
- "grant_type=password"
- "&username={0}"
- "&password={1}".format(login, password)).text
- try:
- return json.loads(str(auth))["access_token"]
- except KeyError:
- print(auth)
- raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement