<commit_before>from flask import Flask, jsonify, render_template, request, send_from_directory
import ConfigParser, MythTV, threading, jsonpickle, os, os.path

config = ConfigParser.RawConfigParser()
config.read('mythtv.cfg')

app = Flask(__name__)

mythtv_db = MythTV.MythDB(args=(('DBHostName', config.get('Database', 'hostname')),
                             ('DBName', config.get('Database', 'name')),
                             ('DBUserName', config.get('Database', 'username')),
                             ('DBPassword', config.get('Database', 'password'))))

mythtv_backend = MythTV.MythBE(db=mythtv_db)
mythtv_xml = MythTV.MythXML(db=mythtv_db)
mythtv_frontends = []

cache_dir = './cache'

def search_for_frontends():
  global mythtv_frontends
  # TODO: May need to wrap this in mutex lock or something, in case a web query comes through as the array is updated...
  mythtv_frontends = []
  for fe in MythTV.Frontend.fromUPNP():
    mythtv_frontends.append(fe)
  #threading.Timer(60, search_for_frontends).start()

search_for_frontends()

# Recordings
@app.route('/recordings.json')
def recordings():
  return jsonpickle.encode(filter(lambda p: p.recgroup != "LiveTV", mythtv_backend.getRecordings()), unpicklable=False)

# Channels
@app.route('/channel/<chanid>/image.jpg')
def channel_image(chanid):
  height = request.args['height']
  width = request.args['width']
  
  base = chanid
  dest = cache_dir
  dest_file = base + '.png'
  dest_file_resized = base + '-' + width + 'x' + height + '.png'
<commit_msg>Catches exception when trying to connect to a frontend that is refusing connections<commit_after>from flask import Flask, jsonify, render_template, request, send_from_directory
import ConfigParser, MythTV, threading, jsonpickle, os, os.path

config = ConfigParser.RawConfigParser()
config.read('mythtv.cfg')

app = Flask(__name__)

mythtv_db = MythTV.MythDB(args=(('DBHostName', config.get('Database', 'hostname')),
                             ('DBName', config.get('Database', 'name')),
                             ('DBUserName', config.get('Database', 'username')),
                             ('DBPassword', config.get('Database', 'password'))))

mythtv_backend = MythTV.MythBE(db=mythtv_db)
mythtv_xml = MythTV.MythXML(db=mythtv_db)
mythtv_frontends = []

cache_dir = './cache'

def search_for_frontends():
  global mythtv_frontends
  # TODO: May need to wrap this in mutex lock or something, in case a web query comes through as the array is updated...
  mythtv_frontends = []
 
  fes = MythTV.Frontend.fromUPNP()
  while(True):
    try:
      mythtv_frontends.append(fes.next())
    except MythTV.exceptions.MythFEError:
      # If we can't connect to the frontend, just ignore - it's probably not set up for remote access
      pass
    except StopIteration:
      break
  
  #threading.Timer(60, search_for_frontends).start()

search_for_frontends()

# Recordings
@app.route('/recordings.json')
def recordings():
  return jsonpickle.encode(filter(lambda p: p.recgroup != "LiveTV", mythtv_backend.getRecordings()), unpicklable=False)

# Channels
@app.route('/channel/<chanid>/image.jpg')
def channel_image(chanid):
  height = request.args['height']
  width = request.args['width']
  
  base = chanid
  dest = cache_dir
  dest_file = base + '.png'
  dest_file_resized = base + '-' + width + 'x' + height + '.png'

{'fraction_non_alphanumeric': 0.13265610994920823, 'fraction_numerical': 0.0011951000896325067, 'mean_word_length': 3.9235294117647057, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '4946944', 'n_tokens_mistral': 1104, 'n_tokens_neox': 1115, 'n_words': 251}