NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 9 10 11 12 13 … 93 Next »
C# Tricks for NPVR

 
  • 0 Vote(s) - 0 Average
C# Tricks for NPVR
imilne
Offline

Posting Freak

Posts: 2,423
Threads: 135
Joined: Feb 2008
#1
2011-08-16, 07:01 PM
I know that at least a few of us don't have much experience with C#, so I thought it might be nice to have a thread with short tips and tricks, but obviously focused around NPVR and its API.

Here's one to kick things off...

This demonstrates two things: how to let a plugin continue to work if the user's skin doesn't support it, and how to merge the contents of a "global.xml" skin file with another file. I needed the latter to define some extra fonts that aren't in the main set without having to redefine them in every skin file that my plugin uses (popups, different views, and so on).

Code:
internal SkinHelper GetSkinHelper(string name)
{
    SkinHelper shMain = null;
    SkinHelper shGlbl = null;

    // Load the required files, either from the current skin's folder...
    if (Directory.Exists(SettingsHelper.GetInstance().GetSkinDirectory() + "VideosPlus"))
    {
        shMain = new SkinHelper("VideosPlus\\" + name);
        shGlbl = new SkinHelper("VideosPlus\\global.xml");
    }
    // ...or from the Default skin if they couldn't be found
    else
    {
        shMain = new SkinHelper("..\\Default\\VideosPlus\\" + name);
        shGlbl = new SkinHelper("..\\Default\\VideosPlus\\global.xml");
    }

    // Now merge in everything found in global.xml
    DataSet dsMain = new DataSet();
    DataSet dsGlbl = new DataSet();
    dsMain.ReadXml(new XmlNodeReader(shMain.GetBackingDocument()));            
    dsGlbl.ReadXml(new XmlNodeReader(shGlbl.GetBackingDocument()));
    dsMain.Merge(dsGlbl);

    shMain.GetBackingDocument().LoadXml(dsMain.GetXml());

    return shMain;
}

Feel free to use, abuse and critique, especially if it turns out that there's a better way of doing it Big Grin

Iain
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#2
2012-02-06, 04:17 PM
Don't use WebClient.DownloadString or WebClient.DownloadData to download the xml before deserializing.

WebClient.DownloadString will apply UTF-16 encoding and non-English characters will not be decoded properly. WebClient.DownloadData will allow some non-English characters to be decoded properly but not others; Cyrillic characters are not decoded properly.

Instead, use System.Xml.XmlTextReader which can take a url as the source parameter and only requires two lines of code:

Code:
System.Xml.XmlReader reader = new System.Xml.XmlTextReader(someUrl);
MyXMLObject myXmlObj = (MyXMLObject)serializer.Deserialize(reader);
mvallevand
Offline

Posting Freak

Ontario Canada
Posts: 52,778
Threads: 954
Joined: May 2006
#3
2012-02-07, 12:41 AM
Since this is tips when reading strings that have utf-8 (which most files I read online do) I use

StreamReader sr = new StreamReader(filename, System.Text.Encoding.UTF8);

This get rid of many problems with 8859-1 conversions, and odd gibberish on the screen.

Martin
whurlston
Offline

Posting Freak

Posts: 7,885
Threads: 102
Joined: Nov 2006
#4
2012-02-07, 12:48 AM
StreamReader didn't work for me with the YouTube feeds, as the video you posted will attest. I tried it with both DownloadString and DownloadData.
mvallevand
Offline

Posting Freak

Ontario Canada
Posts: 52,778
Threads: 954
Joined: May 2006
#5
2012-02-07, 05:09 AM
For fetching "text" from utf-8 sites with WebRadio I use

Code:
WebRequest objRequest = WebRequest.Create(streamURL);
                WebResponse objResponse = objRequest.GetResponse();
                HttpWebResponse resp = (HttpWebResponse)objResponse;
                try
                {
                    Encoding enc = Encoding.GetEncoding(resp.CharacterSet.ToString());
                    Logger.Debug("Encoding " + enc.ToString());
                }
                catch { }
                StreamReader sr = new StreamReader(objResponse.GetResponseStream(), System.Text.Encoding.UTF8);
                string strContent = sr.ReadToEnd();

I log it in case I ever encounter something other than utf-8 and so far so good.

Martin
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#6
2012-02-07, 05:38 AM (This post was last modified: 2012-02-07, 05:42 AM by bgowland.)
mvallevand Wrote:For fetching "text" from utf-8 sites with WebRadio I use
I hope you actually do something in that 'catch' block. Wink

Also, rather than declare 'enc' within your 'try' block, declare it prior and pass it to the constructor of StreamReader and process the text based on the encoding.

Admittedly, UTF-8 is probably the most common but always worth making provision for other encoding.
mvallevand
Offline

Posting Freak

Ontario Canada
Posts: 52,778
Threads: 954
Joined: May 2006
#7
2012-02-07, 06:06 AM
I don't catch that one, I have another try/catch around the whole download, I don't need to know that the encoding wasn't in the header. I can't use other encodings because these populate into NextPVR list boxes and I wasn't sure that they would work if I did.

Martin
bgowland
Offline

Posting Freak

West Yorkshire, UK
Posts: 4,583
Threads: 384
Joined: Dec 2004
#8
2012-02-07, 08:14 AM
mvallevand Wrote:I don't catch that one, I have another try/catch around the whole download,
Hmm, it's time I retired so I won't go into details about why having empty catch blocks or a 'catch all' block are really bad ideas. Not good practices to teach people based on Iain's opening statement and the spirit of this thread.

Cheers,
Brian
mvallevand
Offline

Posting Freak

Ontario Canada
Posts: 52,778
Threads: 954
Joined: May 2006
#9
2012-10-08, 03:07 AM
As some of you know some of the fields in the NPVR database are serialized XML and they are a challenge to parse with normal sql. I finally had a reason to use a snippet of code that I found (I added multiline and ignore case)

Code:
// taken from http://sqlite.phxsoftware.com/forums/p/348/1457.aspx#1457
    [SQLiteFunction(Name = "REGEXP", Arguments = 2, FuncType = FunctionType.Scalar)]
    class MyRegEx : SQLiteFunction
    {
        public override object Invoke(object[] args)
        {
            return System.Text.RegularExpressions.Regex.IsMatch(Convert.ToString(args[1]), Convert.ToString(args[0]),RegexOptions.IgnoreCase|RegexOptions.Multiline);
        }
    }

using this you do things like

Code:
select * from scheduled_recording
where event_details REGEXP ('('Title>*.+vampire*.+</Title>')";

Martin
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)



Possibly Related Threads…
Thread Author Replies Views Last Post
  Way to tell programmatically if channel load in from NPVR has finished... gdogg371 3 2,325 2021-03-11, 03:59 PM
Last Post: mvallevand
  Plugins and NPVR. Where do we start? sub 80 68,064 2020-11-26, 10:02 PM
Last Post: mandai
  Test/Development environment for npvr.db3 scJohn 10 4,179 2020-09-04, 09:14 PM
Last Post: scJohn
  How to extract M3U8 and get matching XMLTV guide data from NPVR almightyj 0 3,357 2018-10-23, 07:24 AM
Last Post: almightyj
  ios app to control npvr ui idea jnbooker15 4 3,555 2015-09-21, 10:19 PM
Last Post: sub
  ios app to control npvr ui idea jnbooker15 0 2,434 2015-09-21, 06:39 PM
Last Post: jnbooker15
  Couple of questions about hacking npvr.db3 drmargarit 6 4,209 2014-09-08, 02:22 AM
Last Post: sub
  high res (256x256+) npvr icon? reven 15 5,772 2013-09-01, 05:13 AM
Last Post: psycik
  trying to fake npvr database for unit tests reven 3 2,237 2013-05-20, 08:53 AM
Last Post: reven
  Multiple genres in npvr.db3 bgowland 5 2,813 2013-04-16, 09:53 PM
Last Post: ACTCMS

  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode