Advertisement
Guest User

gosms_dl.py

a guest
Nov 21st, 2020
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.01 KB | None | 0 0
  1. import argparse
  2. import requests
  3. import time
  4. import math
  5. import zipfile
  6. import os
  7. import io
  8. from threading import Thread
  9. from random import choice
  10. from random import randint
  11. from string import digits
  12. from sys import exit
  13.  
  14. isdone = 0
  15. files = {'global': 0}
  16.  
  17. symbdict = 'abcdf' + digits
  18.  
  19. Videos = ['.mp4', '.3gp']
  20. Photos = ['.jpg', '.png']
  21. Gifs = ['.gif']
  22. Audio = ['.mp3', '.amr']
  23.  
  24. def sort(filename, type, mainpath):
  25.     if type in Photos:
  26.         os.rename(mainpath + filename, mainpath + 'Photos\\' + filename)
  27.     elif type in Videos:
  28.         os.rename(mainpath + filename, mainpath + 'Videos\\' + filename)
  29.     elif type in Gifs:
  30.         os.rename(mainpath + filename, mainpath + 'Gifs\\' + filename)
  31.     elif type in Audio:
  32.         os.rename(mainpath + filename, mainpath + 'Audio\\' + filename)
  33.     else:
  34.         os.rename(mainpath + filename, mainpath + 'Other\\' + filename)
  35.        
  36. def unpack_archive(arch, path, name):
  37.     with arch, zipfile.ZipFile(io.BytesIO(arch.content)) as fzip:
  38.         fzip.extractall(path[:len(path)-1])
  39.    
  40.     i = 0
  41.     for file in fzip.infolist():
  42.         fname = file.filename
  43.         newname = name + '_' + str(i) + fname[fname.find('.'):]
  44.         os.rename(path + fname, path + newname)
  45.         if sorting:
  46.             sort(newname, fname[fname.find('.'):], path)
  47.            
  48.         i = i + 1
  49.    
  50.     fzip.close()
  51.    
  52. def genp():
  53.     return ''.join(choice(symbdict) for i in range(randint(4, 6)))
  54.    
  55. parser = argparse.ArgumentParser(description='GOSMS downloader 3.0')
  56. parser.add_argument('-c', action="store", dest="count", default=5, type=int)
  57. parser.add_argument('-o', action="store", dest="out", default='')
  58. parser.add_argument('-t', action="store", dest="threads", default=1, type=int)
  59. parser.add_argument('--unpack', action="store_true", dest="unpack")
  60. parser.add_argument('-no', action='append', dest="filter", default=None)
  61. parser.add_argument('--sort', action="store_true", dest="sorting")
  62.  
  63. args = parser.parse_args()
  64. count = args.count
  65. tcount = args.threads
  66. path = args.out
  67. unpack = args.unpack
  68. filter = args.filter
  69. sorting = args.sorting
  70.  
  71. if path != '' and path[len(path):] != '\\':
  72.     path = path + '\\'
  73.  
  74. if not os.path.exists(path+'Videos'):
  75.     os.makedirs(path+'Videos')
  76. if not os.path.exists(path+'Photos'):
  77.     os.makedirs(path+'Photos')
  78. if not os.path.exists(path+'Gifs'):
  79.     os.makedirs(path+'Gifs')
  80. if not os.path.exists(path+'Audio'):
  81.     os.makedirs(path+'Audio')
  82. if not os.path.exists(path+'Other'):
  83.     os.makedirs(path+'Other')
  84.    
  85. if tcount > count:
  86.     print("The count of threads should not exceed the url's count")
  87.     exit()
  88.    
  89. class dlThread(Thread):
  90.     def __init__(self, task):
  91.         Thread.__init__(self)
  92.         self.task = task
  93.        
  94.     def run(self):
  95.         for self.i in range(self.task):
  96.             self.gen = genp()
  97.             self.url = 'https://gs.3g.cn/D/' + self.gen + '/c'
  98.             self.response = requests.get(self.url, timeout=5, allow_redirects=False)
  99.             if self.response.status_code != 302:
  100.                 print(self.gen + " is error!")
  101.                 continue
  102.                
  103.             self.url = self.response.headers['location']
  104.            
  105.             self.type = self.url[self.url.find('.', self.url.find('com/')):]
  106.             if filter != None:
  107.                 if self.type in filter:
  108.                     print(self.gen + " passed for blocked type: " + self.type)
  109.                     continue
  110.            
  111.             self.response = requests.get(self.url, timeout=10)
  112.             if self.response.status_code == 200:                                          
  113.                 if self.type == '.zip' and unpack:                
  114.                     unpack_archive(self.response, path, self.gen)
  115.                 else:
  116.                     self.f = open(path + self.gen + self.type, 'wb')
  117.                     self.f.write(self.response.content)
  118.                     self.f.close()
  119.                    
  120.                     if sorting:
  121.                         sort(self.gen + self.type, self.type, path)
  122.                
  123.                 print(self.gen + " downloaded")
  124.                 if not self.type in files:
  125.                     files[self.type] = 0
  126.                    
  127.                 files[self.type] += 1
  128.                 files['global'] += 1
  129.             else:
  130.                 print(self.gen + " is error!")
  131.                
  132.         global isdone
  133.         isdone += 1
  134.  
  135. threads = []
  136. rest = count
  137. for i in range(tcount):
  138.     rest = rest - round(count/tcount)
  139.     if rest >= 0:
  140.         threads.append(dlThread(round(count/tcount)))
  141.     else:
  142.         threads.append(dlThread(rest + round(count/tcount)))
  143.    
  144.     threads[i].start()
  145.     threads[i].id = i
  146. while isdone != tcount:
  147.     pass
  148.  
  149. logstr = '| '
  150. for key in files:
  151.     if key != 'global':
  152.         logstr = logstr + key + ': ' + str(files[key]) + ' | '
  153.  
  154. print("Работа завершена, скачано: " + str(files['global']) + '/' + str(count))
  155. print(logstr)
  156.  
  157. # Coded by MorphEdAlign
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement