def DownloadApk(url, fileName):
    # Streaming, so we can iterate over the response.
    response = requests.get(url, stream=True)
    totalSizeInBytes = response.headers.get(
        'x-archive-orig-content-length') or response.headers.get(
            'content-length', 0)
    if (totalSizeInBytes):
        # Fixed where it stuck on "Downloading legacy WhatsApp V2.11.431 to helpers folder"
        totalSizeInBytes = int(totalSizeInBytes)
    else:
        # totalSizeInBytes must be null
        CustomPrint(
            '\aFor some reason I could not download Legacy WhatsApp, you need to download it on your own now from either of the links given below : ',
            'red')
        print('\n')
        CustomPrint('1. \'' + appURLWhatsAppCDN + '\' (official\'s archive)',
                    'red')
        CustomPrint('2. \'' + appURLWhatsCryptCDN + '\' unofficial website.',
                    'red')
        print('\n')
        CustomPrint(
            'Once downloaded rename it to \'LegacyWhatsApp.apk\' exactly and put in \'helpers\' folder.',
            'red')
        Exit()
    blockSize = 1024  # 1 Kibibyte
    progressBar = tqdm(total=totalSizeInBytes, unit='iB', unit_scale=True)
    with open('helpers/temp.apk', 'wb') as file:
        for data in response.iter_content(blockSize):
            progressBar.update(len(data))
            file.write(data)
    progressBar.close()
    os.rename('helpers/temp.apk', 'helpers/LegacyWhatsApp.apk')
    if totalSizeInBytes != 0 and progressBar.n != totalSizeInBytes:
        CustomPrint('\aSomething went during downloading LegacyWhatsApp.apk')
        Exit()
def init(tcpIP, tcpPort):
    # Detect OS
    isWindows = False
    isLinux = False
    if platform.system() == 'Windows':
        isWindows = True
    if platform.system() == 'Linux':
        isLinux = True

    # Global command line helpers
    currDir = os.path.dirname(os.path.realpath(__file__))
    rootDir = os.path.abspath(os.path.join(currDir, '..'))

    if(isWindows):
        adb = rootDir + '\\bin\\adb.exe'
    else:
        adb = 'adb'

    combo = tcpIP + ':' + tcpPort
    cmd = adb + ' connect ' + combo
    os.system(adb + ' kill-server')
    os.system(adb + ' start-server')
    proc = sp.Popen(cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE,
                    stderr=sp.PIPE, shell=False)
    output, error = proc.communicate()
    output = output.decode('utf-8')
    error = error.decode('utf-8')

    if len(output) == 0 or error:
        output = None
        CustomPrint(error, 'red')
        Exit()
    else:
        output = [x.strip() for x in output.split() if len(x.strip()) > 0]

    if('connected' in (x.lower() for x in output)):
        return combo
    if('authenticate' in (x.lower() for x in output)):
        CustomPrint(
            'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
        Exit()
    if('refused' in (x.lower() for x in output)):
        CustomPrint(
            'Could not find any connected device. Either USB Debugging is off or device is not running ADB over TCP', 'red')
        return ''

    ''' Possible outputs
def AfterConnect(ADBSerialId):
    SDKVersion = int(
        getoutput('adb -s ' + ADBSerialId +
                  ' shell getprop ro.build.version.sdk'))
    if (SDKVersion <= 13):
        CustomPrint(
            'Unsupported device. This method only works on Android v4.0 or higer.',
            'red')
        CustomPrint('Cleaning up temporary direcory.', 'red')
        os.system('rm -rf tmp/*')
        Exit()
    _waPathText = 'adb -s ' + ADBSerialId + ' shell pm path com.whatsapp'
    proc = subprocess.Popen(_waPathText.split(),
                            stdin=subprocess.PIPE,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=False)
    out, err = proc.communicate()
    out = out.decode('utf-8')
    if (not out):
        CustomPrint('Looks like WhatsApp is not installed on device.', 'red')
        Exit()
    WhatsAppapkPath = re.search('(?<=package:)(.*)(?=apk)',
                                str(check_output(
                                    _waPathText.split()))).group(1) + 'apk'
    sdPath = getoutput('adb -s ' + ADBSerialId +
                       ' shell "echo $EXTERNAL_STORAGE"') or '/sdcard'
    # To check if APK even exists at a given path to download!
    # Since that obviously is not available at whatsapp cdn defaulting that to 0 for GH #46
    # Using getoutput instead of this to skip getting data like 0//n//r or whatever was getting recieved on GH #46 bcz check_output returns a byte type object and getoutput returns a str type .
    contentLength = int((re.findall(
        "(?<=content-length:)(.*[0-9])(?=)",
        getoutput(
            'curl -sI https://web.archive.org/web/20141111030303if_/http://www.whatsapp.com/android/current/WhatsApp.apk'
        )) or ['0'])[0])
    _versionNameText = 'adb -s ' + ADBSerialId + \
        ' shell dumpsys package com.whatsapp'
    versionName = re.search("(?<=versionName=)(.*?)(?=\\\\n)",
                            str(check_output(
                                _versionNameText.split()))).group(1)
    CustomPrint('WhatsApp V' + versionName + ' installed on device')
    downloadAppFrom = appURLWhatsAppCDN if (
        contentLength == 18329558) else appURLWhatsCryptCDN
    if (version.parse(versionName) > version.parse('2.11.431')):
        if not (os.path.isfile('helpers/LegacyWhatsApp.apk')):
            CustomPrint(
                'Downloading legacy WhatsApp V2.11.431 to helpers folder')
            DownloadApk(downloadAppFrom, 'helpers/LegacyWhatsApp.apk')
            # wget.download(downloadAppFrom, 'helpers/LegacyWhatsApp.apk')
            print('\n')
        else:
            CustomPrint(
                'Found legacy WhatsApp V2.11.431 apk in helpers folder')
    else:
        # Version lower than 2.11.431 installed on device.
        pass

    return 1, SDKVersion, WhatsAppapkPath, versionName, sdPath
def DownloadApk(url, fileName):
    # Streaming, so we can iterate over the response.
    response = requests.get(url, stream=True)
    totalSizeInBytes = int(response.headers.get('content-length', 0))
    blockSize = 1024  # 1 Kibibyte
    progressBar = tqdm(total=totalSizeInBytes, unit='iB', unit_scale=True)
    with open('helpers/temp.apk', 'wb') as file:
        for data in response.iter_content(blockSize):
            progressBar.update(len(data))
            file.write(data)
    progressBar.close()
    os.rename('helpers/temp.apk', 'helpers/LegacyWhatsApp.apk')
    if totalSizeInBytes != 0 and progressBar.n != totalSizeInBytes:
        CustomPrint('\aSomething went during downloading LegacyWhatsApp.apk')
def AfterConnect(ADBSerialId):
    _sdkVersionText = 'adb -s ' + ADBSerialId + ' shell getprop ro.build.version.sdk'
    SDKVersion = int(
        re.search('[0-9]{2,3}',
                  str(check_output(_sdkVersionText.split()))).group(0))
    if (SDKVersion <= 13):
        CustomPrint(
            'Unsupported device. This method only works on Android v4.0 or higer.',
            'red')
        CustomPrint('Cleaning up temporary direcory.', 'red')
        os.system('rm -rf tmp/*')
        Exit()
    _waPathText = 'adb -s ' + ADBSerialId + ' shell pm path com.whatsapp'
    WhatsAppapkPath = re.search('(?<=package:)(.*)(?=apk)',
                                str(check_output(
                                    _waPathText.split()))).group(1) + 'apk'
    if not (WhatsAppapkPath):
        CustomPrint('Looks like WhatsApp is not installed on device.')
        Exit()
    #SDPath = re.search("(?<=b')(.*)(?=\\\\n)", str(check_output('adb shell "echo $EXTERNAL_STORAGE"'.split()))).group(1)
    contentLength = int(
        re.search(
            "(?<=Content-Length:)(.*[0-9])(?=)",
            str(
                check_output(
                    'curl -sI http://www.cdn.whatsapp.net/android/2.11.431/WhatsApp.apk'
                    .split()))).group(1)
    )  # To check if APK even exists at a given path to download!
    _versionNameText = 'adb -s ' + ADBSerialId + ' shell dumpsys package com.whatsapp'
    versionName = re.search("(?<=versionName=)(.*?)(?=\\\\n)",
                            str(check_output(
                                _versionNameText.split()))).group(1)
    CustomPrint('WhatsApp V' + versionName + ' installed on device')
    downloadAppFrom = appURLWhatsAppCDN if (
        contentLength == 18329558) else appURLWhatsCryptCDN
    if (version.parse(versionName) > version.parse('2.11.431')):
        if not (os.path.isfile('helpers/LegacyWhatsApp.apk')):
            CustomPrint(
                'Downloading legacy WhatsApp V2.11.431 to helpers folder')
            wget.download(downloadAppFrom, 'helpers/LegacyWhatsApp.apk')
        else:
            CustomPrint(
                'Found legacy WhatsApp V2.11.431 apk in helpers folder')

    return 1, SDKVersion, WhatsAppapkPath, versionName
def LinuxUSB(ADBSerialId):
    _deviceName = 'adb -s ' + ADBSerialId + ' shell getprop ro.product.model'
    CustomPrint('Connected to ' +
                re.search("(?<=b')(.*)(?=\\\\n)",
                          str(check_output(_deviceName.split()))).group(1))
    return AfterConnect(ADBSerialId)
def Exit():
    CustomPrint('\nExiting...')
    os.system('adb kill-server')
    quit()
Ejemplo n.º 8
0
def Exit():
    print('\n')
    CustomPrint('Exiting...')
    quit()
Ejemplo n.º 9
0
def init():
    # Detect OS
    isWindows = False
    isLinux = False
    if platform.system() == 'Windows':
        isWindows = True
    if platform.system() == 'Linux':
        isLinux = True

    # Global command line helpers
    currDir = os.path.dirname(os.path.realpath(__file__))
    rootDir = os.path.abspath(os.path.join(currDir, '..'))
    if(isWindows):
        adb = rootDir + '\\bin\\adb.exe'
    else:
        adb = 'adb'

    cmd = adb + ' devices'
    # Kill server before getting list to avoid daemon texts.
    os.system(adb + ' kill-server')
    os.system(adb + ' start-server')
    proc = sp.Popen(cmd.split(), stdin=sp.PIPE, stdout=sp.PIPE,
                    stderr=sp.PIPE, shell=False)
    output, error = proc.communicate()
    output = output.decode('utf-8')
    error = error.decode('utf-8')

    if len(output) == 0 or error:
        output = None
        CustomPrint(error, 'red')
        Exit()
    else:
        output = [x.strip() for x in output.split('\n') if len(x.strip()) > 0]

    if(len(output) == 1):
        CustomPrint(
            'Could not find any connected device. Is USB Debugging on?', 'red')
        return ''

    deviceToConnect = None
    i = 1
    if(len(output) == 2):
        if(output[1].split()[1] == 'offline'):
            CustomPrint(
                'Device is offline, try turning off USB debugging and turn on again.', 'yellow')
            Exit()
        if(output[1].split()[1] == 'unauthorized'):
            CustomPrint(
                'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
            Exit()
        return output[1].split()[0]

    CustomPrint(output[0])
    print('\n')
    if deviceToConnect is None:
        for device in output[1:]:
            name = adb + ' -s ' + \
                device.split()[0] + ' shell getprop ro.product.model'
            CustomPrint(str(i) + '. ' + device.split()
                        [0] + '  ' + device.split()[1] + '  ' + sp.getoutput(name).strip())
            i += 1

    while deviceToConnect is None:
        deviceIndex = int(CustomInput('Enter device number (for ex : 2) : '))
        if deviceIndex <= 0 or deviceIndex + 1 > len(output):
            continue
        deviceToConnect = output[deviceIndex]

    if(deviceToConnect.split()[1] == 'offline'):
        CustomPrint(
            'Device is offline, try turning off USB debugging and turn on again.', 'yellow')
        Exit()
    if(deviceToConnect.split()[1] == 'unauthorized'):
        CustomPrint(
            'Device unauthorized. Please check the confirmation dialog on your device.', 'red')
        Exit()
    return deviceToConnect.split()[0]
def WindowsUSB(adb):
    CustomPrint('Connected to ' +
                getoutput(adb + ' shell getprop ro.product.model'))
    return AfterConnect(adb)
def Exit():
    print('\n')
    CustomPrint('Exiting...')
    os.system('bin\\adb.exe kill-server')
    CustomInput('Hit \'Enter\' key to continue....', 'cyan')
    quit()
def Exit():
    print('\n')
    CustomPrint('Exiting...')
    CustomInput('Hit \'Enter\' key to continue....', 'cyan')
    quit()
def LinuxUSB(ADBSerialId):
    CustomPrint('Connected to ' + getoutput('adb -s ' + ADBSerialId +
                                            ' shell getprop ro.product.model'))
    return AfterConnect(ADBSerialId)
def WindowsUSB(ADBSerialId) : 
    deviceName= adb + ADBSerialId + ' shell getprop ro.product.model'
    CustomPrint('Connected to ' + re.search("(?<=b')(.*)(?=\\\\r)", str(check_output(deviceName))).group(1))
    return AfterConnect(ADBSerialId)
def Exit():
    CustomPrint('\nExiting...')
    os.system('bin\\adb.exe kill-server')
    quit()