<commit_before># coding=utf-8

import http.client
import re
import socket
from urllib.parse import quote

import module_states_file as xmlparser

class WFASearch:
  def __init__(self, terms):
    self.terms = terms
    self.curPod = 1
    self.curSubPod = 0
    (res, page) = getPage(terms)
    if res == http.client.OK:
      self.wfares = xmlparser.parse_string(page)
    else:
      self.wfares = None

  @property
  def success(self):
    try:
      return self.wfares["success"] == "true"
    except:
      return False

  @property
  def error(self):
    if self.wfares["error"] == "true":
      return "An error occurs during computation: " + self.wfares.getNode("error").getNode("msg").getContent()
    else:
      proposal = list()
      for dym in self.wfares.getNode("didyoumeans").getNodes("didyoumean"):
        proposal.append(dym.getContent())
      return "Did you mean: " + ', '.join(proposal) + "?"

  @property
  def nextRes(self):
    try:
      if len(self.wfares.getNodes("pod")) > self.curPod:
        txt = ""
        while txt == "" or subnode.getFirstNode("plaintext").getContent() == "":
          node = self.wfares.getNodes("pod")[self.curPod]
          subnode = node.getNodes("subpod")[self.curSubPod]

          self.curSubPod += 1
          if len(node.getNodes("subpod")) <= self.curSubPod:
            self.curPod += 1
            self.curSubPod = 0

          txt = node["title"] + " " + subnode["title"] + ": " + subnode.getFirstNode("plaintext").getContent()
        return txt
    except IndexError:
      pass
    self.curPod = 1
    return "No more results"


def getPage(terms):
  conn = http.client.HTTPConnection("api.wolframalpha.com")
  try:
    conn.request("GET", "/v2/query?input=%s&appid=%s" % (quote(terms), CONF.getNode("wfaapi")["key"]))
  except socket.gaierror:
    print ("impossible de récupérer la page Wolfram|Alpha.")<commit_msg>Improve stability of WFA module: catch more error types<commit_after># coding=utf-8

import http.client
import re
import socket
from urllib.parse import quote

import module_states_file as xmlparser

class WFASearch:
  def __init__(self, terms):
    self.terms = terms
    self.curPod = 1
    self.curSubPod = 0
    (res, page) = getPage(terms)
    if res == http.client.OK:
      self.wfares = xmlparser.parse_string(page)
    else:
      self.wfares = None

  @property
  def success(self):
    try:
      return self.wfares["success"] == "true"
    except:
      return False

  @property
  def error(self):
    if self.wfares["error"] == "true":
      return "An error occurs during computation: " + self.wfares.getNode("error").getNode("msg").getContent()
    elif self.wfares.hasNode("didyoumeans"):
      start = "Did you mean: "
      tag = "didyoumean"
      end = "?"
    elif self.wfares.hasNode("tips"):
      start = "Tips: "
      tag = "tip"
      end = ""
    elif self.wfares.hasNode("relatedexamples"):
      start = "Related examples: "
      tag = "relatedexample"
      end = ""
    elif self.wfares.hasNode("futuretopic"):
      return self.wfares.getNode("futuretopic")["msg"]
    else:
      return "An error occurs during computation"
    proposal = list()
    for dym in self.wfares.getNode(tag + "s").getNodes(tag):
      if tag == "tip":
        proposal.append(dym["text"])
      elif tag == "relatedexample":
        proposal.append(dym["desc"])
      else:
        proposal.append(dym.getContent())
    return start + ', '.join(proposal) + end

  @property
  def nextRes(self):
    try:
      if len(self.wfares.getNodes("pod")) > self.curPod:
        txt = ""
        while txt == "" or subnode.getFirstNode("plaintext").getContent() == "":
          node = self.wfares.getNodes("pod")[self.curPod]
          subnode = node.getNodes("subpod")[self.curSubPod]

          self.curSubPod += 1
          if len(node.getNodes("subpod")) <= self.curSubPod:
            self.curPod += 1
            self.curSubPod = 0

          txt = node["title"] + " " + subnode["title"] + ": " + subnode.getFirstNode("plaintext").getContent()
        return txt
    except IndexError:
      pass
    self.curPod = 1
    return "No more results"


def getPage(terms):
  conn = http.client.HTTPConnection("api.wolframalpha.com")
  try:
    conn.request("GET", "/v2/query?input=%s&appid=%s" % (quote(terms), CONF.getNode("wfaapi")["key"]))
  except socket.gaierror:
    print ("impossible de récupérer la page Wolfram|Alpha.")
{'fraction_non_alphanumeric': 0.14602169981916818, 'fraction_numerical': 0.003616636528028933, 'mean_word_length': 2.821243523316062, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '6578806', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1444, 'n_words': 314}