<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# horepgd.py
#
# This script provides a daemon which runs daily

import os
import sys
import pwd
import grp
import logging
import logging.handlers
import argparse

from horepg import *
from tvheadend import *

def switch_user(uid = None, gid = None):
  # set gid first
  if gid is not None:
    os.setgid(gid)
  if uid is not None:
    os.setuid(uid)

def daemonize():
  def fork_exit_parent():
    try:
      pid = os.fork()
      if pid > 0:
        # parent, so exit
        sys.exit(0)
    except OSError as exc:
      sys.stderr.write('failed to fork parent process {:0}\n'.format(exc))
      sys.exit(1)
  def redirect_stream(source, target = None):
    if target is None:
      target_fd = os.open(os.devnull, os.O_RDWR)
    else:
      target_fd = target.fileno()
    os.dup2(target_fd, source.fileno())

  os.umask(0)
  os.chdir('/')

  fork_exit_parent()
  os.setsid()
  fork_exit_parent()

  # redirect streams
  sys.stdout.flush()
  sys.stderr.flush()
  redirect_stream(sys.stdin)
  redirect_stream(sys.stdout)
  redirect_stream(sys.stderr)

def run_import(wanted_channels, tvhsocket):
  with TVHXMLTVSocket(tvhsocket) as tvh_client:
    chmap = ChannelMap()
    listings = Listings()
    # add listings for each of the channels
    for channel_id, channel in chmap.channel_map.items():
      if channel['title'] in wanted_channels:
        now = datetime.date.today().timetuple()
        nr = 0
        xmltv = XMLTVDocument()
        xmltv.addChannel(channel_id, channel['title'], channel['images'])
        for i in range(0, 5):
          start = int((calendar.timegm(now) + 86400 * i) * 1000) # milis
          end = start + (86400 * 1000)
          nr = nr + listings.obtain(xmltv, channel_id, start, end)
        debug('Adding {:d} programmes for channel {:s}'.format(nr, channel['title']))
        # send this channel to tvh for processing
        tvh_client.send(xmltv.document.toprettyxml(encoding='UTF-8'))

if __name__ == '__main__':
  parser = argparse.ArgumentParser(description='Fetches EPG data from the Horizon service and passes it to TVHeadend.')
  parser.add_argument('-s', nargs='?', metavar='PATH', dest='tvhsocket', default='/home/hts/.hts/tvheadend/epggrab/xmltv.sock', type=str, help='path to TVHeadend XMLTV socket')
  parser.add_argument('-p', nargs='?', metavar='PATH', dest='pid_filename', default='/var/run/horepgd.pid', type=str, help='path to PID file')
  parser.add_argument('-u', nargs='?', metavar='USER', dest='as_user', default='hts', type=str, help='run as USER')
  parser.add_argument('-g', nargs='?', metavar='GROUP', dest='as_group', default='video', type=str, help='run as GROUP')
  parser.add_argument('-d', dest='daemonize', action='store_const', const=True, default=False, help='daemonize')
  parser.add_argument('-tvh', dest='tvh_host', metavar = 'HOST', default = 'localhost', help='the hostname of TVHeadend to fetch channels from')<commit_msg>make channel matching case insensitive<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# horepgd.py
#
# This script provides a daemon which runs daily

import os
import sys
import pwd
import grp
import logging
import logging.handlers
import argparse

from horepg import *
from tvheadend import *

def switch_user(uid = None, gid = None):
  # set gid first
  if gid is not None:
    os.setgid(gid)
  if uid is not None:
    os.setuid(uid)

def daemonize():
  def fork_exit_parent():
    try:
      pid = os.fork()
      if pid > 0:
        # parent, so exit
        sys.exit(0)
    except OSError as exc:
      sys.stderr.write('failed to fork parent process {:0}\n'.format(exc))
      sys.exit(1)
  def redirect_stream(source, target = None):
    if target is None:
      target_fd = os.open(os.devnull, os.O_RDWR)
    else:
      target_fd = target.fileno()
    os.dup2(target_fd, source.fileno())

  os.umask(0)
  os.chdir('/')

  fork_exit_parent()
  os.setsid()
  fork_exit_parent()

  # redirect streams
  sys.stdout.flush()
  sys.stderr.flush()
  redirect_stream(sys.stdin)
  redirect_stream(sys.stdout)
  redirect_stream(sys.stderr)

def run_import(wanted_channels, tvhsocket):
  with TVHXMLTVSocket(tvhsocket) as tvh_client:
    chmap = ChannelMap()
    listings = Listings()
    # add listings for each of the channels
    for channel_id, channel in chmap.channel_map.items():
      if channel['title'].lower() in (channel.lower() for channel in wanted_channels):
        now = datetime.date.today().timetuple()
        nr = 0
        xmltv = XMLTVDocument()
        xmltv.addChannel(channel_id, channel['title'], channel['images'])
        for i in range(0, 5):
          start = int((calendar.timegm(now) + 86400 * i) * 1000) # milis
          end = start + (86400 * 1000)
          nr = nr + listings.obtain(xmltv, channel_id, start, end)
        debug('Adding {:d} programmes for channel {:s}'.format(nr, channel['title']))
        # send this channel to tvh for processing
        tvh_client.send(xmltv.document.toprettyxml(encoding='UTF-8'))

if __name__ == '__main__':
  parser = argparse.ArgumentParser(description='Fetches EPG data from the Horizon service and passes it to TVHeadend.')
  parser.add_argument('-s', nargs='?', metavar='PATH', dest='tvhsocket', default='/home/hts/.hts/tvheadend/epggrab/xmltv.sock', type=str, help='path to TVHeadend XMLTV socket')
  parser.add_argument('-p', nargs='?', metavar='PATH', dest='pid_filename', default='/var/run/horepgd.pid', type=str, help='path to PID file')
  parser.add_argument('-u', nargs='?', metavar='USER', dest='as_user', default='hts', type=str, help='run as USER')
  parser.add_argument('-g', nargs='?', metavar='GROUP', dest='as_group', default='video', type=str, help='run as GROUP')
  parser.add_argument('-d', dest='daemonize', action='store_const', const=True, default=False, help='daemonize')
  parser.add_argument('-tvh', dest='tvh_host', metavar = 'HOST', default = 'localhost', help='the hostname of TVHeadend to fetch channels from')
{'fraction_non_alphanumeric': 0.1437625083388926, 'fraction_numerical': 0.009673115410273516, 'mean_word_length': 3.7937649880095923, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '440955', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2022, 'n_words': 513}