Adblock breaks this site

[C# Source] GELookup Class

Discussion in 'Programming General' started by crackmyknuckles, Jun 7, 2009.

  1. crackmyknuckles

    crackmyknuckles Newcomer

    Joined:
    Jan 9, 2009
    Posts:
    15
    Referrals:
    0
    Sythe Gold:
    0
    [C# Source] GELookup Class

    I made this as part of a set of tools for Runescape Developers, although I have now rewirtten this class completely with new features and made it more stable/upgradable. This version features some nice things like the GE Image and Graph downloading and Cacheing features it is however rather sloppy as I was never expecting to release it.

    I'll give help on how it works if you PM me but please try to figure it out yourself first.. I have alot to do..

    Anyway heres the code:

    Code:
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Windows.Forms;
    using System.Text.RegularExpressions;
    
    namespace GELookup
    {
        public class GELookup
        {
            private ArrayList id;
            private ArrayList name;
            public List<string> Result;
    
            public GELookup(string Item, bool MultiPage, int Page)
            {
                try
                {
                    Result = new List<string>();
                    id = new ArrayList();
                    name = new ArrayList();
    
                    //Get GE HTML.
                    string strPage = GetFile("http://itemdb-rs.runescape.com/results.ws?query=" + Item + "&sup=All&cat=All&sub=All&page=" + Page + "&vis=1&order=1&sortby=name&price=all&members=");
                    string[] splitter = new string[] { "./viewitem.ws?obj=" };
                    string[] array;
    
                    //Split the HTML at the Items.
                    array = strPage.Split(splitter, StringSplitOptions.None);
                    splitter = new string[] { "\"> " };
    
                    //Add the Items while cleaning up the names/ID's
                    for (int i = 1; i < array.Length; i++)
                    {
                        id.Add(array[i].Split(splitter, StringSplitOptions.None)[0]);
                        name.Add(array[i].Split(splitter, StringSplitOptions.None)[1].Split(new string[] { "</a>" }, StringSplitOptions.None)[0]);
                    }
    
                    //Add the Items to the Result List
                    for (int i = 0; i < id.Count; i++)
                    {
                        Result.Add(name[i].ToString()); ;
                    }
    
                    //If MultiPage is true find the last page number and repeat
                    //the search with ParsePage() method.
                    if (MultiPage)
                    {
                        int Start = strPage.IndexOf("<td class=\"navmid\">\n1");
                        int Finish = strPage.IndexOf("</td>\n<td class=\"navright\">");
                        string Data = "blank";
                        if (Start > 0 && Finish > 0)
                        {
                            Data = strPage.Substring(Start);
                            Data = Data.Substring(0, Data.IndexOf("</td>\n"));
                            Data = Data.Substring(Data.LastIndexOf("bers=\">") + 7, 1);
                            string[] Data2 = Data.Split(new string[] { "\n<a href=\"./results.ws?query=" + Item + "&amp;sup=All&amp;cat=All&amp;sub=All&amp;page=2&amp;vis=1&amp;order=1&amp;sortby=name&amp;price=all&amp;members=\">", "</a>", "<td>" }, StringSplitOptions.None);
    
                            List<object> _Results = new List<object>();
    
                            char[] number = Data.ToCharArray();
                            if (Char.IsNumber(number[0]))
                            {
                                int Count = Convert.ToInt32(Data);
                                for (int X = 2; X <= Count; X++)
                                {
                                    _Results.Add(ParsePage(Item, X));
                                }
    
                                //All of the pages have been parsed so add it all to the Result List.
                                for (int i = 0; i < _Results.Count; i++)
                                {
                                    Result.AddRange((List<string>)_Results[i]);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Error Code: 010.\r\nPlease post this Error Code in the Bug's\r\nSection of the forum.\r\nThank you.");
                }
            }
    
            public GELookup()
            { }
    
            //Self Explanitory.
            private string GetFile(string strURL)
            {
                try
                {
                    WebRequest myWebRequest = WebRequest.Create(strURL);
    
                    WebResponse myWebResponse = myWebRequest.GetResponse();
    
                    Stream ReceiveStream = myWebResponse.GetResponseStream();
    
                    Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
    
                    StreamReader readStream = new StreamReader(ReceiveStream, encode);
    
                    string strResponse = readStream.ReadToEnd();
    
                    readStream.Close();
    
                    myWebResponse.Close();
                    return strResponse;
                }
               catch(Execption e)
                {
                    MessageBox.Show(e.ToString());
                    return null;
                }
            }
            
            //Basicly a copy of the Constructor only this only parses one page...
            private List<string> ParsePage(string Item, int Page)
            {
                try
                {
                    ArrayList id1 = new ArrayList();
                    ArrayList name1 = new ArrayList();
                    List<string> Result1 = new List<string>();
    
                    string strPage = GetFile("http://itemdb-rs.runescape.com/results.ws?query=" + Item + "&sup=All&cat=All&sub=All&page=" + Page + "&vis=1&order=1&sortby=name&price=all&members=");
                    string[] splitter = new string[] { "./viewitem.ws?obj=" };
                    string[] array;
                    array = strPage.Split(splitter, StringSplitOptions.None);
                    splitter = new string[] { "\"> " };
                    for (int i = 1; i < array.Length; i++)
                    {
                        id.Add(array[i].Split(splitter, StringSplitOptions.None)[0]);
                        name1.Add(array[i].Split(splitter, StringSplitOptions.None)[1].Split(new string[] { "</a>" }, StringSplitOptions.None)[0]);
                    }
    
                    for (int i = 0; i < name1.Count; i++)
                    {
                        Result1.Add(name1[i].ToString()); ;
                    }
                    return Result1;
                }
                catch(Execption e)
                {
                    MessageBox.Show(e.ToString());
                    return null;
                }       
            }
            
            //Takes the Index of the Item in the ListBox and returns
            //A string array of prices and the picture.
            public string[] GetPrice(int ItemIndex)
            {
                try
                {
                    string[] ReturnPrices = new string[6];
                    int index = ItemIndex;
                    string[] price = new string[3];
                    string[] array;
                    string[] splitter = new string[] { "price:</b> " };
                    if (index != -1)
                    {
                        string page = GetFile("http://itemdb-rs.runescape.com/viewitem.ws?obj=" + id[index]);
                        array = page.Split(splitter, StringSplitOptions.None);
                        splitter = new string[] { "</span>" };
                        for (int i = 0; i < 3; i++)
                        {
                            price[i] = array[i + 1].Split(splitter, StringSplitOptions.None)[0];
                        }
    
                        Regex Regex = new Regex("\\d{4}_obj_big.gif?");
                        Match match = Regex.Match(page);
                        string matchResult = match.Value;
                        string DynamicId = matchResult.Remove(4);
    
                        ReturnPrices[0] = "Name: " + Result[index];
                        ReturnPrices[1] = "ID: " + id[index];
                        ReturnPrices[2] = "Minimum Price: " + price[0];
                        ReturnPrices[3] = "Market Price: " + price[1];
                        ReturnPrices[4] = "Maximum Price: " + price[2];
                        ReturnPrices[5] = GetImage(Convert.ToString(id[index]), DynamicId);
                    }
    
                    return ReturnPrices;
                }
                catch(Execption e)
                {
                    MessageBox.Show(e.ToString());
                    return null;
                }
            }
            
            //Gets the Image of Item with the ItemID and returns the path to the saved image.
            private string GetImage(string ItemID,string imageDynId)
            {
                try
                {
                    DirectoryInfo Folder = new DirectoryInfo(Directory.GetCurrentDirectory() + @"\GeImages\");
    
                    //Check to see if that folder is there if not
                    //create it.
                    if (!Folder.Exists)
                    {
                        Folder.Create();
                    }
    
                    //Check to see if the file has been downloaded before
                    //if not download it, if so return the path.
                    if (!File.Exists(Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif"))
                    {
                        WebClient objClient = new WebClient();
                        objClient.DownloadFile(@"http://itemdb-rs.runescape.com/" + imageDynId + "_obj_big.gif?id=" + ItemID, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif");
                    }
    
    
                    return Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + ".gif";
                }
                catch(Execption e)
                {
                    MessageBox.Show(e.ToString());
                    return null;
                }
            }
    
            public string[] GetGraph(string ItemID)
            {
                try
                {
                    DirectoryInfo Folder = new DirectoryInfo(Directory.GetCurrentDirectory() + @"\GeImages\");
    
                    //Check to see if that folder is there if not
                    //create it.
                    if (!Folder.Exists)
                    {
                        Folder.Create();
                    }
    
                    string page = GetFile("http://itemdb-rs.runescape.com/viewitem.ws?obj=" + ItemID);
                    Regex Regex = new Regex("\\d{4}_graphimg.gif?");
                    Match match = Regex.Match(page);
                    string matchResult = match.Value;
                    string graphDynId = matchResult.Remove(4);
    
                    Uri File1 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_scaleimg.gif?id=" + ItemID + "&scale=0&axis=0");
                    Uri File2 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_graphimg.gif?id=" + ItemID + "&scale=0");
                    Uri File3 = new Uri("http://itemdb-rs.runescape.com/" + graphDynId + "_scaleimg.gif?id=" + ItemID + "&scale=0&axis=2");
    
                    WebClient objClient = new WebClient();
                    objClient.DownloadFile(File1, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale1.gif");
                    objClient.DownloadFile(File2, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Graph.gif");
                    objClient.DownloadFile(File3, Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale2.gif");
    
                    return new string[3] {Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale1.gif", Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Graph.gif", Directory.GetCurrentDirectory() + @"\GeImages\" + ItemID + "Graph_Scale2.gif"};
                }
                catch(Exception e)
                {
                    MessageBox.Show(e.ToString());
                    return null;
                }
            }        
        }
    }
    
    This orignal version was inspired by and parts of it are from Weeb's GElookup and Flaming Idiots VB.Net GELookup Applications.

    I take no credit for those parts(if you can find any intact code from them) however the rest is of my own creation..

    Enjoy...
     
< Writing to Webserver[help] | [Tutorial Series] Learn C# from Scratch >


 
 
Adblock breaks this site