Guest User

SenRequestV2.py

a guest
Nov 28th, 2013
36
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import urllib
  2. import json
  3. import time
  4. import hashlib
  5. import getpass
  6.  
  7. try:
  8.  
  9.     appKey = ''
  10.     appID = ''
  11.     sessionKey = ''
  12.     userEmail = ''
  13.     userPassword = ''
  14.     m = hashlib.sha1()
  15.  
  16.     parentFolderKeys = []
  17.  
  18.     folderKey = ''
  19.  
  20.     def login(email, password):
  21.         m.update(email + password + appID + appKey)
  22.         url = 'https://www.mediafire.com/api/user/get_session_token.php?email=' + email + '&password=' + password + '&application_id=' + appID + '&signature=' + m.hexdigest() + '&version=1&response_format=json'
  23.         result = json.load(urllib.urlopen(url))
  24.         simpleParse = json.dumps(result, sort_keys=True, indent=2)
  25.  
  26.         folderKey = ''
  27.  
  28.         if result.get('response').get('result') == 'Success':
  29.             print 'Connected successfully!'
  30.             return result.get('response').get('session_token')
  31.         else:
  32.             print 'Failed to connect to user account.'
  33.             return None
  34.  
  35.     def getFolders(key):
  36.         if folderKey == '':
  37.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  38.         else:
  39.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  40.  
  41.         result = json.load(urllib.urlopen(url))
  42.  
  43.         to = len(result.get('response').get('folder_content').get('folders'))
  44.        
  45.         for i in range (0, to):
  46.             print result.get('response').get('folder_content').get('folders')[i].get('name')
  47.  
  48.     def getFiles(key):
  49.         if folderKey == '':
  50.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&content_type=files&response_format=json'
  51.         else:
  52.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&content_type=files&response_format=json'
  53.         result = json.load(urllib.urlopen(url))
  54.        
  55.         to = len(result.get('response').get('folder_content').get('files'))
  56.        
  57.         for i in range (0, to):
  58.             print result.get('response').get('folder_content').get('files')[i].get('filename')
  59.  
  60.     def jumpInFolder(key, folderName):
  61.         global folderKey
  62.         if folderKey == '':
  63.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  64.         else:
  65.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  66.  
  67.         result = json.load(urllib.urlopen(url))
  68.        
  69.         for i in range (0, to):
  70.             currentItem = result.get('response').get('folder_content').get('folders')[i]
  71.             #print "currentName: " + currentItem.get('name') + " Was searching for: " + folderName
  72.             if currentItem.get('name') == folderName:
  73.                 parentFolderKeys.append(folderKey)
  74.                 folderKey = currentItem.get('folderkey')
  75.                 break
  76.         if folderKey == '':
  77.             print "Couldn't find a folder named: " + folderName
  78.     def jumpBackInFolder():
  79.         global folderKey
  80.  
  81.         if len(parentFolderKeys) > 0:
  82.             folderKey = parentFolderKeys.pop()
  83.  
  84.     def downloadFolder(key, chunk):
  85.         downloadList = ''
  86.         allFiles = []
  87.  
  88.         if chunk <= 0:
  89.             chunk = 1
  90.  
  91.         while True:
  92.             if folderKey == '':
  93.                 url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&chunk=' + str(chunk) + '&content_type=files&response_format=json'
  94.             else:
  95.                 url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&chunk=' + str(chunk) + '&content_type=files&response_format=json'
  96.             result = json.load(urllib.urlopen(url))
  97.            
  98.             if len(result.get('response').get('folder_content').get('files')) > 0:
  99.                 chunk += 1
  100.                 allFiles.extend(result.get('response').get('folder_content').get('files'))
  101.             else:
  102.                 break
  103.         to = len(allFiles)
  104.  
  105.         print "Amount of files:", len(allFiles)
  106.         print "Please wait while links are saved to the DownloadList.txt file. This may take a while if you have a lot of files.\n"
  107.         for i in range (0, to):
  108.             currentFileKey = allFiles[i].get('quickkey')
  109.  
  110.             url = 'https://www.mediafire.com/api/file/get_links.php?session_token=' + key + '&quick_key=' + currentFileKey + '&duration=60&response_format=json'
  111.  
  112.             result = json.load(urllib.urlopen(url))
  113.  
  114.             download = result.get('response').get('links')[0].get('direct_download')
  115.             fileName = allFiles[i].get('filename')
  116.             downloadList += (download + ', ' + fileName + '\n')
  117.  
  118.         print downloadList
  119.         with open("DownloadList.txt", "w") as textFile:
  120.             textFile.write(downloadList)
  121.  
  122.     def downloadAll():
  123.         with open("DownloadList.txt", "r") as textFile:
  124.             links = textFile.readlines()
  125.             for i in range (0, len(links)):
  126.                 url = links[i].split(', ')[0]
  127.                 fileName = links[i].split(', ')[1].split('\n')[0]
  128.  
  129.                 print 'URL: ' + url
  130.  
  131.                 urllib.urlretrieve(url, fileName)
  132.         print "\a",
  133.         print "\a",
  134.         print "\a",
  135.  
  136.     def getInput():
  137.         return raw_input()
  138.  
  139.     text = ""
  140.     while text != "exit":
  141.         text = getInput()
  142.         if text == "login":
  143.             print 'Email: ',
  144.             userEmail = getInput()
  145.             userPassword = getpass.getpass()
  146.             sessionKey = login(userEmail, userPassword)
  147.         if text == "ls":
  148.             print
  149.             getFolders(sessionKey)
  150.             print
  151.             getFiles(sessionKey)
  152.         if text.split(' ')[0] == "get":
  153.             if len(text.split(' ')) > 1:
  154.                 try:
  155.                     arg = int(text.split(' ', 1)[1])
  156.                     downloadFolder(sessionKey, arg)
  157.                     downloadAll()
  158.                 except ValueError:
  159.                     downloadFolder(sessionKey, 1)
  160.                     downloadAll()
  161.             else:
  162.                 downloadFolder(sessionKey, 1)
  163.                 downloadAll()
  164.         if text == "go":
  165.             downloadAll()
  166.         if text.split(' ')[0] == "cd":
  167.             if len(text.split(' ')) > 1:
  168.                 arg = text.split(' ', 1)[1]
  169.                 if arg == '..':
  170.                     print 'Jumping back'
  171.                     jumpBackInFolder()
  172.                 else:
  173.                     jumpInFolder(sessionKey, arg)
  174.             else:
  175.                 print 'Syntax for cd was invalid.'
  176.         if text == "help":
  177.             print 'Possible commands:'
  178.             print '\tlogin'
  179.             print '\tls\tShows the folders first, then shows files.'
  180.             print '\tcd [Folder Name]'
  181.             print '\tcd ..'
  182.             print '\tget\tDownloads all the files with a folder.'
  183.             print '\tget [number]\tDefaults to 1. | 2 would mean to start after file 100.'
  184.             print '\tgo\tWill download the files from the generated list of links.'
  185.             print '\texit'
  186.             print '\thelp'
  187.         print
  188.     print 'Bye!'
  189.  
  190. except Exception as ex:
  191.     print ex
  192.     print "\a",
  193.     time.sleep(0.5)
  194.     print "\a",
  195.     print "\a",
RAW Paste Data