Guest User

SenRequestV2.py

a guest
Dec 1st, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.47 KB | None | 0 0
  1. import urllib
  2. import json
  3. import time
  4. import hashlib
  5. import getpass
  6.  
  7. try:
  8.     appKey = ''
  9.     appID = ''
  10.     sessionKey = ''
  11.     userEmail = ''
  12.     userPassword = ''
  13.     m = hashlib.sha1()
  14.  
  15.     parentFolderKeys = []
  16.  
  17.     folderKey = ''
  18.  
  19.     def login(email, password):
  20.         m.update(email + password + appID + appKey)
  21.         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'
  22.         result = json.load(urllib.urlopen(url))
  23.         simpleParse = json.dumps(result, sort_keys=True, indent=2)
  24.  
  25.         folderKey = ''
  26.  
  27.         if result.get('response').get('result') == 'Success':
  28.             print 'Connected successfully!'
  29.             return result.get('response').get('session_token')
  30.         else:
  31.             print 'Failed to connect to user account.'
  32.             return None
  33.  
  34.     def getFolders(key):
  35.         if folderKey == '':
  36.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  37.         else:
  38.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  39.  
  40.         result = json.load(urllib.urlopen(url))
  41.  
  42.         to = len(result.get('response').get('folder_content').get('folders'))
  43.        
  44.         for i in range (0, to):
  45.             print result.get('response').get('folder_content').get('folders')[i].get('name')
  46.  
  47.     def getFiles(key):
  48.         if folderKey == '':
  49.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&content_type=files&response_format=json'
  50.         else:
  51.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&content_type=files&response_format=json'
  52.         result = json.load(urllib.urlopen(url))
  53.        
  54.         to = len(result.get('response').get('folder_content').get('files'))
  55.        
  56.         for i in range (0, to):
  57.             print result.get('response').get('folder_content').get('files')[i].get('filename')
  58.  
  59.     def jumpInFolder(key, folderName):
  60.         global folderKey
  61.         if folderKey == '':
  62.             url = 'https://www.mediafire.com/api/folder/get_content.php?session_token=' + key + '&response_format=json'
  63.         else:
  64.             url = 'https://www.mediafire.com/api/folder/get_content.php?folder_key=' + folderKey + '&response_format=json'
  65.  
  66.         result = json.load(urllib.urlopen(url))
  67.        
  68.         to = len(result.get('response').get('folder_content').get('folders'))
  69.  
  70.         for i in range (0, to):
  71.             currentItem = result.get('response').get('folder_content').get('folders')[i]
  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(startAt):
  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.                 if startAt == '':
  130.                     print 'URL: ' + url
  131.  
  132.                     urllib.urlretrieve(url, fileName)
  133.                 elif startAt == fileName:
  134.                     print 'URL: ' + url
  135.  
  136.                     urllib.urlretrieve(url, fileName)
  137.                     startAt = ''
  138.         print "\a",
  139.         print "\a",
  140.         print "\a",
  141.  
  142.     def getInput():
  143.         return raw_input()
  144.  
  145.     #Parse command
  146.     def parseC(command, t):
  147.         if command.split(' ')[0] == t:
  148.             return True
  149.         else:
  150.             return False
  151.  
  152.     def argsCount(comment):
  153.         return len(text.split(' ')) - 1
  154.  
  155.     def getArg(command):
  156.         return text.split(' ', 1)[1]
  157.  
  158.     print '\nProgram started!\n'
  159.     text = ""
  160.     while not parseC(text, "exit"):
  161.         text = getInput()
  162.         if parseC(text, "login"):
  163.             print 'Email: ',
  164.             userEmail = getInput()
  165.             userPassword = getpass.getpass()
  166.             sessionKey = login(userEmail, userPassword)
  167.         if parseC(text, "ls"):
  168.             print
  169.             getFolders(sessionKey)
  170.             print
  171.             getFiles(sessionKey)
  172.         if parseC(text, "get"):
  173.             if argsCount(text) > 0:
  174.                 try:
  175.                     arg = int(getArg(text))
  176.                     downloadFolder(sessionKey, arg)
  177.                     downloadAll('')
  178.                 except ValueError:
  179.                     downloadFolder(sessionKey, 1)
  180.                     downloadAll('')
  181.             else:
  182.                 downloadFolder(sessionKey, 1)
  183.                 downloadAll('')
  184.         if parseC(text, "go"):
  185.             if argsCount(text) > 0:
  186.                 downloadAll(getArg(text))
  187.             else:
  188.                 downloadAll('')
  189.         if parseC(text, "cd"):
  190.             if argsCount(text) > 0:
  191.                 arg = getArg(text)
  192.                 if arg == '..':
  193.                     jumpBackInFolder()
  194.                 else:
  195.                     jumpInFolder(sessionKey, arg)
  196.             else:
  197.                 print 'Syntax for cd was invalid.'
  198.         if parseC(text, "help"):
  199.             if argsCount(text) == 0:
  200.                 print '\nPossible commands:\n'
  201.                 print '\tlogin\tRequires MediaFire email and password.'
  202.                 print '\tls\tShows the folders first, then shows files.'
  203.                 print '\tcd [FolderName]'
  204.                 print '\tcd ..'
  205.                 print '\tget\tDownloads all the files with a folder.'
  206.                 print '\tget [number]\tDefaults to 1. | 2 would mean to start after file 100.'
  207.                 print '\tgo [FileName]\tDownloads from list, can start at a file name.'
  208.                 print '\texit\tEnds the program.'
  209.                 print '\thelp\tShows the help.'
  210.                 print '*Note that elements within brackets are optionnal.*'
  211.                 print '*Note that all commands, and their arguments are case sensitive.*'
  212.                 print '*Type help followed by a command to get a more in depth explaination of it.*'
  213.             else:
  214.                 arg = getArg(text)
  215.                 if arg == 'login':
  216.                     print '\tIn order to login in, you need to have an account on MediaFire, that account consists of an email address and a password.'
  217.                     print 'Note that the password won\'t be shown while you type it in. This is to prevent eavesdroppers from over seeing it and compromising your account.'
  218.                 elif arg == 'ls':
  219.                     print '\tThis command shows you a you a partial list of folders and files in the current folder.'
  220.                 elif arg == 'cd':
  221.                     print '\tThis command allows you to move up or down withing a folder tree.'
  222.                     print 'In order to move within a folder, you type \'cd FolderName\'. (See the \'ls\' command. for more info.)'
  223.                     print 'In order to move back within a folder, you type \'cd ..\''
  224.                 elif arg == 'get':
  225.                     print '\tget is used to query a list direct download links for all the files within a folder. Once the list is generated, it then proceeds to download them one by one.'
  226.                     print 'With a [number] parameter, it will use it to skip a certain amount of chunks. Files within MediaFire are grouped into chunks, otherwise, result from a query could be quite large with folders that have thousands of files. If a folder has 100 files and a chunk contains 25 files, the folder would be broken up into 4 chunks.'
  227.                 elif arg == 'go':
  228.                     print '\tA sort of follow up to the \'get\' command. Sometimes, it can happen that the program crashes for whatever reason, cancelling the currently downloading files. Use the go command to resume the download queue.'
  229.                     print 'If you want to resume the download starting from a certain file, you can call the \'go\' command followed by the name of your file such as \'go HelloWorld.txt\'.'
  230.                 elif arg == 'exit':
  231.                     print '\tRather self explainatory.'
  232.                 elif arg == 'help':
  233.                     print '\tHaving fun searching random stuff?'
  234.                 else:
  235.                     print 'No help found for that command.'
  236.         print
  237.     print 'Bye!'
  238.  
  239. except Exception as ex:
  240.     print ex
  241.     print "\a",
  242.     time.sleep(0.5)
  243.     print "\a",
  244.     print "\a",
Advertisement
Add Comment
Please, Sign In to add comment