def checkLogin():
    if not isNull(USER.accessToken):
        #print('Checking Access Token...') #add to translations
        msg, check = API.verifyAccessToken(USER.accessToken)
        if check == True:
            Printf.info(
                LANG.MSG_VALID_ACCESSTOKEN.format(
                    displayTime(int(USER.expiresAfter - time.time()))))
            return
        else:
            Printf.info(LANG.MSG_INVAILD_ACCESSTOKEN)
            msg, check = API.refreshAccessToken(USER.refreshToken)
            if check == True:
                Printf.success(
                    LANG.MSG_VALID_ACCESSTOKEN.format(
                        displayTime(int(API.key.expiresIn))))
                USER.userid = API.key.userId
                USER.countryCode = API.key.countryCode
                USER.accessToken = API.key.accessToken
                USER.expiresAfter = time.time() + int(API.key.expiresIn)
                UserSettings.save(USER)
                return
            else:
                Printf.err(msg)
                tmp = UserSettings()  #clears saved tokens
                UserSettings.save(tmp)
    login()
    return
def autoGetAccessToken():
    array = API.tryGetAccessToken(USER.userid)
    if len(array) <= 0:
        return
    for item in array:
        msg, check = API.loginByAccessToken(item, USER.userid)
        if check == False:
            continue
        if item != USER.assesstoken:
            USER.assesstoken = item
            UserSettings.save(USER)
            Printf.info("Auto get accesstoken from tidal cache success!")
            return
def setAccessToken():
    while True:
        print("-------------AccessToken---------------")
        token = Printf.enter("accessToken('0' go back):")
        if token == '0':
            return
        msg, check = API.loginByAccessToken(token, USER.userid)
        if check == False:
            Printf.err(msg)
            continue
        break

    USER.assesstoken = token
    UserSettings.save(USER)
def login(username="", password=""):
    while True:
        if isNull(username) or isNull(password):
            print("---------------" + LANG.CHOICE_LOGIN + "-----------------")
            username = Printf.enter(LANG.PRINT_USERNAME)
            password = Printf.enter(LANG.PRINT_PASSWORD)
        msg, check = API.login(username, password, TOKEN1)
        if check == False:
            Printf.err(msg)
            username = ""
            password = ""
            continue
        api2 = TidalAPI()
        msg, check = api2.login(username, password, TOKEN2)
        break

    USER.username = username
    USER.password = password
    USER.userid = API.key.userId
    USER.countryCode = API.key.countryCode
    USER.sessionid1 = API.key.sessionId
    USER.sessionid2 = api2.key.sessionId
    UserSettings.save(USER)
def login():
    print(LANG.AUTH_START_LOGIN)
    msg, check = API.getDeviceCode()
    if check == False:
        Printf.err(msg)
        return

    print(LANG.AUTH_LOGIN_CODE.format(API.key.userCode))
    print(
        LANG.AUTH_NEXT_STEP.format(API.key.verificationUrl,
                                   displayTime(API.key.authCheckTimeout)))
    print(LANG.AUTH_WAITING)
    start = time.time()
    elapsed = 0
    while elapsed < API.key.authCheckTimeout:
        elapsed = time.time() - start
        msg, check = API.checkAuthStatus()
        if check == False:
            if msg == "pending":
                time.sleep(API.key.authCheckInterval)
                continue
            Printf.err(msg)
            break
        if check == True:
            Printf.success(
                LANG.MSG_VALID_ACCESSTOKEN.format(
                    displayTime(int(API.key.expiresIn))))
            USER.userid = API.key.userId
            USER.countryCode = API.key.countryCode
            USER.accessToken = API.key.accessToken
            USER.refreshToken = API.key.refreshToken
            USER.expiresAfter = time.time() + int(API.key.expiresIn)
            UserSettings.save(USER)
            break
    if elapsed >= API.key.authCheckTimeout:
        Printf.err(LANG.AUTH_TIMEOUT)
    return
def mainCommand():
    try:
        opts, args = getopt.getopt(sys.argv[1:], "ho:l:v:u:p:a:q:r", [
            "help", "output=", "link=", "version", "username", "password",
            "accessToken", "quality", "resolution"
        ])
        link = None
        for opt, val in opts:
            if opt in ('-h', '--help'):
                Printf.usage()
                return
            if opt in ('-v', '--version'):
                Printf.logo()
                return
            if opt in ('-l', '--link'):
                link = val
            if opt in ('-o', '--output'):
                CONF.downloadPath = val
            if opt in ('-u', '--username'):
                USER.username = val
                UserSettings.save(USER)
            if opt in ('-p', '--password'):
                USER.password = val
                UserSettings.save(USER)
            if opt in ('-a', '--accessToken'):
                USER.assesstoken = val
                UserSettings.save(USER)
            if opt in ('-q', '--quality'):
                CONF.audioQuality = Settings.getAudioQuality(val)
            if opt in ('-r', '--resolution'):
                CONF.videoQuality = Settings.getVideoQuality(val)

        if link is None:
            Printf.err(
                "Please enter the link(url/id/path)! Enter 'tidal-dl -h' for help!"
            )
            return
        if not mkdirs(CONF.downloadPath):
            Printf.err(LANG.MSG_PATH_ERR + CONF.downloadPath)
            return

        checkLogin()
        start(USER, CONF, link)
        return
    except getopt.GetoptError:
        Printf.err("Argv error! Enter 'tidal -h' for help!")
from aigpy.stringHelper import isNull
from aigpy.pathHelper import mkdirs
from aigpy.pipHelper import getLastVersion
from aigpy.versionHelper import cmpVersion

from tidal_dl.tidal import TidalAPI
from tidal_dl.settings import Settings, UserSettings
from tidal_dl.printf import Printf, VERSION
from tidal_dl.download import start
from tidal_dl.enum import AudioQuality, VideoQuality
from tidal_dl.lang.language import getLang, setLang, initLang

ssl._create_default_https_context = ssl._create_unverified_context

API = TidalAPI()
USER = UserSettings.read()
CONF = Settings.read()
LANG = initLang(CONF.language)


def displayTime(seconds, granularity=2):
    result = []
    intervals = (
        ('weeks', 604800),
        ('days', 86400),
        ('hours', 3600),
        ('minutes', 60),
        ('seconds', 1),
    )

    for name, count in intervals:
Example #8
0
 def __init__(self):
     self.api = TidalAPI()
     self.api2 = None
     self.user = UserSettings.read()
     self.token1, self.token2 = self.api.getToken()
     self.checkLogin()