def search(key):
    try:
        # 番号案内ページのURL
        url = 'https://www.telnavi.jp/phone/%s' % key
        # ページ読み込み
        res = urlopen(url)
        status = res.getcode()
        if status == 200:
            # タグ抽出
            html = res.read()
            soup = BeautifulSoup(html, 'html.parser')
            title = soup.find('title').get_text()
            # 電話番号08012000021の情報は?|電話番号検索の電話帳ナビ
            # 電話番号0120102030は司法書士法人新宿事務所
            if re.match(r'電話番号[0-9]+は', title):
                name = re.sub(r'電話番号[0-9]+は', '', title)
            else:
                name = None
        else:
            name = None
        res.close()
        Common.log('key=%s name=%s status=%s url=%s' %
                   (key, name, status, url))
    except Exception as e:
        name = None
        Common.log(e)
    return name
 def onRegState(self, param):
     # ログ
     Common.log('\n'.join([
         # https://www.pjsip.org/pjsip/docs/html/structpj_1_1OnRegStateParam.htm
         'status: %s' % param.status,
         'code: %s' % param.code,
         'reason: %s' % param.reason,
         # 'rdata: %s' % param.rdata,
         'rdata.info: %s' % param.rdata.info,
         'rdata.wholeMsg: \n-----\n%s\n-----' % '\n'.join(param.rdata.wholeMsg.strip().split('\r\n')),
         'rdata.srcAddress: %s' % param.rdata.srcAddress,
         'rdata.pjRxData: %s' % param.rdata.pjRxData,
         'expiration: %s' % param.expiration,
     ]))
     info = self.getInfo()
     Common.log('\n'.join([
         # https://www.pjsip.org/pjsip/docs/html/structpj_1_1AccountInfo.htm
         'id: %s' % info.id,
         'isDefault: %s' % info,
         'uri: %s' % info.uri,
         'regIsConfigured: %s' % info.regIsConfigured,
         'regIsActive: %s' % info.regIsActive,
         'regExpiresSec: %s' % info.regExpiresSec,
         'regStatus: %s' % info.regStatus,
         'regStatusText: %s' % info.regStatusText,
         'regLastErr: %s' % info.regLastErr,
         'onlineStatus: %s' % info.onlineStatus,
         'onlineStatusText: %s' % info.onlineStatusText,
     ]))
     # 通知
     if param.code == 200:
         Common.notify('Registered as SIP client', time=3000)
     else:
         Common.notify('SIP registration failed (%d)' % param.code, time=3000, error=True)
Esempio n. 3
0
def run(argv):
    plugin = Common(addon=xbmcaddon.Addon(), addon_handle=int(argv[1]))
    credential = Credential(plugin)
    content = Content(plugin, credential)

    params = dict(urllib_parse_qsl(argv[2][1:]))
    if 'action' in params:

        plugin.log('params = {0}'.format(params))

        if params.get('action') == 'listHome':
            content.listHome()
        elif params.get('action') == 'listSubnavi':
            content.listSubnavi(params.get('path'), params.get('hasitems'),
                                params.get('items_to_add'))
        elif params.get('action') == 'showVideos':
            content.showVideos(params.get('path'), params.get('show_videos'))
        elif params.get('action') == 'playVoD':
            content.playVoD(params.get('path'))
        elif params.get('action') == 'login':
            content.login()
        elif params.get('action') == 'logout':
            content.logout()
        elif params.get('action') == 'clearCache':
            content.clearCache()
    else:
        content.rootDir()
Esempio n. 4
0
 def favorites(self):
     # 検索
     response_body = Request().favorites(self.query)
     if response_body:
         response_data = json.loads(response_body)
         if response_data['status'] == 1:
             xbmc.executebuiltin('Container.Refresh')
         else:
             Common.log('switch favorites failed (status=%s)' % response_data['status'], error=True)
             Common.notify('Operation failed')
     else:
         Common.log('empty response', error=True)
         Common.notify('Operation failed')
def initializeChannel():
    # リセット
    Common.SET('garapon_ch', '')
    # チャンネル情報を取得
    response_body = Request().channel()
    if response_body:
        response_data = json.loads(response_body)
        if response_data['status'] == 1:
            # チャンネル情報をファイルに書き出す
            Common.write_json(Common.CHANNEL_FILE, response_data)
            # チャンネル数を設定
            Common.SET('garapon_ch', '%d channels' % len(response_data['ch_list'].keys()))
            # 設定画面のテンプレートを読み込む
            template = Common.read_file(Common.TEMPLATE_FILE)
            # テンプレートに書き出すジャンル情報
            genre = Genre().getLabel()
            # チャンネル情報とあわせてテンプレートに適用
            source = template.format(
                channel=Channel().getLabel(),
                g0=genre['g0'],
                g00=genre['g00'],
                g01=genre['g01'],
                g02=genre['g02'],
                g03=genre['g03'],
                g04=genre['g04'],
                g05=genre['g05'],
                g06=genre['g06'],
                g07=genre['g07'],
                g08=genre['g08'],
                g09=genre['g09'],
                g10=genre['g10'],
                g11=genre['g11']
            )
            # 設定画面をファイルに書き出す
            Common.write_file(Common.SETTINGS_FILE, source)
            # 完了
            Common.notify('Channel initialized successfully')
            return True
        else:
            Common.log('channel failed', response_body, error=True)
            Common.notify('Channel initialization failed')
            return False
    else:
        Common.log('empty response', error=True)
        Common.notify('Channel initialization failed')
        return False
Esempio n. 6
0
 def set_timer(self):
     # 現在時刻
     now = time.time()
     if now > self.next_aired:
         xbmc.executebuiltin('Container.Refresh')
         Common.log('updateOnAir: xbmc.executebuiltin')
     else:
         # 遅延を設定
         delay = self.next_aired - now + 30
         if delay < 0:
             delay = 0
         # idを設定
         Common.write_file(Common.RESUME_FILE, '')
         id = os.path.getmtime(Common.RESUME_FILE)
         # スレッドを起動
         threading.Timer(delay, self.check_onair, args=[id]).start()
         Common.log('updateOnAir: threading.Timer.start: %d %f' % (id, delay))
def initializeSession():
    # リセット
    Common.SET('garapon_session', '')
    # データ取得
    response_body = Request().auth()
    if response_body:
        response_data = json.loads(response_body)
        if response_data['status'] == 1:
            if response_data['login'] == 1:
                gtvsession = response_data['gtvsession']
                Common.SET('garapon_session', gtvsession)
                Common.notify('Session initialized successfully')
                return True
            else:
                Common.log('auth failed', response_body, error=True)
                Common.notify('Session initialization failed')
                return False
        else:
            Common.log('auth failed', response_body, error=True)
            Common.notify('Session initialization failed')
            return False
    else:
        Common.log('empty response', error=True)
        Common.notify('Session initialization failed')
        return False
def initializeNetwork():
    # リセット
    Common.SET('garapon_addr', '')
    Common.SET('garapon_http', '')
    Common.SET('garapon_https', '')
    # データ取得
    response_body = Request().getgtvaddress()
    if response_body:
        params = {}
        for i in response_body.split('\n'):
            try:
                (key, value) = i.split(';', 1)
                if key == '0':
                    params['message'] = value
                elif key == '1':
                    params['message'] = value
                else:
                    params[key] = value
            except Exception:
                pass
        if params['message'] == 'success':
            Common.SET('garapon_addr', params['ipaddr'])
            if params['ipaddr'] == params['gipaddr']:
                Common.SET('garapon_http', params['port'])
                Common.SET('garapon_https', params['port2'])
            else:
                Common.SET('garapon_http', '')
                Common.SET('garapon_https', '')
            Common.notify('Network initialized successfully')
            return True
        else:
            Common.log('getgtvaddress failed', response_body, error=True)
            Common.notify('Network initialization failed')
            return False
    else:
        Common.log('empty response', error=True)
        Common.notify('Network initialization failed')
        return False
Esempio n. 9
0
 def search(self, onair=False, retry=True):
     # 検索
     response_body = Request().search(self.query)
     if response_body:
         response_data = json.loads(response_body)
         if response_data['status'] == 1:
             # 検索結果の番組
             programs = response_data['program']
             if onair:
                 # 放送中の番組はチャンネル順
                 for item in sorted(programs, key=lambda item: item['ch']):
                     if item.get('ts') == 1:
                         self.add_item(item, onair)
                 # 放送中の番組の更新を設定
                 UpdateOnAir(programs).set_timer()
             else:
                 # 放送済みの番組は時間降順
                 for item in sorted(programs, key=lambda item: item['startdate'], reverse=True):
                     if item.get('ts') == 1:
                         self.add_item(item, onair)
             # 検索結果の続きがある場合は次のページへのリンクを表示
             hit = int(response_data['hit'])
             page = int(self.args.get('p'))
             if hit > page * Common.ITEMS:
                 self.args['p'] = page + 1
                 query = urlencode(self.args)
                 # 次のページへ
                 self.add_directory_item('[COLOR lightgreen]%s[/COLOR]' % (Common.STR(30922)), query, 'search', iconimage=Common.RIGHT)
             # end of directory
             xbmcplugin.endOfDirectory(int(sys.argv[1]))
         elif retry is True:
             # セッションを初期化してリトライする
             if initializeSession():
                 if checkSettings():
                     self.search(onair, retry=False)
                 else:
                     Common.log('invalid settings', error=True)
                     Common.notify('Search failed')
             else:
                 Common.log('session initialization failed', error=True)
                 Common.notify('Search failed')
         else:
             # リトライも失敗
             Common.log('retry failed', error=True)
             Common.notify('Search failed')
     else:
         Common.log('empty response', error=True)
         Common.notify('Search failed')
Esempio n. 10
0
 def receive(self, criterion):
     # メール取得
     mails = self.service.receive(criterion, 'TEXT')
     # 新規メールのリスト
     newmails = []
     senders = {}
     # メール毎の処理
     for mail in mails:
         # データ変換
         filename, timestamp = self.convert(mail)
         # ファイルパス
         filepath = os.path.join(self.cache_path, filename)
         if not os.path.isfile(filepath):
             content = ''
             # header
             for key in [
                     'From', 'To', 'CC', 'Date', 'Subject', 'Message-Id'
             ]:
                 if mail[key]:
                     content += '%s: %s\r\n' % (key, mail[key].replace(
                         '\r\n', ''))
             # separator
             content += '\r\n'
             # body
             content += mail['body']
             # ファイル書き込み
             f = open(filepath, 'w')
             f.write(content)
             f.close()
             # タイムスタンプを変更
             os.utime(filepath, (timestamp, timestamp))
             # 新規メールのパスを格納
             newmails.append(filepath)
             senders[filepath] = mail['From'].replace('\r\n', '')
     Common.log('%d/%d mails retrieved using criterion "%s"' %
                (len(newmails), len(mails), criterion))
     return newmails, senders
Esempio n. 11
0
 def __request(self, url, data=None):
     try:
         if data:
             if isinstance(data, bytes):
                 pass
             elif isinstance(data, str):
                 data = data.encode(encoding='utf-8', errors='ignore')
             else:
                 raise TypeError
             response = urllib.request.urlopen(
                 urllib.request.Request(url, data))
         else:
             response = urllib.request.urlopen(url)
     except urllib.error.HTTPError as e:
         Common.log('HTTPError: %s' % str(e.code), error=True)
         Common.notify('Request failed')
         return
     except urllib.error.URLError as e:
         Common.log('URLError: %s' % str(e.reason), error=True)
         Common.notify('Request failed')
         return
     response_body = response.read()
     response.close()
     return response_body
 def onScreensaverDeactivated(self):
     Common.log('screensaver deactivated')
 def onScreensaverActivated(self):
     Common.log('screensaver activated')
 def onSettingsChanged(self):
     Common.log('settings changed')
 def onIncomingCall(self, param):
     # ログ
     Common.log('\n'.join([
         # https://www.pjsip.org/pjsip/docs/html/structpj_1_1OnIncomingCallParam.htm
         'callId: %s' % param.callId,
         # 'rdata: %s' % param.rdata,
         'rdata.info: %s' % param.rdata.info,
         'rdata.wholeMsg: \n-----\n%s\n-----' % '\n'.join(param.rdata.wholeMsg.strip().split('\r\n')),
         'rdata.srcAddress: %s' % param.rdata.srcAddress,
         'rdata.pjRxData: %s' % param.rdata.pjRxData,
     ]))
     call = pj.Call(self, param.callId)
     info = call.getInfo()
     Common.log('\n'.join([
         # https://www.pjsip.org/pjsip/docs/html/structpj_1_1CallInfo.htm
         'id: %s' % info.id,
         'role: %s' % info.role,
         'accId: %s' % info.accId,
         'localUri: %s' % info.localUri,
         'localContact: %s' % info.localContact,
         'remoteUri: %s' % info.remoteUri,
         'remoteContact: %s' % info.remoteContact,
         'callIdString: %s' % info.callIdString,
         # 'setting: %s' % info.setting,
         'setting.flag: %s' % info.setting.flag,
         'setting.reqKeyframeMethod: %s' % info.setting.reqKeyframeMethod,
         'setting.audioCount: %s' % info.setting.audioCount,
         'setting.videoCount: %s' % info.setting.videoCount,
         'state: %s' % info.state,
         'stateText: %s' % info.stateText,
         'lastStatusCode: %s' % info.lastStatusCode,
         'media: %s' % info.media,
         'provMedia: %s' % info.provMedia,
         'connectDuration: %d.%03d' % (info.connectDuration.sec, info.connectDuration.msec),
         'totalDuration: %d.%03d' % (info.totalDuration.sec, info.totalDuration.msec),
         'remOfferer: %s' % info.remOfferer,
         'remAudioCount: %s' % info.remAudioCount,
         'remVideoCount: %s' % info.remVideoCount,
     ]))
     # Kodiをアクティベート
     if Common.GET('cec') == 'true':
         xbmc.executebuiltin('CECActivateSource')
     # 発信者番号から番号検索
     name, key = lookup(info.remoteUri)
     local = parse(info.localUri)
     # 通知
     duration = Common.GET('duration')
     Common.notify(name, time=int(duration) * 1000)
     # メールによる通知
     if Common.GET('mailaddon') and Common.GET('mailnotify') == 'true':
         template = Common.GET('mailtemplate') or Common.STR(32913)
         address = Common.GET('mailaddress')
         message = template.format(name=name, key=key, local=local)
         xbmc.executebuiltin('RunPlugin("plugin://%s?%s")' % (Common.GET('mailaddon'), urlencode({
             'action': 'send',
             'subject': message,
             'message': message,
             'to': address
         })))
     # LINE notifyによる通知
     if Common.GET('lineaddon') and Common.GET('linenotify') == 'true':
         template = Common.GET('linetemplate') or Common.STR(32913)
         token = Common.GET('linetoken')
         message = template.format(name=name, key=key, local=local)
         xbmc.executebuiltin('RunPlugin("plugin://%s?%s")' % (Common.GET('lineaddon'), urlencode({
             'action': 'send',
             'name': token,
             'message': message
         })))
srcfile = Common.GET('pjsua2')
# 設定をチェック
if not os.path.isfile(srcfile) or os.path.basename(srcfile) != 'pjsua2.py':
    Common.ADDON.openSettings()
    sys.exit()
try:
    # pjsua2.pyをコピー
    shutil.copy(srcfile, Common.PY_FILE)
    # _pjsua2.soをコピー
    srcfile = glob.glob(os.path.join(os.path.dirname(srcfile), '_pjsua2*.so'))[0]
    shutil.copy(srcfile, Common.SO_FILE)
    # インポート実行
    from resources.pjsua2 import pjsua2 as pj
except Exception as e:
    Common.notify('Importing pjsua2 failed', time=3000, error=True)
    Common.log(e)
    sys.exit()

# 電子メールクライアントの有無を確認
try:
    mailaddon = 'script.handler.email'
    xbmcaddon.Addon(mailaddon)
    Common.SET('mailaddon', mailaddon)
except Exception:
    Common.SET('mailaddon', '')

# LINE Notifyハンドラの有無を確認
try:
    lineaddon = 'script.handler.line.notify'
    xbmcaddon.Addon(lineaddon)
    Common.SET('lineaddon', lineaddon)