Skip to content
WJunction - Webmaster Forum

[c#] IMDB Info Grabber (Full program)

Status
Not open for further replies.
Ok.

Here is a handy little application - fully working - that obtains information from IMDB.com.

Fundamentally you can go much further, but im releasing the code thus far so users can learn from the code and how its used.

useage: enter a movie URL into the textbox and click search.
Example: http://www.imdb.com/title/tt0892318/

Download Link (exe):
http://www.coding.extremecoderz.com/IMDBGrabber.rar
Download Link (solution): http://www.coding.extremecoderz.com/IMDBsol.rar

Anyway. without further ado:

Form1.cs Source:
PHP:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;
using IMDBGrabber;

namespace IMDBGrabber
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private delegate void DisplayStatus(string info, int progress, int maxProgress);
        private void Status(string info, int progress, int maxProgress)
        {
            toolStripProgressBar1.Maximum = maxProgress;
            toolStripProgressBar1.Value = progress;
            toolStripStatusLabel1.Text = info;
        }

        private delegate void DisplayOutput(
            string title,
            string desc,
            string rating,
            string votes,
            string releaseDate,
            string tagLine
            );
        
        private void Output(
            string title,
            string desc,
            string rating,
            string votes,
            string releaseDate,
            string tagLine
            )

        {
            title_tb.Text = title;
            desc_tb.Text = desc;
            rating_tb.Text = rating + " (" + votes + ")";
            releaseDate_tb.Text = releaseDate;
            tagLine_tb.Text = tagLine;
            
        }

        private delegate void displayCoverDelegate(string imageURL);
        private void DisplayCoverImage(string imageURL)
        {
            pictureBox1.Image = IMDB.getCover(imageURL);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            Thread myThread = new Thread(DoWork);
            myThread.Start();
        }

        private void DoWork()
        {
            DisplayStatus DisplayStatus = new DisplayStatus(Status);
            DisplayOutput DisplayOutput = new DisplayOutput(Output);
            displayCoverDelegate displayCoverDelegate = new displayCoverDelegate(DisplayCoverImage);
            Invoke(DisplayStatus, "Connecting...", 1, 3);

            object[] returnValues = IMDB.getInfo(textBox1.Text);

            Invoke(DisplayOutput,
                returnValues[0].ToString(),
                returnValues[1].ToString(),
                returnValues[2].ToString(),
                returnValues[3].ToString(),
                returnValues[4].ToString(),
                returnValues[5].ToString()
                );

            Invoke(DisplayStatus, "Getting Cover Image...", 2, 3);

            Invoke(displayCoverDelegate, returnValues[6].ToString());

            Invoke(DisplayStatus, "Ready.", 3, 3);
        }
        
    }
}
IMDB.cs Source:
PHP:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;
using System.IO;
using System.Web;
using System.Drawing;

namespace IMDBGrabber
{
    class IMDB
    {

        public static string[] GetStringInBetween(string strBegin, string strEnd, string strSource, bool includeBegin, bool includeEnd)
        {
            string[] result = { "", "" };
            int iIndexOfBegin = strSource.IndexOf(strBegin);
            if (iIndexOfBegin != -1)
            {
                if (includeBegin)
                { iIndexOfBegin -= strBegin.Length; }
                strSource = strSource.Substring(iIndexOfBegin
                    + strBegin.Length);
                int iEnd = strSource.IndexOf(strEnd);

                if (iEnd != -1)
                {
                    if (includeEnd)
                    { iEnd += strEnd.Length; }
                    result[0] = strSource.Substring(0, iEnd);
                    if (iEnd + strEnd.Length < strSource.Length)
                    { result[1] = strSource.Substring(iEnd + strEnd.Length); }
                }
            }
            else
            { result[1] = strSource; }
            return result;
        }

        public static object[] getInfo(string url)
        {
            HttpWebRequest WebReq;
            HttpWebResponse WebResp;
            Stream Answer;
            StreamReader _Answer;
            string[] result;

            string ResponseOutput;

            WebReq = (HttpWebRequest)WebRequest.Create(url);
            WebReq.KeepAlive = false;
            WebReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4";
            WebReq.Method = "GET";

            WebReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            WebReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (WebResp = (HttpWebResponse)WebReq.GetResponse())
            {
                Answer = WebResp.GetResponseStream();
                _Answer = new StreamReader(Answer);
                ResponseOutput = _Answer.ReadToEnd();
            }
            
            string
                title,
                description,
                rating,
                votes,
                releaseDate,
                tagLine,
                coverURL;

            // rating (VOtes), Main Team. Plot, Main Release Date, Cast
            // Director,Release Date,Genre,Tagline,Plot,Cast,Runtime,Country,Langu age, that will be awesome IMHO

            result = GetStringInBetween("name=\"title\" content=\"", "\">", ResponseOutput, false, false);
            title = HttpUtility.HtmlDecode(result[0]);
            
            result = GetStringInBetween("name=\"description\" content=\"", "\">", ResponseOutput, false, false);
            description = HttpUtility.HtmlDecode(result[0]);

            result = GetStringInBetween("        <b>", "</b> ", ResponseOutput, false, false);
            rating = HttpUtility.HtmlDecode(result[0]);

            result = GetStringInBetween("class=\"tn15more\">", "</a>", ResponseOutput, false, false);
            votes = HttpUtility.HtmlDecode(result[0]);

            result = GetStringInBetween("<h5>Release Date:</h5>\n<div class=\"info-content\">\n", "<", ResponseOutput, false, false);
            releaseDate = HttpUtility.HtmlDecode(result[0]);

            result = GetStringInBetween("<h5>Tagline:</h5>\n<div class=\"info-content\">\n", "<", ResponseOutput, false, false);
            tagLine = HttpUtility.HtmlDecode(result[0]);

            result = GetStringInBetween("<link rel=\"image_src\" href=\"", "<", ResponseOutput, false, false);
            coverURL = HttpUtility.HtmlDecode(result[0]);

            return new object[] 
            { 
                title, 
                description, 
                rating, 
                votes, 
                releaseDate,
                tagLine,
                coverURL
            };

        }

        public static Image getCover(string url)
        {
            HttpWebRequest WebReq;
            HttpWebResponse WebResp;
            Image tempImage = null;

            WebReq = (HttpWebRequest)WebRequest.Create(url);
            WebReq.KeepAlive = false;
            WebReq.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.4) Gecko/20100611 Firefox/3.6.4";
            WebReq.Method = "GET";

            WebReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
            WebReq.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;

            using (WebResp = (HttpWebResponse)WebReq.GetResponse())
            {
                System.IO.Stream _WebStream = WebResp.GetResponseStream();
                tempImage = Image.FromStream(_WebStream);
            }

            return tempImage;
        }
    }
}
 

16 comments

Testing it out now! =)
Thanx <3

Preview
sii12n.png
 
The whole point really is to take a look at the code and see what is being done and how to go about improving it. I will continue to carry on and make the program as i said, but thought the source could maybe be the beginning of a few personal projects for you guys :)
 
Make it also to upload the image on tinypic or we, it would be a real win mate. Great application, as usual (you know it, so my comment is useless :p ) <3
 
yeah, i shall continue the project. Include other data as indicated in the remarks in the source code, add error handlers, save to .PNG - etc..
 
I had .Net framework 4 installed already but I guess I need .net Framework 2 for your apps ?

I think we have to enter URL and then it grabs IMDB info, right ?
If thats true, this would be more good. A user simply enters a Movie name and it searches and lists the movies found and then user can select the one he likes.
 
Status
Not open for further replies.

About the author

jayfella
Active Member · Joined
1,600
Messages
565
Reactions
113
Points

Advertise on WJunction

Reach 1000's of webmasters, hosts & affiliates. Banner & sponsored-thread slots available.

Contact us
Back
Top Bottom