Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.39 KB | None | 0 0
  1. #! /usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. from flask import Flask, request, json
  4. from time import sleep
  5. from threading import Thread
  6. from pydub import AudioSegment
  7. import threading
  8. import vk_api
  9. import random
  10. import re
  11. import time
  12. import shutil
  13. import sqlite3
  14. import requests
  15. import datetime
  16. import hashlib
  17. import os
  18. import struct
  19. import wave
  20.  
  21. vk = vk_api.VkApi(token="f3bed5bc7d5f7be64512d0e36cdf8e1048475074e068902f72b58f94c8ad8ca23325201b2d005c6fc7607")
  22. vkUser =vk_api.VkApi(token="")
  23. app = Flask(__name__)
  24.  
  25. conn = sqlite3.connect("vkDB.db", check_same_thread=False) # ��� :memory: ����� ��������� � RAM
  26. cursor = conn.cursor()
  27.  
  28. reg = []
  29.  
  30. def user_id(name):
  31.     time_now = datetime.datetime.now()
  32.    
  33.     text = hex(int(f"{time_now.hour}{time_now.minute}{time_now.second}{time_now.year}{time_now.month}{time_now.day}"))
  34.     return f"{name}_{text}
  35.  
  36. def sendAttach(attach):
  37.    vk.method("messages.send",
  38.    {
  39.    "peer_id": id,
  40.    "random_id": random.randint(1, 2147483647),
  41.    "attachment": attach
  42.    })
  43.  
  44. @app.route('/', methods = ["POST"])
  45. def main():
  46.    data = json.loads(request.data)
  47.    if data["type"] == "confirmation":
  48.        return "1b095f74"
  49.    if data["type"] == "message_new":
  50.        object = data["object"]
  51.        id = object["peer_id"]
  52.        msgID = object["id"]
  53.        body = object["text"]
  54.        fromID = object["from_id"]
  55.        attachments = object["attachments"]
  56.  
  57.  
  58.        def sendMessage(text):
  59.            vk.method("messages.send",
  60.            {
  61.            "peer_id": id,
  62.            "random_id": random.randint(1, 2147483647),
  63.            "message": text
  64.            })
  65.  
  66.        try:
  67.  
  68.            sql = "SELECT * FROM users WHERE user_id=" + str(id)
  69.            cursor.execute(sql)
  70.            r = cursor.fetchall()
  71.  
  72.            if body == "?":
  73.                sendMessage(len(r))
  74.  
  75.            if len(r) == 0:
  76.                try:
  77.                    sql = "INSERT INTO users VALUES (NULL,'"+ str(id) +"',NULL, 0,0,0)"
  78.                    cursor.execute(sql)
  79.                    conn.commit()
  80.                    sendMessage("Вы успешно зарегистрировались! \n")
  81.                except Exception as e:
  82.                    sendMessage(str(e))
  83.  
  84.            if body == "/test":
  85.                if len(object['reply_message']) != 0 :
  86.  
  87.                    if object['reply_message']['attachments'][0]['type'] == "audio_message":
  88.  
  89.                        link = object['reply_message']['attachments'][0]['audio_message']['link_mp3']
  90.  
  91.                        response = requests.get(link, stream=True)
  92.                        with open( str(id)+'.mp3', 'wb') as out_file:
  93.                            shutil.copyfileobj(response.raw, out_file)
  94.                        del response
  95.  
  96.                        sound = AudioSegment.from_mp3(""+str(id)+".mp3")
  97.                        sound.export(""+str(id)+".wav", format="wav")
  98.  
  99.  
  100.                        url = vk.method('docs.getMessagesUploadServer', {'type': 'audio_message', "peer_id":id})['upload_url']
  101.                        files = [('file', (""+str(id)+".wav", open(""+str(id)+".wav", 'rb')))]
  102.                        url2 = requests.post(url, files=files).text
  103.                        response = json.loads(url2)['file']
  104.  
  105.                        response2 = vk.method("docs.save",
  106.                        {
  107.                        "file":response
  108.                        })
  109.  
  110.                        doc_id = response2['audio_message']['id']
  111.                        owner_id = response2['audio_message']['owner_id']
  112.                        attach = "doc"+str(owner_id)+"_"+str(doc_id)
  113.                        sendMessage(str(attach))
  114.  
  115.                        vk.method("messages.send",
  116.                            {
  117.                            "peer_id": id,
  118.                            "random_id": random.randint(1, 2147483647),
  119.                            "attachment": attach
  120.                            })
  121.  
  122.                        os.remove(""+str(id)+".wav")
  123.  
  124.        except Exception as e:
  125.            vk.method("messages.send",
  126.            {
  127.            "peer_id": 379586207,
  128.            "random_id": random.randint(1, 2147483647),
  129.            "message": "ошибка, проверьте логи; " + repr(e)
  130.            })
  131.    return "ok"
  132. #if __name__ == "__main__":
  133. #    app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement