1- import asyncio
2- import sys
1+ import socket as dsocket
2+ from asyncio import sleep , new_event_loop , run , gather , create_task , set_event_loop
33from contextlib import suppress
4+ from dataclasses import dataclass
45from json import load
5- from random import choice
6+ from os import path , getcwd , listdir , chdir , remove , system , mkdir
7+ from os .path import exists
8+ from random import choice , randrange
69from re import findall
7- import os
10+ from shutil import copytree , rmtree , copyfile
11+ from ssl import create_default_context
12+ from sys import exit
13+ from typing import IO
14+ from urllib .parse import urlparse
15+ from zipfile import ZipFile
816
9- import aiohttp
10- import colorama
11- from colorama import Fore
1217from aioconsole import aprint , ainput
18+ from aiofiles import open as openfile
19+ from aiohttp import ClientSession
1320from bs4 import BeautifulSoup
21+ from colorama import Fore , init
22+ from requests import get
1423
15- colorama .init ()
24+ init ()
25+
26+
27+ class AutoUpdater :
28+ def __init__ (self , version ):
29+ self .version = version
30+ self .latest = self .get_latest ()
31+ self .this = getcwd ()
32+ self .file = "temp/latest.zip"
33+ self .folder = f"temp/latest_{ randrange (1_000_000 , 999_999_999 )} "
34+
35+ @dataclass
36+ class latest_data :
37+ version : str
38+ zip_url : str
39+
40+ def get_latest (self ):
41+ rjson = get ("https://api.github.com/repos/MatrixTM/MultiAccountGenerator/tags" ).json ()
42+ return self .latest_data (version = rjson [0 ]["name" ], zip_url = get (rjson [0 ]["zipball_url" ]).url )
43+
44+ @staticmethod
45+ def download (host , dPath , filename ):
46+ with dsocket .socket (dsocket .AF_INET , dsocket .SOCK_STREAM ) as sock :
47+ context = create_default_context ()
48+ with context .wrap_socket (sock , server_hostname = "api.github.com" ) as wrapped_socket :
49+ wrapped_socket .connect ((dsocket .gethostbyname (host ), 443 ))
50+ wrapped_socket .send (
51+ f"GET { dPath } HTTP/1.1\r \n Host:{ host } \r \n Accept: text/html,application/xhtml+xml,application/xml;q=0.9,file/avif,file/webp,file/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9\r \n \r \n " .encode ())
52+
53+ resp = b""
54+ while resp [- 4 :- 1 ] != b"\r \n \r " :
55+ resp += wrapped_socket .recv (1 )
56+ else :
57+ resp = resp .decode ()
58+ content_length = int (
59+ "" .join ([tag .split (" " )[1 ] for tag in resp .split ("\r \n " ) if "content-length" in tag .lower ()]))
60+ _file = b""
61+ while content_length > 0 :
62+ data = wrapped_socket .recv (2048 )
63+ if not data :
64+ print ("EOF" )
65+ break
66+ _file += data
67+ content_length -= len (data )
68+ with open (filename , "wb" ) as file :
69+ file .write (_file )
70+
71+ def update (self ):
72+ if not self .version == self .latest .version :
73+ rmtree ("temp" ) if exists ("temp" ) else ""
74+ mkdir ("temp" )
75+ print ("Updating Script..." )
76+ parsed = urlparse (self .latest .zip_url )
77+ self .download (parsed .hostname , parsed .path , self .file )
78+ ZipFile (self .file ).extractall (self .folder )
79+ print (exists (self .folder ))
80+ print (exists (listdir (self .folder )[0 ]))
81+ chdir ("{}/{}" .format (self .folder , listdir (self .folder )[0 ]))
82+ for files in listdir ():
83+ if path .isdir (files ):
84+ with suppress (FileNotFoundError ):
85+ rmtree ("{}/{}" .format (self .this , files ))
86+ copytree (files , "{}/{}" .format (self .this , files ))
87+ else :
88+ with suppress (FileNotFoundError ):
89+ remove ("{}/{}" .format (self .this , files ))
90+ copyfile (files , "{}/{}" .format (self .this , files ))
91+ rmtree ("../../../temp" )
92+ exit ("Run Script Again!" )
93+ return
94+ print ("Script is up to date!" )
1695
1796
1897class Generator :
1998 def __init__ (self ):
99+ self .version = "v1.1"
100+ AutoUpdater (self .version ).update ()
20101 self .config : dict = load (open ('config.json' ))
21- self .output = open (self .config ["output" ], "a+" )
22- self .tasks = []
23- self .useless_value = 0
24- self .colors = [
102+ self .output : IO = open (self .config ["output" ], "a+" )
103+ self .tasks : list = []
104+ self .colors : list = [
25105 Fore .LIGHTGREEN_EX ,
26106 Fore .LIGHTBLACK_EX ,
27107 Fore .LIGHTMAGENTA_EX ,
@@ -37,8 +117,8 @@ async def make_beautiful(self, text: str, reset=True) -> str:
37117
38118 return "" .join (tmp ) if not reset else "" .join (tmp ) + Fore .RESET
39119
40- async def banner (self ):
41- os . system ('cls||clear' )
120+ async def banner (self ) -> None :
121+ system ('cls||clear' )
42122 print (await self .make_beautiful ("""
43123 __ __ _ _ _ _____
44124 | \/ | | | | (_)/ ____|
@@ -48,46 +128,48 @@ async def banner(self):
48128 |_| |_|\__,_|\__|_|_|\_____|\___|_| |_|
49129 #MahsaAmini\n \n """ , False ))
50130
51- async def run (self ):
131+ async def run (self ) -> None :
52132 await self .banner ()
53133 for i in range (len (self .config ["services" ])):
54134 await aprint (f"{ i } - { self .config ['services' ][i ]} " )
55135 await aprint ("69 - exit\n " )
56136 inp = await ainput ("Select service to scrape url >> " )
57- sys . exit (1 ) if inp == "69" else inp # Exit if exit selected
137+ exit (1 ) if inp == "69" else inp # Exit if exit selected
58138 if int (inp ) <= len (self .config ['services' ]):
59139 inp = self .config ['services' ][int (inp )] # Change number to Name
60140 await self .banner ()
61141 else :
62142 await ainput ("Select valid Item\n Press Enter to Exit" )
63- sys . exit (1 )
143+ exit (1 )
64144 await aprint ("Creating tasks" )
65- for _ in range (self .config ["thread" ]):
145+ for i in range (self .config ["thread" ]):
66146 self .tasks .append (
67- asyncio . create_task (self .generate (self .config ['url' ][inp ], self .config ['selector' ][inp ] or inp )))
68- self . useless_value += 1
69- await aprint ( "%s Task Created!" % self . useless_value , end = " \r " )
70- await asyncio . sleep ( .1 )
147+ create_task (self .generate (i , self .config ['url' ][inp ], self .config ['selector' ][inp ] or inp )))
148+ await aprint ( "%s Task Created!" % i , end = " \r " )
149+ await sleep ( .1 )
150+ print ( )
71151
72- await asyncio . gather (* self .tasks )
152+ await gather (* self .tasks )
73153
74- async def generate (self , url : str , selector : str ) -> None :
154+ async def generate (self , worker , url : str , selector : str ) -> None :
75155 while True :
76156 with suppress (Exception ):
77- with open (self .config ["output" ], "a+" ) as file :
78- session = aiohttp .ClientSession ()
79- request = await session .post (choice (url ), data = {"gen" : "" }, timeout = self .config ["request-timeout" ] or 5 )
80- await session .close ()
81- outUrl = \
82- findall ("http://.*" , str (BeautifulSoup (await request .text (), "html.parser" ).select (selector )))[0 ]
83- await aprint (await self .make_beautiful (outUrl ))
84- file .write ("%s" % outUrl )
157+ async with openfile (self .config ["output" ], "a+" ) as file :
158+ async with ClientSession () as session :
159+ request = await session .post (choice (url ), data = {"gen" : "" },
160+ timeout = self .config ["request-timeout" ] or 5 )
161+ outUrl = \
162+ findall ("http://.*" ,
163+ str (BeautifulSoup (await request .text (), "html.parser" ).select (selector )))[
164+ 0 ]
165+ await aprint ("%s[%s] " % (Fore .LIGHTCYAN_EX , worker ) + await self .make_beautiful (outUrl ))
166+ await file .write ("%s" % outUrl )
85167
86168
87169if __name__ == '__main__' :
88- loop = asyncio . new_event_loop ()
89- asyncio . set_event_loop (loop )
170+ loop = new_event_loop ()
171+ set_event_loop (loop )
90172 try :
91- asyncio . run (Generator ().run ())
173+ run (Generator ().run ())
92174 except KeyboardInterrupt :
93175 pass
0 commit comments