Exemplo n.º 1
0
    private bool pre = false; /* for PRE-Tag */

    /**
     * rename this method to test the convertor - should be able to be compiled stand-alone
     */

    public static void Main(string[] args)
    {
      Thread.CurrentThread.Name = "HtmlToText";
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
      try
      {
        // Use the current user in case an NTLM Proxy or similar is used.
        // request.Proxy = WebProxy.GetDefaultProxy();
        request.Proxy.Credentials = CredentialCache.DefaultCredentials;
      }
      catch (Exception) {}

      HttpWebResponse response = (HttpWebResponse)request.GetResponse();
      try
      {
        Stream stream = response.GetResponseStream();
        string data = "";
        using (StreamReader r = new StreamReader(stream))
        {
          data = r.ReadToEnd();
        }

        HtmlToText app = new HtmlToText(data);
        string text = app.ToString();
        Console.WriteLine(text);
      }
      finally
      {
        if (response != null)
        {
          response.Close();
        }
      }
    }
Exemplo n.º 2
0
    private string DownloadMainStory()
    {
      // Get the selected item
      GUIListItem item = GetSelectedItem();
      if (item == null)
      {
        return null;
      }

      feed_details feed = (feed_details)item.MusicTag;

      // Download the story
      string text = null;
      try
      {
        string data = string.Empty;
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(feed.m_link);
        try
        {
          // Use the current user in case an NTLM Proxy or similar is used.
          // request.Proxy = WebProxy.GetDefaultProxy();
          request.Proxy.Credentials = CredentialCache.DefaultCredentials;
        }
        catch (Exception) {}

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();
        try
        {
          using (Stream stream = response.GetResponseStream())
          {
            Encoding enc;
            try
            {
              enc = Encoding.GetEncoding(response.ContentEncoding);
            }
            catch
            {
              // Using Default Encoding
              enc = Encoding.GetEncoding(m_strSiteEncoding);
            }
            using (StreamReader r = new StreamReader(stream, enc))
            {
              data = r.ReadToEnd();
            }
          }
          // Convert html to text
          HtmlToText html = new HtmlToText(data);
          text = html.ToString().Trim();
        }
        finally
        {
          if (response != null)
          {
            response.Close();
          }
        }
      }
      catch (Exception ex)
      {
        GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_SHOW_WARNING, 0, 0, 0, 0, 0, 0);
        msg.Param1 = 9; //my news
        msg.Param2 = 912; //Unable to download latest news
        msg.Param3 = 0;

        string errtxt = ex.Message;
        int pos = errtxt.IndexOf(":");
        if (pos != -1)
        {
          errtxt = errtxt.Substring(pos + 1);
        }
        msg.Label3 = String.Format("{0}\n\n({1})", m_strSiteURL, errtxt);
        GUIWindowManager.SendMessage(msg);

        // Log exception
        Log.Info("ex:{0} {1} {2}", ex.Message, ex.Source, ex.StackTrace);
      }

      return text;
    }