Advertisement
Guest User

Untitled

a guest
Jun 11th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.01 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import requests
  3. import json
  4. from PIL import Image, ImageDraw, ImageFont
  5. import textwrap
  6. import re
  7. import vk
  8. from vk.exceptions import VkAPIError
  9. import os
  10. from io import BytesIO
  11. import sys
  12.  
  13.  
  14. class VkAdvance:
  15.     api = None
  16.     graffiti = None
  17.  
  18.     def __init__(self, api):
  19.         self.api = api
  20.  
  21.     @classmethod
  22.     def login(cls, as_mobile=False, auth_fp='auth.txt', token_fp='token.txt'):
  23.  
  24.         if not os.path.exists(token_fp):
  25.             open(token_fp, 'w').close()
  26.  
  27.         with open(token_fp) as token_file:
  28.             vk_token = token_file.read()
  29.         session = vk.Session(vk_token)
  30.         api = vk.API(session, v=5.65)
  31.         try:
  32.             api.messages.get(count=1)
  33.             return cls(api=api)
  34.  
  35.         except VkAPIError:
  36.             with open(auth_fp, 'r') as auth_file:
  37.                 auth = auth_file.read()
  38.  
  39.             login, password = tuple(map(lambda x: x.strip(), auth.split('|')))
  40.             if as_mobile:
  41.                 token = cls.get_token_as_mobile(login, password)
  42.                 with open(token_fp, 'w') as token_file:
  43.                     token_file.write(token)
  44.                 session = vk.Session(token)
  45.             else:
  46.                 session = vk.AuthSession(user_password=password,
  47.                                          user_login=login,
  48.                                          app_id=5726518,
  49.                                          scope='wall,docs,messages,photos,music')
  50.                 with open(token_fp, 'w') as token_file:
  51.                     token_file.write(session.access_token)
  52.             api = vk.API(session, v=5.65)
  53.             return cls(api=api)
  54.  
  55.     def save_graffiti(self, graffiti):
  56.         u = self.api.docs.getUploadServer(type='graffiti')['upload_url']
  57.         r = requests.post(u, files={'file': ('graffiti.png', graffiti.getvalue(), 'image/png')})
  58.         print(r.text)
  59.         j = json.loads(r.text)
  60.         print(j)
  61.         g = self.api.docs.save(file=j['file'])[0]
  62.         return 'doc%s_%s' % (g['owner_id'], g['id'])
  63.  
  64.     def send_text(self, peer_id, text='', font_color=(5, 5, 200), font='f1.ttf', font_size=50, post_text=''):
  65.         if font[-3:] == 'ttf':
  66.             font = ImageFont.truetype(font, font_size)
  67.         else:
  68.             font = ImageFont.FreeTypeFont(font, font_size)
  69.         text = textwrap.wrap(text, width=20)
  70.         x_text = 0
  71.         y_text = 10
  72.         for line in text:
  73.             (x, y) = font.getsize(line)
  74.             y_text += y
  75.             if x > x_text:
  76.                 x_text = x
  77.         text = '\n'.join(text)
  78.         image = Image.new('RGBA', (x_text, y_text), (0, 0, 0, 0))
  79.         draw = ImageDraw.Draw(image)
  80.         draw.multiline_text((0, 6), text, spacing=10, font=font, fill=font_color)
  81.         img_bytes = BytesIO()
  82.         image.save(img_bytes, 'PNG')
  83.         doc = self.save_graffiti(img_bytes)
  84.         self.api.messages.send(peer_id=peer_id, message=post_text, attachment=doc)
  85.  
  86.     def send_to_comment(self, wall, graffiti):
  87.         post = re.search(r'-?\d+_\d+', wall).group(0)
  88.         owner_id, post_id = post.split('_')
  89.         self.api.wall.createComment(owner_id=owner_id, post_id=post_id, attachments=graffiti)
  90.  
  91.     def send_to_chat(self, chat_id, graffiti):
  92.         self.api.messages.send(chat_id=chat_id, attachment=graffiti)
  93.  
  94.     def send_to_im(self, user_id, graffiti):
  95.         self.api.messages.send(user_id=user_id, attachment=graffiti)
  96.  
  97.     @staticmethod
  98.     def get_token_as_mobile(login, password):
  99.         print(login, password)
  100.         auth = requests.post(
  101.             "https://oauth.vk.com/token?scope=all&client_id=2274003&"
  102.             "client_secret=hHbZxrka2uZ6jB1inYsH&2fa_supported=1&"
  103.             "lang=ru&"
  104.             "device_id=3b95e297b6eb8852&"
  105.             "grant_type=password"
  106.             "&username={0}"
  107.             "&password={1}".format(login, password)).text
  108.         try:
  109.             return json.loads(str(auth))["access_token"]
  110.         except KeyError:
  111.             print(auth)
  112.             raise
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement