test aggiornamento automatico domini

This commit is contained in:
marco
2020-11-26 21:12:39 +01:00
parent 94f5b13012
commit 1ea7b8b016
3 changed files with 105 additions and 61 deletions

26
.github/workflows/updateDomains.yml vendored Normal file
View File

@@ -0,0 +1,26 @@
name: Update channel domains
on:
workflow_dispatch:
schedule:
- cron: '30 17 * * *'
jobs:
update:
runs-on: ubuntu-latest
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: 2.7
- name: Run tests
run: |
python tests/test_generic.py
- uses: mikeal/publish-to-github-action@master
env:
GITHUB_TOKEN: ${{ secrets.API_TOKEN_GITHUB }}
BRANCH_NAME: '' #optional defaults to master

View File

@@ -2,7 +2,6 @@
import xbmc, xbmcgui
import xbmcaddon
import json
from platformcode import config, logger
import requests
import sys
@@ -236,63 +235,3 @@ def test_conn(is_exit, check_dns, view_msg,
# else:
# return False
# def for creating the channels.json file
def check_channels(inutile=''):
"""
I read the channel hosts from the channels.json file, I check them,
I write the channels-test.json file with the error code and the new url in case of redirect
urls MUST have http (s)
During the urls check the ip, asdl and dns checks are carried out.
This is because it can happen that at any time the connection may have problems. If it does, check it
relative writing of the file is interrupted with a warning message
"""
logger.info()
folderJson = xbmc.translatePath(xbmcaddon.Addon().getAddonInfo('path')).decode('utf-8')
fileJson = 'channels.json'
with open(folderJson+'/'+fileJson) as f:
data = json.load(f)
risultato = {}
for chann, host in sorted(data['direct'].items()):
ris = []
# to get an idea of the timing
# useful only if you control all channels
# for channels with error 522 about 40 seconds are lost ...
logger.info("check #### INIZIO #### channel - host :%s - %s " % (chann, host))
rslt = Kdicc(lst_urls = [host]).http_Resp()
# all right
if rslt['code'] == 200:
risultato[chann] = host
# redirect
elif str(rslt['code']).startswith('3'):
# risultato[chann] = str(rslt['code']) +' - '+ rslt['redirect'][:-1]
if rslt['redirect'].endswith('/'):
rslt['redirect'] = rslt['redirect'][:-1]
risultato[chann] = rslt['redirect']
# non-existent site
elif rslt['code'] == -2:
risultato[chann] = 'Host Sconosciuto - '+ str(rslt['code']) +' - '+ host
# site not reachable - probable dns not set
elif rslt['code'] == 111:
risultato[chann] = ['Host non raggiungibile - '+ str(rslt['code']) +' - '+ host]
else:
# other types of errors
# risultato[chann] = 'Errore Sconosciuto - '+str(rslt['code']) +' - '+ host
risultato[chann] = host
logger.info("check #### FINE #### rslt :%s " % (rslt))
risultato = {'findhost': data['findhost'], 'direct': risultato}
fileJson_test = 'channels-test.json'
# I write the updated file
with open(folderJson+'/'+fileJson_test, 'w') as f:
data = json.dump(risultato, f, sort_keys=True, indent=4)
logger.info(data)

79
tools/updateDomains.py Normal file
View File

@@ -0,0 +1,79 @@
import json, os, sys
import socket
path = os.getcwd()
sys.path.insert(0, path)
if sys.version_info[0] >= 3:
from lib.httplib2 import py3 as httplib2
else:
from lib.httplib2 import py2 as httplib2
def http_Resp(lst_urls):
rslt = {}
for sito in lst_urls:
try:
s = httplib2.Http()
code, resp = s.request(sito, body=None)
if code.previous:
print("r1 http_Resp: %s %s %s %s" %
(code.status, code.reason, code.previous['status'],
code.previous['-x-permanent-redirect-url']))
rslt['code'] = code.previous['status']
rslt['redirect'] = code.previous['-x-permanent-redirect-url']
rslt['status'] = code.status
else:
rslt['code'] = code.status
except httplib2.ServerNotFoundError as msg:
# both for lack of ADSL and for non-existent sites
rslt['code'] = -2
except socket.error as msg:
# for unreachable sites without correct DNS
# [Errno 111] Connection refused
rslt['code'] = 111
except:
rslt['code'] = 'Connection error'
return rslt
if __name__ == '__main__':
fileJson = 'channels.json'
with open(fileJson) as f:
data = json.load(f)
result = data['direct']
for chann, host in sorted(data['direct'].items()):
# to get an idea of the timing
# useful only if you control all channels
# for channels with error 522 about 40 seconds are lost ...
print("check #### INIZIO #### channel - host :%s - %s " % (chann, host))
rslt = http_Resp([host])
# all right
if rslt['code'] == 200:
result[chann] = host
# redirect
elif str(rslt['code']).startswith('3'):
# result[chann] = str(rslt['code']) +' - '+ rslt['redirect'][:-1]
if rslt['redirect'].endswith('/'):
rslt['redirect'] = rslt['redirect'][:-1]
result[chann] = rslt['redirect']
# non-existent site
elif rslt['code'] == -2:
print('Host Sconosciuto - '+ str(rslt['code']) +' - '+ host)
# site not reachable
elif rslt['code'] == 111:
print('Host non raggiungibile - '+ str(rslt['code']) +' - ' + host)
else:
# other types of errors
print('Errore Sconosciuto - '+str(rslt['code']) +' - '+ host)
print("check #### FINE #### rslt :%s " % (rslt))
result = {'findhost': data['findhost'], 'direct': result}
# I write the updated file
with open(fileJson, 'w') as f:
json.dump(result, f, sort_keys=True, indent=4)