기본 콘텐츠로 건너뛰기

NodeJs 를 이용한 EPG 정보가져오기


* 나의 환경 정보
장비 : 라즈베리파이 3 b
OS : 라즈비안 Jessie
사용 프로그램 : NodeJs v.8.11.3
방송사 : KT 올레TV

필요한 NODE 모듈은 아래와 같다.
var cheerio = require('cheerio'),
    request = require('request'),
    iconv = require('iconv-lite'),
    charset = require('charset'),
    fs = require('fs'),
    shell = require('shelljs');

* 순서

1. node로 웹크롤링을 해와서 방송 정보 xml파일을 만든다.

2. 만들어진 xml파일을 socat 을 이용하여 tvheadend xmltv.sock 로 전송해준다.

* 소스

1. request 를 이용한 스크래핑
request.post({
        uri: epg2xml[gISP],
        headers: {'content-type': 'application/x-www-form-urlencoded'},
        body: require('querystring').stringify(postData),
        encoding: null
        },function(err,res,body){
            if(err){
                throw err;
            }

            var enc = charset(res.headers, body);
            var rst = iconv.decode(body, enc);
            var $$ = cheerio.load(rst);

2. cheerio 를 이용한 html 파싱
$$('.schedule_info tbody > tr').each(function(num, elem) {

                var tHour = "";
                var tTime = [];
                var tPgm = [];
                var tCtg = [];
                var tRat = [];
                $$(this).find('td').each(function(idx, obj) {

3. xml 파일 생성
function fnWriteXml() {

    for(var k=0; k<arrSche.length; k=k+5){

        if((k+5) >= arrSche.length){
            gXml += '   arrSche[k]+arrSche[k+1]+'00 +0900" stop="'+fnGetFullDate(gDate)+'010000 +0900" channel="'+channelInfo[gCount].Id+'">\n';
        }else{
            gXml += '   arrSche[k]+arrSche[k+1]+'00 +0900" stop="'+arrSche[k+5]+arrSche[k+6]+'00 +0900" channel="'+channelInfo[gCount].Id+'">\n';
        }
        gXml += '       '</span>+<span style="color: #9cdcfe;">arrSche</span>[<span style="color: #9cdcfe;">k</span>+<span style="color: #b5cea8;">2</span>]+<span style="color: #ce9178;">'\n';

4. xml파일 socat 전송
function fnCallSocat(){
    if(shell.exec('cat xmlv.xml | sudo socat - UNIX-CONNECT:/home/hts/.hts/tvheadend/epggrab/xmltv.sock').code !== 0){
        shell.echo('failed');
        shell.exit(1);
    }
}


댓글

  1. 안녕하세요. 케이블tv epg를 찾다가 오게되었습니다. 좋은 정보감사합니다.

    답글삭제
    답글
    1. 네 안녕하세요
      만들고 혼자서 연습삼아서 대충올렸는데 도움이되었다니 다행이네요

      삭제

댓글 쓰기

이 블로그의 인기 게시물

라즈베리파이 Tvheadend EPG 자동 스케줄등록

1. shell 파일을 생성한다. sudo nano /home/pi/shell/ epg.sh 2. 아래 내용을 입력한다. #!/bin/sh sudo /usr/bin/python /home/pi/epg2xml-1.2.4/ epg2xml.py -i KT -s /home/hts/.hts/tvheadend/epggrab/xmltv.sock 3. 실행권한을 추가한다. sudo chmod +x epg.sh 4. 자동스케줄등록을 위하여 crontab을 연다. crontab -e 5. 스케줄을 등록한다. 4 0 * * * /home/pi/shell/ epg.sh 6. crontab을 재시작한다. sudo /etc/init.d/cron restart

라즈베리파이 TVheadend EPG 설치로그

pi@raspberrypi:~ $ cd /home/pi/epg2xml-1.2.4 pi@raspberrypi:~/epg2xml-1.2.4 $ sudo chmod -x epg2xml.py pi@raspberrypi:~/epg2xml-1.2.4 $ sudo chmod 755 Channel.json pi@raspberrypi:~/epg2xml-1.2.4 $ sudo easy_install beautifulsoup sudo: easy_install: command not found pi@raspberrypi:~/epg2xml-1.2.4 $ python --version Python 2.7.9 pi@raspberrypi:~/epg2xml-1.2.4 $ sudo apt-get install python-setuptools Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed:   python-pkg-resources Suggested packages:   python-distribute python-distribute-doc The following NEW packages will be installed:   python-pkg-resources python-setuptools 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 306 kB of archives. After this operation, 957 kB of additional disk space will be used.Do you want to continue? [Y/n] y Get:1 http://mirrordirector.raspbian.org/raspbian / jessie/...