Guest User

MediaFireDownloader

a guest
Nov 27th, 2013
43
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.  
  6. appKey = ''
  7. appID = ''
  8. sessionKey = ''
  9. userEmail = ''
  10. userPassword = ''
  11. m = hashlib.sha1()
  12.  
  13. parentFolderKeys = []
  14.  
  15. folderKey = ''
  16.  
  17. def login(email, password):
  18.     m.update(email + password + appID + appKey)
  19.     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'
  20.     result = json.load(urllib.urlopen(url))
  21.  
  22.     folderKey = ''
  23.  
  24.     if result.get('response').get('result') == 'Success':
  25.         return result.get('response').get('session_token')
  26.     else:
  27.         print 'Failed to connect to user account.'
  28.         return None
  29.  
  30. def getFolders(key):
  31.     if folderKey == '':
  32.         url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  33.     else:
  34.         url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  35.  
  36.     result = json.load(urllib.urlopen(url))
  37.  
  38.     to = len(result.get('response').get('folder_content').get('folders'))
  39.    
  40.     for i in range (0, to):
  41.         print result.get('response').get('folder_content').get('folders')[i].get('name')
  42.  
  43. def getFiles(key):
  44.     if folderKey == '':
  45.         url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&content_type=files&response_format=json'
  46.     else:
  47.         url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&content_type=files&response_format=json'
  48.     result = json.load(urllib.urlopen(url))
  49.    
  50.     to = len(result.get('response').get('folder_content').get('files'))
  51.    
  52.     for i in range (0, to):
  53.         print result.get('response').get('folder_content').get('files')[i].get('filename')
  54.  
  55. def jumpInFolder(key, folderName):
  56.     global folderKey
  57.     if folderKey == '':
  58.         url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  59.     else:
  60.         url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  61.  
  62.     result = json.load(urllib.urlopen(url))
  63.     to = len(result.get('response').get('folder_content').get('folders'))
  64.    
  65.     for i in range (0, to):
  66.         currentItem = result.get('response').get('folder_content').get('folders')[i]
  67.         if currentItem.get('name') == folderName:
  68.             parentFolderKeys.append(folderKey)
  69.             folderKey = currentItem.get('folderkey')
  70.             break
  71.     if folderKey == '':
  72.         print "Couldn't find a folder named: " + folderName
  73. def jumpBackInFolder():
  74.     global folderKey
  75.  
  76.     if len(parentFolderKeys) > 0:
  77.         folderKey = parentFolderKeys.pop()
  78. def downloadFolder(key):
  79.     downloadList = ''
  80.  
  81.     if folderKey == '':
  82.         url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&content_type=files&response_format=json'
  83.     else:
  84.         url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&content_type=files&response_format=json'
  85.     result = json.load(urllib.urlopen(url))
  86.    
  87.     to = len(result.get('response').get('folder_content').get('files'))
  88.     allFiles = result.get('response').get('folder_content').get('files')
  89.     print "Amount of files: ", len(allFiles)
  90.     for i in range (0, to):
  91.         currentFileKey = allFiles[i].get('quickkey')
  92.  
  93.         url = 'https://www.mediafire.com/api/file/get_links.php?session_token=' + key + '&quick_key=' + currentFileKey + '&duration=60&response_format=json'
  94.  
  95.         result = json.load(urllib.urlopen(url))
  96.  
  97.         download = result.get('response').get('links')[0].get('direct_download')
  98.         fileName = allFiles[i].get('filename')
  99.         downloadList += (download + ', ' + fileName + '\n')
  100.  
  101.     with open("DownloadList.txt", "w") as textFile:
  102.         textFile.write(downloadList)
  103.  
  104. def downloadAll():
  105.     with open("DownloadList.txt", "r") as textFile:
  106.         links = textFile.readlines()
  107.         for i in range (0, len(links)):
  108.             url = links[i].split(', ')[0]
  109.             fileName = links[i].split(', ')[1].split('\n')[0]
  110.  
  111.             print 'Downloading: ' + fileName
  112.             #page = urllib.urlopen(url).read()
  113.  
  114.             #with open(fileName + ".html", "w") as textFile:
  115.             #    textFile.write(page)
  116.  
  117.             #ultimateURL = page.split('kNO = "', 1)[1].split('"', 1)[0]
  118.             #print 'Real file name: ' + fileName
  119.             #print 'Real download URL: ' + ultimateURL
  120.  
  121.             #urllib.urlretrieve(download, fileName)
  122.             urllib.urlretrieve(url, fileName)
  123.  
  124. def getInput():
  125.     return raw_input()
  126.  
  127. text = ""
  128. while text != "exit":
  129.     text = getInput()
  130.     if text == "login":
  131.         print 'Enter your email: ',
  132.         userEmail = getInput()
  133.         print 'Enter your password: ',
  134.         userPassword = getInput()
  135.         sessionKey = login(userEmail, userPassword)
  136.     if text == "ls":
  137.         print
  138.         getFolders(sessionKey)
  139.         print
  140.         getFiles(sessionKey)
  141.     if text == "get":
  142.         downloadFolder(sessionKey)
  143.         downloadAll()
  144.     if text.split(' ')[0] == "cd":
  145.         if len(text.split(' ')) > 1:
  146.             arg = text.split(' ', 1)[1]
  147.             if arg == '..':
  148.                 print 'Jumping back'
  149.                 jumpBackInFolder()
  150.             else:
  151.                 jumpInFolder(sessionKey, arg)
  152.         else:
  153.             print 'Syntax for cd was invalid.'
  154.     if text == "help":
  155.         print 'Possible commands:'
  156.         print '\tlogin'
  157.         print '\tls\tShows the folders first, then shows files.'
  158.         print '\tcd [Folder Name]'
  159.         print '\tcd ..'
  160.         print '\tget\tDownloads all the files with a folder.'
  161.         print '\texit'
  162.         print '\thelp'
  163.     print
  164. print 'Bye!'
RAW Paste Data