by nolovelust
30. March 2010 21:25
For part 2 click Building Simple Shoutcast Web Interface for Mobile Part 2
App_Code/ScEngine.cs
using System;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Data.Common;
///
/// Summary description for ScEngine
///
public class ScEngine
{
public DataSet GetStationList(string q, string job)
{
q = ScEngine.ReturnAlphaNumericForSearch(q);
q = ScEngine.LimitString(q, 30);
DataSet ds = new DataSet();
string genreXmlFile = ScSettings.DownloadFolder + q + ".xml";
if (File.Exists(genreXmlFile))
{
FileInfo fi = new FileInfo(genreXmlFile);
if (DateTime.Now.Subtract(fi.LastWriteTime).TotalDays > 1)
{
File.WriteAllText(genreXmlFile, DownloadXML(q, job), Encoding.UTF8);
}
// read from local file
ds.ReadXml(genreXmlFile);
}
else
{
string newDownload = DownloadXML(q, job);
MemoryStream mStream = new MemoryStream(ASCIIEncoding.Default.GetBytes(newDownload));
File.WriteAllText(genreXmlFile, newDownload, Encoding.UTF8);
ds.ReadXml(mStream);
}
return ds;
}
public string DownloadXML(string q, string job)
{
string url = (job == "search") ? ScSettings.strURLsearch + q : ScSettings.strURLbrowse + q;
StringBuilder sb = new StringBuilder();
string tempString = null;
int count = 0;
byte[] buf = new byte[8192];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = 4000; request.Accept = "*/*";
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0; .NET CLR 3.5.30729; .NET CLR";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
do
{
count = resStream.Read(buf, 0, buf.Length);
if (count != 0)
{
tempString = Encoding.UTF8.GetString(buf, 0, count);
sb.Append(tempString);
}
}
while (count > 0);
return sb.ToString();
}
public static int ReturnNumeric(string input)
{
if (String.IsNullOrEmpty(input) == true)
{
return 0;
}
else
{
try
{
return Convert.ToInt32(input);
}
catch
{
return 0;
}
}
}
public static string ReturnAlphaNumeric(string input)
{
if (String.IsNullOrEmpty(input) == true)
{
return string.Empty;
}
else
{
try
{
return Regex.Replace(input, @"[^a-zA-Z0-9\.\-_]", "_").ToString();
}
catch
{
return string.Empty;
}
}
}
public static string ReturnAlphaNumericForSearch(string input)
{
if (String.IsNullOrEmpty(input) == true)
{
return string.Empty;
}
else
{
try
{
return Regex.Replace(input, @"[^a-zA-Z0-9\.\-_]", " ").ToString();
}
catch
{
return string.Empty;
}
}
}
public static string LimitString(string strToLimit, int limitTo)
{
if (strToLimit != null && strToLimit.Length > 0)
{
strToLimit = strToLimit.Trim();
if (strToLimit.Length > limitTo)
{
return strToLimit.Substring(0, limitTo);
}
else
{
return strToLimit;
}
}
return strToLimit;
}
public static void DeleteOldXML(int days)
{
try
{
// used in global.asax app start
DateTime KeepDate = DateTime.Now.AddDays(days);
DirectoryInfo fileListing = new DirectoryInfo(ScSettings.DownloadFolder);
foreach (FileInfo f in new DirectoryInfo(ScSettings.DownloadFolder).GetFiles())
{
if (f.LastWriteTime <= KeepDate && f.Extension.ToLower() == ".xml")
f.Delete();
}
}
catch
{
}
}
}
App_Code/ScGenres.cs
using System;
using System.Data;
using System.IO;
using System.Web;
///
/// Summary description for ShoutCastCategoreies
///
public class ScGenres
{
public DataSet GetGenres()
{
string myXMLfile = HttpContext.Current.Server.MapPath("~/App_Data/Genres.xml");
DataSet ds = new DataSet();
if (HttpContext.Current.Cache["SCGenres"] == null)
{
FileStream fsReadXml = new FileStream
(myXMLfile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
try
{
ds.ReadXml(fsReadXml);
//HttpContext.Current.Cache["Genres"] = ds;
HttpContext.Current.Cache.Insert("SCGenres", ds, null,
System.Web.Caching.Cache.NoAbsoluteExpiration,
TimeSpan.FromMinutes(60));
return ds;
}
catch (Exception ex)
{
return null;
}
finally
{
fsReadXml.Close();
}
}
else
{
ds = (DataSet)HttpContext.Current.Cache["SCGenres"]; ;
return ds;
}
}
}
App_Code/ScSettings.cs
///
/// Summary description for Settings
///
using System.Web;
public class ScSettings
{
public ScSettings()
{
}
public static string strURLsearch = "http://www.shoutcast.com/sbin/newxml.phtml?search=";
public static string strURLbrowse = "http://www.shoutcast.com/sbin/newxml.phtml?genre=";
public static string DownloadFolder = HttpContext.Current.Server.MapPath("~/App_Data/Downloads/");
}