From a911a289ff548a6f19fed0ac3595361765d2018b Mon Sep 17 00:00:00 2001 From: "deepsource-autofix[bot]" <62050782+deepsource-autofix[bot]@users.noreply.github.com> Date: Sat, 24 Feb 2024 13:29:02 +0000 Subject: [PATCH] style: format code with Autopep8 and Black This commit fixes the style issues introduced in 8b47313 according to the output from Autopep8 and Black. Details: None --- bot/apps/accounts/views.py | 382 ++++++++++++++++++++----------------- func/get_ans.py | 99 ++++++---- func/get_updates.py | 88 +++++---- 3 files changed, 317 insertions(+), 252 deletions(-) diff --git a/bot/apps/accounts/views.py b/bot/apps/accounts/views.py index 143e06e..850efbd 100644 --- a/bot/apps/accounts/views.py +++ b/bot/apps/accounts/views.py @@ -12,206 +12,238 @@ a = Status.objects.all()[0] if a.work: - start_bot() + start_bot() + def index(request): - a = Status.objects.all()[0] - accounts_list = Account.objects.all() - return render(request, 'accounts/list.html', {'accounts_list': accounts_list, 'start': a.work}) + a = Status.objects.all()[0] + accounts_list = Account.objects.all() + return render( + request, "accounts/list.html", {"accounts_list": accounts_list, "start": a.work} + ) + def settings(request, account_id): - try: - a = Account.objects.get( id = account_id ) - except: - raise Http404('Аккаунт не найден') + try: + a = Account.objects.get(id=account_id) + except: + raise Http404("Аккаунт не найден") - return render(request, 'accounts/settings.html', {'account': a}) + return render(request, "accounts/settings.html", {"account": a}) def save(request, account_id): - try: - a = Account.objects.get( id = account_id ) - except: - raise Http404('Аккаунт не найден') - - try: - a.name = request.POST['name'] - except: - pass - - try: - a.mark = request.POST['mark'] - except: - pass - - try: - a.sleep = request.POST['sleep'] - except: - pass - - try: - a.status = request.POST['status'] - except: - pass - - try: - x = request.POST['auto_friends'] - a.auto_friends = True - except: - a.auto_friends = False - - try: - x = request.POST['ls_user'] - a.ls_user = True - except: - a.ls_user = False - - try: - x = request.POST['group_name'] - a.group_name = True - except: - a.group_name = False - - try: - x = request.POST['voice_bot'] - a.voice_bot = True - except: - a.voice_bot = False - - try: - x = request.POST['reply'] - a.reply = True - except: - a.reply = False - - - - a.save() - - base_name = request.POST['base_name'] - try: - with open(f"base/{base_name}") as f: - pass - a.base_name = base_name - a.save() - return HttpResponseRedirect( reverse('Accounts:settings', args=(a.id,)) ) - except: - print(base_name) - return render(request, 'accounts/settings.html', {'account': a, 'error_base': base_name}) - + try: + a = Account.objects.get(id=account_id) + except: + raise Http404("Аккаунт не найден") + + try: + a.name = request.POST["name"] + except: + pass + + try: + a.mark = request.POST["mark"] + except: + pass + + try: + a.sleep = request.POST["sleep"] + except: + pass + + try: + a.status = request.POST["status"] + except: + pass + + try: + x = request.POST["auto_friends"] + a.auto_friends = True + except: + a.auto_friends = False + + try: + x = request.POST["ls_user"] + a.ls_user = True + except: + a.ls_user = False + + try: + x = request.POST["group_name"] + a.group_name = True + except: + a.group_name = False + + try: + x = request.POST["voice_bot"] + a.voice_bot = True + except: + a.voice_bot = False + + try: + x = request.POST["reply"] + a.reply = True + except: + a.reply = False + + a.save() + + base_name = request.POST["base_name"] + try: + with open(f"base/{base_name}") as f: + pass + a.base_name = base_name + a.save() + return HttpResponseRedirect(reverse("Accounts:settings", args=(a.id,))) + except: + print(base_name) + return render( + request, "accounts/settings.html", {"account": a, "error_base": base_name} + ) + def new_account(request): - return render(request, 'accounts/new_account.html') + return render(request, "accounts/new_account.html") + def new_account_token(request): - return render(request, 'accounts/new_account_token.html') + return render(request, "accounts/new_account_token.html") + def new(request): - return render(request, 'accounts/new.html') + return render(request, "accounts/new.html") + def new_acc_login_or_token(request): - return render(request, 'accounts/new_acc_login_or_token.html') - + return render(request, "accounts/new_acc_login_or_token.html") + + def create_group(request): - - token = request.POST['longpoll_token'] - - id = request.POST['id'] - - a = requests.get('https://api.vk.com/method/groups.getLongPollServer', params={'lp_version': 3, 'v': 5.121 , 'access_token': token, 'group_id': id}) - - try: - ts = a.json()['response']['ts'] - except: - return render(request, 'accounts/new_account.html', {'error': True}) - - a = requests.get('https://api.vk.com/method/groups.getById', params={'access_token': token, 'group_id': id, 'v': 5.121}) - - name = a.json()['response'][0]['name'] - - a = Account(call=name, token=token, group=id) - - a.save() - - return render(request, 'accounts/settings.html', {'account': a}) - + token = request.POST["longpoll_token"] + + id = request.POST["id"] + + a = requests.get( + "https://api.vk.com/method/groups.getLongPollServer", + params={"lp_version": 3, "v": 5.121, "access_token": token, "group_id": id}, + ) + + try: + ts = a.json()["response"]["ts"] + except: + return render(request, "accounts/new_account.html", {"error": True}) + + a = requests.get( + "https://api.vk.com/method/groups.getById", + params={"access_token": token, "group_id": id, "v": 5.121}, + ) + + name = a.json()["response"][0]["name"] + + a = Account(call=name, token=token, group=id) + + a.save() + + return render(request, "accounts/settings.html", {"account": a}) + + def new_group(request): - return render(request, 'accounts/new_group.html') + return render(request, "accounts/new_group.html") + def create_account(request): - - login = request.POST['login'] - password = request.POST['password'] - - a = requests.get(f'https://api.vk.com/oauth/token?grant_type=password&client_id=2274003&client_secret=hHbZxrka2uZ6jB1inYsH&username={login}&password={password}') - try: - token = a.json()['access_token'] - except: - print(a.text) - return render(request, 'accounts/new_account.html', {'error': True}) - - a = requests.get('https://api.vk.com/method/account.getProfileInfo', params={'v': 5.121, 'access_token': token}) - - name = a.json()['response']['first_name'] - last_name = a.json()['response']['last_name'] - - a = Account(call=f'{name} {last_name}', token=token) - - a.save() - - return render(request, 'accounts/settings.html', {'account': a}) + login = request.POST["login"] + password = request.POST["password"] + + a = requests.get( + f"https://api.vk.com/oauth/token?grant_type=password&client_id=2274003&client_secret=hHbZxrka2uZ6jB1inYsH&username={login}&password={password}" + ) + try: + token = a.json()["access_token"] + except: + print(a.text) + return render(request, "accounts/new_account.html", {"error": True}) + + a = requests.get( + "https://api.vk.com/method/account.getProfileInfo", + params={"v": 5.121, "access_token": token}, + ) + + name = a.json()["response"]["first_name"] + last_name = a.json()["response"]["last_name"] + + a = Account(call=f"{name} {last_name}", token=token) + + a.save() + + return render(request, "accounts/settings.html", {"account": a}) def start(request): - - a = Status.objects.all()[0] - - accounts_list = Account.objects.all() - - if accounts_list: - - a.work = True - a.save() - - start_bot() - - return render(request, 'accounts/list.html', {'accounts_list': accounts_list, 'start': a.work}) - return render(request, 'accounts/list.html', {'accounts_list': accounts_list, 'start': a.work}) - - + a = Status.objects.all()[0] + + accounts_list = Account.objects.all() + + if accounts_list: + a.work = True + a.save() + + start_bot() + + return render( + request, + "accounts/list.html", + {"accounts_list": accounts_list, "start": a.work}, + ) + return render( + request, "accounts/list.html", {"accounts_list": accounts_list, "start": a.work} + ) + + def end(request): - - a = Status.objects.all()[0] - a.work = False - a.save() - - accounts_list = Account.objects.all() - - return render(request, 'accounts/list.html', {'accounts_list': accounts_list, 'start': a.work}) - + a = Status.objects.all()[0] + a.work = False + a.save() + + accounts_list = Account.objects.all() + + return render( + request, "accounts/list.html", {"accounts_list": accounts_list, "start": a.work} + ) + + def delete_yesno(request, account_id): - return render(request, 'accounts/delete_yesno.html', {'account_id': account_id}) - + return render(request, "accounts/delete_yesno.html", {"account_id": account_id}) + + def delete_account(request, account_id): - Account.objects.get(id=account_id).delete() - return index(request) + Account.objects.get(id=account_id).delete() + return index(request) + def create_account_token(request): - - token = request.POST['token'] - - a = requests.get('https://api.vk.com/method/account.getProfileInfo', params={'v': 5.121, 'access_token': token}) - - try: - name = a.json()['response']['first_name'] - last_name = a.json()['response']['last_name'] - except: - return render(request, 'accounts/new_account_token.html', {'error': True}) - - a = requests.get('https://api.vk.com/method/account.getProfileInfo', params={'v': 5.121, 'access_token': token}) - - a = Account(call=f'{name} {last_name}', token=token) - - a.save() - - return render(request, 'accounts/settings.html', {'account': a}) + token = request.POST["token"] + + a = requests.get( + "https://api.vk.com/method/account.getProfileInfo", + params={"v": 5.121, "access_token": token}, + ) + + try: + name = a.json()["response"]["first_name"] + last_name = a.json()["response"]["last_name"] + except: + return render(request, "accounts/new_account_token.html", {"error": True}) + + a = requests.get( + "https://api.vk.com/method/account.getProfileInfo", + params={"v": 5.121, "access_token": token}, + ) + + a = Account(call=f"{name} {last_name}", token=token) + + a.save() + + return render(request, "accounts/settings.html", {"account": a}) diff --git a/func/get_ans.py b/func/get_ans.py index 9deb84c..2670952 100644 --- a/func/get_ans.py +++ b/func/get_ans.py @@ -1,45 +1,64 @@ -import random, time +import random +import time from func.tell import * from func.regular import * from func.append_attach import * from func.randoms import * -def get_ans(message, base, commands, TOKEN, peer_id, group, tr_group, voice_bot, user_id, count_message, start_bot, regulars): - max_g = 0 - ans = [] - g = 0 - for f in commands: - if f[0].lower() == message.lower(): - ans = f[1] - break - else: - for f in base: - if f and len(f) > 1: - s1, s2 = message.lower(), f[0].lower() - max = len(s2) - if len(s1) > len(s2): - s1, s2 = s2, s1 - max = len(s2) - p = 0 - for i in range(len(s1)): - if s1[i] == s2[i]: - p += 1 - g = int(p/max*100) - if g > max_g: - ans = [f[1],] - max_g = g - if g == max_g: - ans.append(f[1]) - if ans: - ans = random.choice(ans) - if '@[' in ans: - ans = randoms(ans) - if 'https://vk.com/' in ans: - return append_attach(ans) - if (ans[0] == '{' and ans[-1] == '}') or voice_bot: - for h in regulars: - if h in ans.lower(): - ans = regular(ans, user_id, count_message, start_bot, TOKEN, regulars) - break - return '', tell(ans, TOKEN, peer_id, group, tr_group), True - return ans, '', False + +def get_ans( + message, + base, + commands, + TOKEN, + peer_id, + group, + tr_group, + voice_bot, + user_id, + count_message, + start_bot, + regulars, +): + max_g = 0 + ans = [] + g = 0 + for f in commands: + if f[0].lower() == message.lower(): + ans = f[1] + break + else: + for f in base: + if f and len(f) > 1: + s1, s2 = message.lower(), f[0].lower() + max = len(s2) + if len(s1) > len(s2): + s1, s2 = s2, s1 + max = len(s2) + p = 0 + for i in range(len(s1)): + if s1[i] == s2[i]: + p += 1 + g = int(p / max * 100) + if g > max_g: + ans = [ + f[1], + ] + max_g = g + if g == max_g: + ans.append(f[1]) + if ans: + ans = random.choice(ans) + if "@[" in ans: + ans = randoms(ans) + if "https://vk.com/" in ans: + return append_attach(ans) + if (ans[0] == "{" and ans[-1] == "}") or voice_bot: + for h in regulars: + if h in ans.lower(): + ans = regular( + ans, user_id, count_message, start_bot, TOKEN, regulars + ) + break + return "", tell(ans, TOKEN, peer_id, group, tr_group), True + return ans, "", False diff --git a/func/get_updates.py b/func/get_updates.py index a4f2d4c..1142140 100644 --- a/func/get_updates.py +++ b/func/get_updates.py @@ -1,41 +1,55 @@ -import requests, json +import requests +import json + def get_server(TOKEN, group): - while True: - try: - if group: - a = 'groups' - else: - a = 'messages' - paramss = {'lp_version': 3, 'v': 5.121 , 'access_token': TOKEN} - if group: - paramss.update({'group_id': group}) - a = requests.get('https://api.vk.com/method/' + a + '.getLongPollServer', params = paramss) - a = json.loads(a.text)['response'] - key = a['key'] - server = a['server'] - ts = a['ts'] - if server[:8] == 'https://': - server = server[8:] - return key, server, ts - except: - continue - + while True: + try: + if group: + a = "groups" + else: + a = "messages" + paramss = {"lp_version": 3, "v": 5.121, "access_token": TOKEN} + if group: + paramss.update({"group_id": group}) + a = requests.get( + "https://api.vk.com/method/" + a + ".getLongPollServer", params=paramss + ) + a = json.loads(a.text)["response"] + key = a["key"] + server = a["server"] + ts = a["ts"] + if server[:8] == "https://": + server = server[8:] + return key, server, ts + except: + continue + def get_upd(TOKEN, key, server, ts, group): - while True: - try: - b = requests.get(f'https://{server}', params = {'act': 'a_check', 'key': key, 'ts': ts, 'wait': 5, 'versions': 3, 'mode': 10}) - upd = json.loads(b.text) - if 'failed' in upd: - if upd['failed'] == 1: - ts = upd['ts'] - return get_upd(TOKEN, key, server, ts) - if upd['failed'] in (2,3): - key, server, ts = get_server(TOKEN, group) - return get_upd(TOKEN, key, server, ts) - ts = upd['ts'] - return upd['updates'], key, server, ts - except Exception as err: - print(err) - continue + while True: + try: + b = requests.get( + f"https://{server}", + params={ + "act": "a_check", + "key": key, + "ts": ts, + "wait": 5, + "versions": 3, + "mode": 10, + }, + ) + upd = json.loads(b.text) + if "failed" in upd: + if upd["failed"] == 1: + ts = upd["ts"] + return get_upd(TOKEN, key, server, ts) + if upd["failed"] in (2, 3): + key, server, ts = get_server(TOKEN, group) + return get_upd(TOKEN, key, server, ts) + ts = upd["ts"] + return upd["updates"], key, server, ts + except Exception as err: + print(err) + continue