Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import argparse
- import requests
- import time
- import math
- import zipfile
- import os
- import io
- from threading import Thread
- from random import choice
- from random import randint
- from string import digits
- from sys import exit
- isdone = 0
- files = {'global': 0}
- symbdict = 'abcdf' + digits
- Videos = ['.mp4', '.3gp']
- Photos = ['.jpg', '.png']
- Gifs = ['.gif']
- Audio = ['.mp3', '.amr']
- def sort(filename, type, mainpath):
- if type in Photos:
- os.rename(mainpath + filename, mainpath + 'Photos\\' + filename)
- elif type in Videos:
- os.rename(mainpath + filename, mainpath + 'Videos\\' + filename)
- elif type in Gifs:
- os.rename(mainpath + filename, mainpath + 'Gifs\\' + filename)
- elif type in Audio:
- os.rename(mainpath + filename, mainpath + 'Audio\\' + filename)
- else:
- os.rename(mainpath + filename, mainpath + 'Other\\' + filename)
- def unpack_archive(arch, path, name):
- with arch, zipfile.ZipFile(io.BytesIO(arch.content)) as fzip:
- fzip.extractall(path[:len(path)-1])
- i = 0
- for file in fzip.infolist():
- fname = file.filename
- newname = name + '_' + str(i) + fname[fname.find('.'):]
- os.rename(path + fname, path + newname)
- if sorting:
- sort(newname, fname[fname.find('.'):], path)
- i = i + 1
- fzip.close()
- def genp():
- return ''.join(choice(symbdict) for i in range(randint(4, 6)))
- parser = argparse.ArgumentParser(description='GOSMS downloader 3.0')
- parser.add_argument('-c', action="store", dest="count", default=5, type=int)
- parser.add_argument('-o', action="store", dest="out", default='')
- parser.add_argument('-t', action="store", dest="threads", default=1, type=int)
- parser.add_argument('--unpack', action="store_true", dest="unpack")
- parser.add_argument('-no', action='append', dest="filter", default=None)
- parser.add_argument('--sort', action="store_true", dest="sorting")
- args = parser.parse_args()
- count = args.count
- tcount = args.threads
- path = args.out
- unpack = args.unpack
- filter = args.filter
- sorting = args.sorting
- if path != '' and path[len(path):] != '\\':
- path = path + '\\'
- if not os.path.exists(path+'Videos'):
- os.makedirs(path+'Videos')
- if not os.path.exists(path+'Photos'):
- os.makedirs(path+'Photos')
- if not os.path.exists(path+'Gifs'):
- os.makedirs(path+'Gifs')
- if not os.path.exists(path+'Audio'):
- os.makedirs(path+'Audio')
- if not os.path.exists(path+'Other'):
- os.makedirs(path+'Other')
- if tcount > count:
- print("The count of threads should not exceed the url's count")
- exit()
- class dlThread(Thread):
- def __init__(self, task):
- Thread.__init__(self)
- self.task = task
- def run(self):
- for self.i in range(self.task):
- self.gen = genp()
- self.url = 'https://gs.3g.cn/D/' + self.gen + '/c'
- self.response = requests.get(self.url, timeout=5, allow_redirects=False)
- if self.response.status_code != 302:
- print(self.gen + " is error!")
- continue
- self.url = self.response.headers['location']
- self.type = self.url[self.url.find('.', self.url.find('com/')):]
- if filter != None:
- if self.type in filter:
- print(self.gen + " passed for blocked type: " + self.type)
- continue
- self.response = requests.get(self.url, timeout=10)
- if self.response.status_code == 200:
- if self.type == '.zip' and unpack:
- unpack_archive(self.response, path, self.gen)
- else:
- self.f = open(path + self.gen + self.type, 'wb')
- self.f.write(self.response.content)
- self.f.close()
- if sorting:
- sort(self.gen + self.type, self.type, path)
- print(self.gen + " downloaded")
- if not self.type in files:
- files[self.type] = 0
- files[self.type] += 1
- files['global'] += 1
- else:
- print(self.gen + " is error!")
- global isdone
- isdone += 1
- threads = []
- rest = count
- for i in range(tcount):
- rest = rest - round(count/tcount)
- if rest >= 0:
- threads.append(dlThread(round(count/tcount)))
- else:
- threads.append(dlThread(rest + round(count/tcount)))
- threads[i].start()
- threads[i].id = i
- while isdone != tcount:
- pass
- logstr = '| '
- for key in files:
- if key != 'global':
- logstr = logstr + key + ': ' + str(files[key]) + ' | '
- print("Работа завершена, скачано: " + str(files['global']) + '/' + str(count))
- print(logstr)
- # Coded by MorphEdAlign
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement