コード例 #1
0
ファイル: episodes.py プロジェクト: dkluis/TVMaze-Management
def find_shows_not_followed_or_skipped():
    logfile = logging(caller='Episodes - Shows Not Followed',
                      filename='Episodes without Shows')
    episodes_found = execute_tvm_request(api=tvmaze_apis.get_episodes_status,
                                         code=True,
                                         sleep=0)
    eps_to_process = episodes_found.json()
    logfile.write(f'Number of Episodes to Process {len(eps_to_process)}')
    log.write(f'Number of Episodes to Process {len(eps_to_process)}')
    base_url = tvmaze_apis.get_episodes_status + '/'
    base_show = ''
    base_epi = -1
    for epi in eps_to_process:
        if vli > 2:
            logfile.write(f'Processing: {epi["episode_id"]}', 2)
        sql = f'select e.showid, s.status ' \
              f'from episodes e join shows s on e.showid = s.showid ' \
              f'where e.epiid = {epi["episode_id"]}'
        show_info = db.execute_sql(sqltype='Fetch', sql=sql)
        if epi['episode_id'] == base_epi + 1:
            base_epi = epi['episode_id']
            continue
        if not show_info:
            api = base_url + str(epi['episode_id'])
            result = execute_tvm_request(api=api, req_type='get', sleep=2.25)
            if not result:
                logfile.write(f'Could not get info from TVMaze for {api}')
                base_epi = epi['episode_id']
                continue
            page = Soup(result.content, 'html.parser')
            show_page = page.findAll('div', {"id": "general-info-panel"})
            link_page = show_page[0].findAll('a')
            for link in link_page:
                split_link = str(link).split('/')
                if split_link[1] == 'shows':
                    show_id = split_link[2]
                    if base_show != show_id:
                        base_show = show_id
                        logfile.write(
                            f'>>>>>>>>>>>>>>>>>>>>>> Found a showid: {show_id} to follow'
                        )
                        update_tvm_show_status(show_id, logfile)
                        episode_processing(show_id)
                    break
            base_epi = epi['episode_id']
            continue
        if len(show_info) != 1:
            logfile.write(
                f'Episode Info {epi} found multiple records >>>>>>>>>>>>>> {show_info}',
                0)
            base_epi = epi['episode_id']
            continue
コード例 #2
0
def func_get_tvmaze_show_info(showid):
    showinfo = execute_tvm_request(
        f'{tvmaze_apis.get_episodes_by_show_pre}{showid}',
        timeout=(20, 10),
        return_err=True)
    if not showinfo:
        log.write(f'Error with API call {showinfo}', 0)
        return
    if 'Error Code:' in showinfo:
        log.write(f'This show gives an error: {showid} {showinfo}')
        if "404" in showinfo:
            log.write(f'Now Deleting: {showid}')
            sql_tvm = f'delete from shows where `showid` = {showid}'
            result = db.execute_sql(sqltype='Commit', sql=sql_tvm)
            log.write(f'Delete result: {result}')
        return

    showinfo = showinfo.json()
    sql_shows = f"update shows " \
                f"set showstatus = '{showinfo['status']}', " \
                f"premiered = '{showinfo['premiered']}', " \
                f"language = '{showinfo['language']}', " \
                f"thetvdb = '{showinfo['externals']['thetvdb']}', " \
                f"imdb = '{showinfo['externals']['imdb']}' " \
                f"where `showid` = {showid}"
    result = db.execute_sql(sqltype='Commit', sql=sql_shows)
    if not result:
        log.write(f'Error when updating show {showid} {result}', 0)
コード例 #3
0
ファイル: shows.py プロジェクト: dkluis/TVMaze-Management
def process_all_shows(start, end, sync):
    ind = start  # Enter the last page processed
    while ind <= end:  # Paging is going up to 250  # Remember last page processed here: 197
        if vli > 3:
            log.write(f'Processing TVMaze Page: {ind}', 4)
        req = tvmaze_apis.get_shows_by_page + str(ind)
        response = execute_tvm_request(api=req, err=False)
        if vli > 3:
            log.write(f'Response to page request {ind} is {response}', 4)
        if response.status_code == 404:
            log.write(f'Last TVMaze page found was: {ind - 1}', 4)
            break
        for res in response.json():
            result = mariadb.execute_sql(sql="SELECT * from shows WHERE showid = {0}".format(res['id']),
                                         sqltype="Fetch")
            if not result:
                if vli > 2:
                    log.write(f'Inserting: {res["id"]}, {res["name"]}', 3)
                # insert_show(res)
            else:
                if sync:
                    if vli > 2:
                        log.write(f'Syncing: {res["id"]}, {res["name"]}', 3)
                    # update_show(res)
                else:
                    if vli > 3:
                        log.write(f'Found: {res["id"]}, {res["name"]}', 4)
        ind = ind + 1
コード例 #4
0
def update_tvmaze_episode_status(epiid):
    """
                           Function to update TVMaze
    :param epiid:          The episode to update
    :return:               Response from TVMaze or False if episode was updated before
    """
    status_sql = f'select epiid, mystatus from episodes where epiid = {epiid}'
    result = db.execute_sql(sql=status_sql, sqltype='Fetch')
    if not result:
        log.write(
            f'Episode could not be found {epiid} #############################',
            0)
        return False
    else:
        result = result[0]
    if result[1] == 'Downloaded' or result[1] == 'Watched':
        log.write(
            f'This episode {epiid} has already been update with "{result[1]}"')
        return False
    if vli > 2:
        log.write(f"Updating TVMaze for: {epiid}", 3)
    baseurl = tvmaze_apis.update_episode_status + str(epiid)
    epoch_date = int(date.today().strftime("%s"))
    data = {"marked_at": epoch_date, "type": 1}
    return execute_tvm_request(baseurl,
                               data=data,
                               req_type='put',
                               code=True,
                               log_ind=True)
コード例 #5
0
def get_eztv_api_options(imdb_id, seas, showname):
    download_options = []
    if not imdb_id:
        return download_options
    eztv_show = imdb_id
    eztv_url = db.execute_sql(
        sqltype='Fetch',
        sql='SELECT link_prefix FROM download_options '
        'where `providername` = "eztvAPI"')[0][0] + eztv_show[2:]
    eztv_req_result = execute_tvm_request(api=eztv_url,
                                          timeout=(20, 20),
                                          err=False)
    if not eztv_req_result:
        return download_options
    eztv_data = eztv_req_result.json()
    if vli > 2:
        log.write(f'Actions: Checking eztvAPi url: {eztv_url}', 3)
    eztv_count = eztv_data['torrents_count']
    if eztv_count == 0:
        return download_options
    eztv_epis = eztv_data['torrents']
    for eztv_epi in eztv_epis:
        filename = str(eztv_epi['filename']).lower()
        mag_url = eztv_epi['magnet_url']
        size = float(eztv_epi['size_bytes']) / 1000000
        size = int(size)
        size = str(size).zfill(6)
        prio = validate_requirements(filename, True, seas, showname, 10)
        if vli > 2:
            log.write(
                f'Checking filename eztvAPI {filename} with {seas} got prio {prio}',
                3)
        if prio > 100:
            download_options.append((prio, filename, mag_url, size, 'eztvAPI'))
    return download_options
コード例 #6
0
def get_piratebay_api_options(show, seas):
    piratebay_titles = []
    # api = 'https://piratebay.bid/s/?q=' + str(show).replace(' ', '+') + '+' + seas
    pb_info = db.execute_sql(sqltype='Fetch',
                             sql=f'SELECT * from download_options '
                             f'WHERE `providername` = "piratebay"')[0]
    api = f"{pb_info[1]}{show} {seas}"
    piratebay_data = execute_tvm_request(api=api, timeout=(20, 20))
    if not piratebay_data:
        return piratebay_titles
    piratebay_page = Soup(piratebay_data.content, 'html.parser')
    piratebay_table = piratebay_page.findAll('table', {'id': 'searchResult'})
    if len(piratebay_table) == 0:
        return piratebay_titles
    piratebay_table = piratebay_table[0]
    pb_table = piratebay_table.findAll('tr')
    magnet_link = ''
    size = 0
    for pb_table_rec in pb_table:
        cl_dl = pb_table_rec.findAll('a', {'class': 'detLink'})
        showname = ''
        showl = ''
        shownamel = ''
        shownamenp = ''
        for cl_dl_rec in cl_dl:
            showname = (cl_dl_rec['title'])
            showl = show.lower().replace(" ", ".")
            shownamel = showname.lower()
            shownamenp = shownamel.replace('+', '.')
        if showl.replace('.', ' ') in shownamel or showl in shownamenp:
            show_refs = pb_table_rec.findAll('a',
                                             {'href': re.compile("^magnet:")})
            for ref in show_refs:
                magnet_link = str(ref['href'])
                infos = pb_table_rec.findAll('font', {'class': 'detDesc'})
                for info in infos:
                    sub_info = str(info).split('<font class="detDesc">')
                    sub_info = sub_info[1].split(', ULed by')
                    sub_info = sub_info[0].replace('Uploaded ',
                                                   '').replace(' Size ', '')
                    sub_info = sub_info.split(',')
                    size = str(sub_info[1]).replace('\xa0', '')
                    if 'MiB' in size:
                        size = str(size).split('MiB')
                        size_multiplier = 1
                    elif 'GiB' in size:
                        size = str(size).split('GiB')
                        size_multiplier = 1000
                    else:
                        size_multiplier = 0
                    sizem = float(size[0]) * size_multiplier
                    sizem = int(sizem)
                    size = str(sizem).zfill(6)
        showname = showname.replace('Details for ', '')
        prio = validate_requirements(showname, False, seas, show, 15)
        if prio > 100:
            piratebay_titles.append(
                (prio, showname, magnet_link, size, 'piratebay'))
    return piratebay_titles
コード例 #7
0
def update_tvmaze_episode_status(epiid):
    api = tvmaze_apis.update_episode_status + str(epiid)
    epoch_date = int(date.today().strftime("%s"))
    data = {"marked_at": epoch_date, "type": 1}
    response = execute_tvm_request(api=api,
                                   req_type='put',
                                   code=True,
                                   data=data,
                                   err=True)
    if not response:
        return False
    return True
コード例 #8
0
def update_tvmaze_episode_status(epiid, upd_date):
    if vli > 1:
        log.write(f'Update TVM Episode Status: {epiid}, {upd_date}', 2)
    baseurl = tvmaze_apis.get_episodes_status + '/' + str(epiid)
    if upd_date:
        epoch_date = int(upd_date.strftime("%s"))
    else:
        epoch_date = int(date.today().strftime("%s"))
    data = {"marked_at": epoch_date, "type": 0}
    response = execute_tvm_request(baseurl,
                                   data=data,
                                   req_type='put',
                                   code=True,
                                   log_ind=do_log)
    return response
コード例 #9
0
def get_eztv_options(show, seas):
    eztv_titles = []
    api = db.execute_sql(
        sqltype='Fetch',
        sql='SELECT link_prefix FROM download_options '
        'where `providername` = "eztv"')[0][0] + show + '-' + seas
    eztv_data = execute_tvm_request(api=api, timeout=(20, 20), err=False)
    if not eztv_data:
        return eztv_titles
    eztv_page = Soup(eztv_data.content, 'html.parser')
    eztv_table = eztv_page.findAll('a', {'class': 'magnet'})
    if len(eztv_table) == 0:
        return eztv_titles
    dloptions = []
    for rec in eztv_table:
        if show.lower() in str(rec).lower() and seas.lower() in str(
                rec).lower():
            dloptions.append(f"***{rec}***")
    if len(dloptions) == 0:
        return dloptions
    for dlo in dloptions:
        size = 0
        split1 = str(dlo).split('href="')[1]
        magnet_link = str(split1).split('" rel=')[0]
        split1 = str(magnet_link).split(';dn=')[1]
        showname = str(split1).split('%')[0]
        if ' (' in str(dlo):
            split1 = str(dlo).split(' (')[1]
            sizeraw = str(split1).split(') ')[0]
            s = sizeraw.split(' ')
            if s[1] == 'MB':
                size = float(s[0])
                size = int(size)
                size = str(size).zfill(6)
            elif s[1] == 'GB':
                size = float(s[0]) * 1000
                size = int(size)
        size = str(size).zfill(6)
        if vli > 2:
            log.write(f"{dlo} \n Validating {showname}, True, {seas}, {show} ",
                      3)
        prio = validate_requirements(showname, True, seas, show, 10)
        if vli > 2:
            log.write(f'Prio returned {prio} for {showname}', 3)
        if prio > 100:
            prio = prio - 5
            eztv_titles.append((prio, showname, magnet_link, size, 'eztv'))
    return eztv_titles
コード例 #10
0
ファイル: episodes.py プロジェクト: dkluis/TVMaze-Management
def update_tvm_show_status(showid, logfile):
    logfile.write(f'TVMaze update {showid}')
    api = f'{tvmaze_apis.update_followed_shows}/{showid}'
    result = execute_tvm_request(api, req_type='put', code=True)
    if not result:
        logfile.write(f"Web error trying to follow show: {showid}")
    sql = f'select showid, status from shows where showid = {showid}'
    result = db.execute_sql(sqltype='Fetch', sql=sql)
    if result:
        if result[0][1] != 'Followed':
            sql = f'update shows set status = "Followed", download = "Skip" where `showid` = {showid}'
            result = db.execute_sql(sqltype='Commit', sql=sql)
            if not result:
                logfile.write(
                    f'Update in the DB did not work: {sql}, {result}')
        else:
            logfile.write(f'Show {showid} was already followed')
    else:
        logfile.write(f'Show {showid} not found')
コード例 #11
0
def update_tvm_epis(epis_to_update: list, reason: str, upd_date: datetime):
    if reason == 'Downloaded':
        tvm_type = 1
    elif reason == 'Watched':
        tvm_type = 0
    else:
        tvm_type = 2
    epiid = epis_to_update[0][1]
    baseurl = f'{tvmaze_apis.get_episodes_status}/{str(epiid)}'
    if upd_date != '':
        epoch_date = int(
            datetime.datetime.strptime(upd_date, '%Y-%m-%d').strftime("%s"))
    else:
        epoch_date = int(datetime.datetime.today().strftime("%s"))
    data = {"marked_at": epoch_date, "type": tvm_type}
    response = execute_tvm_request(baseurl,
                                   data=data,
                                   req_type='put',
                                   code=True)
    return response
コード例 #12
0
def get_rarbg_api_options(show, seas):
    dl_options = []
    dl_info = db.execute_sql(sqltype='Fetch',
                             sql=f'SELECT * from download_options '
                             f'WHERE `providername` = "rarbgAPI"')[0]
    main_link = f"{dl_info[1]}{show} {seas}{dl_info[2]}"
    main_request = execute_tvm_request(api=main_link, req_type='get')
    if main_request:
        main_request = main_request.json()
    else:
        return dl_options
    if f'No results found' in str(main_request):
        return dl_options
    if vli > 4:
        log.write(f'Found main_request {main_request}', 5)
    records = main_request['torrent_results']
    for record in records:
        name = record['title']
        magnet = record['download']
        size = str(int(record['size'] / 1000000)).zfill(6)
        prio = validate_requirements(name, True, seas, show, 20)
        if prio > 100:
            dl_options.append((prio, name, magnet, size, 'rarbgAPI'))
    return dl_options
コード例 #13
0
ファイル: episodes.py プロジェクト: dkluis/TVMaze-Management
def episode_processing(single=''):
    started = timer()
    api = ''
    if single == '':
        if vli > 1:
            log.write(
                f'Starting to process recently updated episodes for insert and re-sync'
            )
        shows_sql = "SELECT * FROM shows " \
                    "where status = 'Followed' and (record_updated = current_date or eps_updated is Null)"
        shows_sql = shows_sql.replace('None', 'Null')
        result = db.execute_sql(sqltype='Fetch', sql=shows_sql)
    else:
        # shows = [(32, "Fargo")]
        # show_num = (len(shows))
        result = [(single, 'A Show')]
        log.write(f'Starting to process Single Show: {single}')
    total_episodes = 0
    updated = 0
    inserted = 0
    for show in result:
        api = f"{tvmaze_apis.get_episodes_by_show_pre}{show[0]}{tvmaze_apis.get_episodes_by_show_suf}"
        episodes = execute_tvm_request(api=api, sleep=0.5)
        if not episodes:
            continue
        episodes = episodes.json()
        num_eps = len(episodes)
        total_episodes = total_episodes + num_eps
        for epi in episodes:
            result = db.execute_sql(
                sql="SELECT * from episodes WHERE epiid = {0}".format(
                    epi['id']),
                sqltype="Fetch")
            # Section move to avoid duplicate code
            if len(epi['name']) > 130:
                epiname = epi['name'][:130]
            else:
                epiname = epi['name']
            if len(epi['url']) > 150:
                epiurl = epi['url'][:150]
            else:
                epiurl = epi['url']
            # end section
            if len(result) == 0:
                if epi['airdate'] == '':
                    airdate = None
                else:
                    airdate = f"'{epi['airdate']}'"
                # Had Section
                sql = generate_insert_sql(
                    table='episodes',
                    primary=epi['id'],
                    f1=(1, show[0]),
                    f2=(2, f'''"{str(epiname).replace('"', ' ')}"'''),
                    f3=(3, f"'{epiurl}'"),
                    f4=(4, epi['season']),
                    f5=(5, epi['number']),
                    f6=(6, airdate),
                    f7=(7, None),
                    f8=(8, None),
                    f9=(9, f"'{str(datetime.now())[:10]}'"))
                sql = sql.replace("'None'", 'NULL').replace('None', 'NULL')
                db.execute_sql(sql=sql, sqltype='Commit')
                inserted += 1
            elif len(result) == 1:
                if vli > 3:
                    log.write(f'Working on EPI: {epi["id"]}', 4)
                # Had Section
                if epi['airdate'] is None or epi['airdate'] == '':
                    sql = generate_update_sql(
                        epiname=str(epiname).replace('"', ' '),
                        url=epiurl,
                        season=epi['season'],
                        episode=epi['number'],
                        rec_updated=f"'{str(datetime.now())[:10]}'",
                        where=f"epiid={epi['id']}",
                        table='episodes')
                else:
                    sql = generate_update_sql(
                        epiname=str(epiname).replace('"', ' '),
                        url=epiurl,
                        season=epi['season'],
                        episode=epi['number'],
                        airdate=f"'{epi['airdate']}'",
                        rec_updated=f"'{str(datetime.now())[:10]}'",
                        where=f"epiid={epi['id']}",
                        table='episodes')
                sql = sql.replace('None', 'NULL')
                db.execute_sql(sql=sql, sqltype='Commit')
                updated += 1
            else:
                log.write(
                    f"Found more than 1 record for {epi['id']} episode which should not happen",
                    0)
                db.close()
                log.end()
                quit()
            if (updated + inserted) % 250 == 0:
                if vli > 2:
                    log.write(f'Processed {updated + inserted} records', 3)
        if vli > 2:
            log.write(f'Do Show update for {show[0]}', 3)
        db.execute_sql(
            sqltype='Commit',
            sql=f'UPDATE shows '
            f'set eps_updated = current_date, eps_count = {num_eps} '
            f'WHERE showid = {show[0]}')

    log.write(
        f"Updated existing episodes: {updated} and Inserted new episodes: {inserted}"
    )
    log.write(f"Total number of shows: {len(result)}")
    log.write(f"Total number of episodes: {total_episodes}")
    ended = timer()
    log.write(
        f'The process (including calling the TVMaze APIs) took: {ended - started} seconds'
    )

    log.write(f"Starting update of episode statuses and date")
    episodes = execute_tvm_request(api=tvmaze_apis.get_episodes_status,
                                   code=True,
                                   sleep=0,
                                   err=True)
    if not episodes:
        log.write(f'API: {api} failed')
        return
    elif "Error Code" in episodes:
        log.write(f'Api call {api} resulted with: {episodes}')
        return

    # epis = str(episodes.content, encoding='utf-8').replace('null', '')
    # eps_updated = epis.replace('[', '').replace(']', '')
    # eps_updated = ast.literal_eval(eps_updated)
    eps_updated = episodes.json()
    updated = 0
    if vli > 2:
        log.write(f"Episodes to process: {len(eps_updated)}", 3)
    for epi in eps_updated:
        if epi['type'] == 0:
            watch = "Watched"
        elif epi['type'] == 1:
            watch = "Downloaded"
        else:
            watch = "Skipped"
        if epi['marked_at'] == 0:
            when = None
        else:
            when = date.fromtimestamp(epi['marked_at'])
        if when is None:
            sql = generate_update_sql(
                mystatus=watch,
                mystatus_date=None,
                rec_updated=f"'{str(datetime.now())[:10]}'",
                where=f"epiid={epi['episode_id']}",
                table='episodes')
        else:
            sql = generate_update_sql(
                mystatus=watch,
                mystatus_date=f"'{when}'",
                rec_updated=f"'{str(datetime.now())[:10]}'",
                where=f"epiid={epi['episode_id']}",
                table='episodes')
        sql = sql.replace('None', 'NULL')
        db.execute_sql(sqltype='Commit', sql=sql)
        updated += 1
        if updated % 5000 == 0:
            if vli > 2:
                log.write(f"Processed {updated} records", 3)

    log.write(f"Total Episodes updated: {updated}")
    if single != '':
        log.write(f'Finished Single')
        return

    log.write(f'Starting to find episodes to reset')
    found = False
    result = db.execute_sql(sqltype='Fetch', sql=std_sql.episodes)
    count = 0
    ep_list = []
    for res in result:
        count += 1
        for epi in eps_updated:
            if epi['episode_id'] == res[0]:
                found = True
                break
            else:
                found = False
        if not found:
            ep_list.append(res[0])
        if count % 5000 == 0:
            if vli > 2:
                log.write(f'Processed {count} records', 3)

    log.write(f'Number of Episodes to reset is {len(ep_list)}')
    for epi in ep_list:
        result = db.execute_sql(sqltype='Commit',
                                sql=f'UPDATE episodes '
                                f'SET mystatus = NULL, mystatus_date = NULL '
                                f'WHERE epiid = {epi}')
        if not result:
            log.write(f'Epi reset for {epi} went wrong {result}')

    # Checking to see if there are any episodes with no status on TVMaze for Followed shows set to skip downloading
    # and tracking so that we can set them to skipped on TVMaze

    eps_to_update = db.execute_sql(sqltype='Fetch', sql=tvm_views.eps_to_check)
    if len(eps_to_update) != 0:
        log.write(f'There are {len(eps_to_update)} episodes to update')
        for epi in eps_to_update:
            baseurl = tvmaze_apis.get_episodes_status + '/' + str(epi[0])
            epoch_date = int(date.today().strftime("%s"))
            data = {"marked_at": epoch_date, "type": 2}
            response = execute_tvm_request(baseurl,
                                           data=data,
                                           req_type='put',
                                           code=True)
            if not response:
                log.write(
                    f'TVMaze update did not work: {baseurl}, {data} {response}'
                )
            else:
                log.write(
                    f'Updating Epi {epi[0]} as Skipped since the Show download is set to Skip'
                )
コード例 #14
0
ファイル: shows.py プロジェクト: dkluis/TVMaze-Management
def process_update_all_shows():
    response = execute_tvm_request(api=tvmaze_apis.get_updated_shows, err=True)
    if response:
        response = response.json()
    else:
        log.write(f"Response did not contain a json for API {tvmaze_apis.get_updated_shows}", 0)
        return
    if vli > 1:
        log.write(f'Number of Shows to potentially update {len(response)}', 2)
    updated = 0
    inserted = 0
    skipped = 0
    processed = 0
    batch = 0
    for key in response:
        processed = processed + 1
        if processed % 5000 == 0:
            if vli > 3:
                log.write(f"Processed {processed} records. ", 4)
        showid = key
        showupdated = response[key]
        result = mariadb.execute_sql(sql=f"SELECT * from shows WHERE showid = {showid}", sqltype="Fetch")
        if not result:
            showinfo = execute_tvm_request(f'{tvmaze_apis.get_episodes_by_show_pre}{showid}')
            if not showinfo:
                log.write(f'Working on {key} in response and cannot find the show info {showinfo}', 0)
                continue
            showinfo = showinfo.json()
            si = process_show_info(showinfo)
            sql = generate_insert_sql(
                table='shows',
                primary=showinfo['id'],
                f1=('quotes', f'{showinfo["name"]}'),
                f2=(2, f"'{showinfo['url']}'"),
                f3=(3, f"'{showinfo['type']}'"),
                f4=(4, f"'{showinfo['status']}'"),
                f5=(5, f"'{si['premiered']}'"),
                f6=(6, f"'{si['language']}'"),
                f7=(7, f"'{si['runtime']}'"),
                f8=(8, f"'{si['network']}'"),
                f9=(9, f"'{si['country']}'"),
                f10=(10, f"'{showinfo['externals']['tvrage']}'"),
                f11=(11, f"'{showinfo['externals']['thetvdb']}'"),
                f12=(12, f"'{showinfo['externals']['imdb']}'"),
                f13=(13, f"'{showinfo['updated']}'"),
                f14=(14, f"'{date.fromtimestamp(showinfo['updated'])}'"),
                f15=(15, f"'{si['interest']}'"),
                f16=(16, 'NULL'),
                f17=(17, f"'{str(datetime.now())[:10]}'"),
                f18=(18, f"'{transform_showname(showinfo['name'])}'"),
                f19=(19, None),
                f20=(20, None),
                f21=(21, None)
            )
            sql = sql.replace("'None'", 'NULL').replace('None', 'NULL')
            mariadb.execute_sql(sql=sql, sqltype='Commit')
            inserted = inserted + 1
        else:
            if len(result) != 1:
                log.write(f"Found too many records or not enough:{result}", 0)
                mariadb.close()
                quit()
            else:
                result = result[0]
            if result[13] == showupdated or result[15] != "Followed":
                skipped = skipped + 1
            else:
                showinfo = execute_tvm_request(f'{tvmaze_apis.get_episodes_by_show_pre}{showid}')
                if not showinfo:
                    log.write(f'Request timed-out, skipping update')
                    continue
                showinfo = showinfo.json()
                si = process_show_info(showinfo)
                if result[19] is None:
                    sql = generate_update_sql(tvmaze_updated=showupdated,
                                              tvmaze_upd_date=f"'{date.fromtimestamp(showupdated)}'",
                                              network=si['network'],
                                              country=si['country'],
                                              runtime=si['runtime'],
                                              language=si['language'],
                                              premiered=si['premiered'],
                                              showname=str(showinfo["name"]).replace('"', "'"),
                                              alt_showname=f'''{str(transform_showname(showinfo["name"]))
                                              .replace('"', "'")}''',
                                              tvrage=showinfo['externals']['tvrage'],
                                              thetvdb=showinfo['externals']['thetvdb'],
                                              imdb=showinfo['externals']['imdb'],
                                              showstatus=showinfo['status'],
                                              record_updated='current_date',
                                              where=f"showid={showid}",
                                              table='shows')
                else:
                    sql = generate_update_sql(tvmaze_updated=showupdated,
                                              tvmaze_upd_date=f"'{date.fromtimestamp(showupdated)}'",
                                              network=si['network'],
                                              country=si['country'],
                                              runtime=si['runtime'],
                                              language=si['language'],
                                              premiered=si['premiered'],
                                              showname=str(showinfo["name"]).replace('"', "'"),
                                              tvrage=showinfo['externals']['tvrage'],
                                              thetvdb=showinfo['externals']['thetvdb'],
                                              imdb=showinfo['externals']['imdb'],
                                              showstatus=showinfo['status'],
                                              record_updated='current_date',
                                              where=f"showid={showid}",
                                              table='shows')
                mariadb.execute_sql(sql=sql, sqltype='Commit')
                updated = updated + 1
                batch = batch + 1
                if batch % 100 == 0:
                    if vli > 3:
                        log.write(f"Commit of {batch} updated records", 4)
    if batch != 0:
        if vli > 3:
            log.write(f"Final Commit of updated records", 4)
    log.write(f"Processed {processed} records. ")
    log.write(f'Shows Evaluated: {len(response)} -> Inserted: {inserted} '
              f'-> Updated: {updated}, No Update Needed: {skipped}')
コード例 #15
0
db = mariaDB(caller=log.caller, filename=log.filename)

etu_sql = "select epiid, airdate from episodes where mystatus = 'Watched' and mystatus_date is NULL"
eps_to_update = db.execute_sql(sqltype='Fetch', sql=etu_sql)
log.write(f'Number of Episodes to update: {len(eps_to_update)}')
for eptu in eps_to_update:
    baseurl = tvmaze_apis.get_episodes_status + '/' + str(eptu[0])
    # baseurl = 'https://api.tvmaze.com/v1/user/episodes/' + str(eptu[0])
    if not eptu[1]:
        continue
    epoch_date = eptu[1].strftime('%s')
    if int(epoch_date) <= 0:
        continue
    data = {"marked_at": epoch_date, "type": 0}
    response = execute_tvm_request(baseurl,
                                   data=data,
                                   req_type='put',
                                   code=True)
    if response:
        log.write(
            f'Updated epi {eptu[0]} as watched on {eptu[1]}, with response {response}'
        )
    else:
        log.write(
            f'Failed to update --------> epi {eptu[0]} as watched on {eptu[1]}'
        )

db.close()
log.end()
quit()
コード例 #16
0
ファイル: shows.py プロジェクト: dkluis/TVMaze-Management
def process_followed_shows():
    result = execute_tvm_request(api=tvmaze_apis.get_followed_shows, code=True)
    if not result:
        result = ''
        log.write(f"Some error with the call to TVMaze occurred {tvmaze_apis.get_followed_shows}, {result}", 0)
        return
    result = result.json()
    found = False
    records = mariadb.execute_sql(sqltype='Fetch', sql=std_sql.followed_shows)
    count = 0
    nf_list = []
    for show in records:
        count += 1
        for res in result:
            if res['show_id'] == show[0]:
                found = True
                break
            else:
                found = False
        if not found:
            nf_list.append(show[0])
    new_followed = 0
    for res in result:
        validates = mariadb.execute_sql(sqltype='Fetch',
                                        sql=f'SELECT showname, status from shows where showid={res["show_id"]}')
        if validates[0][1] != 'Followed':
            new_followed += 1
            if vli > 2:
                log.write(f'Process Followed shows {validates[0][0]}  {validates[0][1]}', 3)
            download = mariadb.execute_sql(sqltype='Fetch',
                                           sql=f'SELECT info FROM key_values WHERE `key` = "def_dl"')[0][0]
            result = mariadb.execute_sql(sqltype='Commit', sql=f'UPDATE shows SET status="Followed", '
                                                               f'download="{download}" '
                                                               f'WHERE showid={res["show_id"]}')
            if not result:
                log.write(f'Update error on Shows table for show: '
                          f'{res["show_id"]} trying to make a followed show', 0)
                mariadb.close()
                quit()
            if vli > 1:
                log.write(f'Now following show {validates[0][0]}', 2)

    un_followed = 0
    for nf in nf_list:
        un_followed += 1
        result = mariadb.execute_sql(sqltype="Fetch", sql=f'SELECT showname, status from shows where showid={nf}')
        if not result:
            log.write(f'Read error in for nf loop on: {nf}', 0)
            mariadb.close()
            quit()
        if len(result) != 1:
            log.write(f'Did not get a single record for show in nf loop: {nf}', 0)
            mariadb.close()
            quit()
        showname = result[0][0]
        result = mariadb.execute_sql(sqltype='Commit', sql=f'DELETE FROM episodes WHERE showid={nf}')
        if not result:
            log.write(f'Delete error on Episodes table for show in nf loop: {nf}', 0)
            mariadb.close()
            quit()
        result = mariadb.execute_sql(sqltype='Commit',
                                     sql=f'UPDATE shows SET status="Skipped", download=NULL WHERE showid={nf}')
        if not result:
            log.write(f'Update error on Shows table for show in nf loop: {nf} trying to un-follow', 0)
            mariadb.close()
            quit()
        if vli > 1:
            log.write(f'Un-followed show {showname}', 2)

    log.write(f"Updates performed based on TVMaze Followed status.  "
              f"Un-followed: {un_followed} and new Followed: {new_followed}")
コード例 #17
0
def update_tvmaze_followed_shows(showid):
    api = tvmaze_apis.update_followed_shows = str(showid)
    response = execute_tvm_request(api=api, code=True, req_type='put')
    return response