WN

WN (https://www.wn.se/forum/index.php)
-   Off Topic (https://www.wn.se/forum/forumdisplay.php?f=7)
-   -   IMDB-Parser (https://www.wn.se/forum/showthread.php?t=1049233)

Kekke 2011-07-24 13:49

IMDB-Parser
 
Skrev en snabb och simpel IMDB-parser i php som jag tänkte dela med mig av.

En snabb titt på hur det ser ut;
http://78.69.194.141/imdb.php

Användning (imdb.php);
Kod:

<?php
include('cImdbParser.php');
$cImdb = new cImdb();

if(isset($_POST['imdb']) && $_POST['imdb'] != "") {
        if($cImdb->getData("http://www.imdb.com/title/tt1375666/")) {
                echo "Data retrieved..<br />";
        }

        if($cImdb->getMovieTitle() == "") {
                echo "Title does not exist..<br />";
        }
        else {
                echo "Title: " . $cImdb->getMovieTitle() . "<br />";
        }

        if($cImdb->getFirstGenre() == "") {
                echo "First genre does not exist..<br />";
        }
        else {
                echo "First genre: " . $cImdb->getFirstGenre() . "<br />";
        }

        if($cImdb->getSecondGenre() == "") {
                echo "Second genre does not exist..<br />";
        }
        else {
                echo "Second genre: " . $cImdb->getSecondGenre() . "<br />";
        }

        if($cImdb->getYear() == "") {
                echo "Year does not exist..<br />";
        }
        else {
                echo "Year: " . $cImdb->getYear() . "<br />";
        }

        if($cImdb->getMovieRating() == "") {
                echo "Rating does not exist..<br />";
        }
        else {
                echo "Rating: " . $cImdb->getMovieRating() . "<br />";
        }
        echo '<a href="imdb.php">New search</a>';
}
else {
?>
        <form method="post" action="imdb.php">
        IMDB-Url (e.g. http://www.imdb.com/title/tt1375666/):<br />
        <input type="text" name="imdb" />
        <br />
        <input type="submit" value="Retrieve data" />
        </form>
<?php
}
?>

och klassen (cImdbParser.php);
Kod:

<?php
class cImdb
{
        private $movieData;
        private $genreOffset;
        private $yearOffset;

        function __construct()
        {
                $this->genreOffset = 13;
                $this->yearOffset = 12;
        }

        function getData($movieUrl)
        {
                //Parse the HTML
                $this->movieData = file_get_contents($movieUrl);

                //If we couldnt get the contents, return false
                if($this->movieData === FALSE) {
                        return false;
                }
                else {
                        return true;
                }
        }
       
        function getYear()
        {
                $firstYearPos = strpos($this->movieData, "href=\"/year/");

                if($firstYearPos > 0) {
                        //First year pos found

                        //Add the offset..
                        $firstYearPos += $this->yearOffset;

                        //Year should be 4 letters long :-P
                        $yearLength = 4;

                        //And then we get the year, if we do, return it.
                        $firstYear = substr($this->movieData, $firstYearPos, $yearLength);

                        //It doesnt matter if the string is empty, as we return empty string on false retrieval of year
                        return $firstYear;
                }
                else {
                        return "";
                }
        }

        function getFirstGenre()
        {
                $firstGenrePos = strpos($this->movieData, "href=\"/genre/");

                if($firstGenrePos > 0) {
                        //First Genre Found

                        //Add the genre string offset
                        $firstGenrePos += $this->genreOffset;

                        //Get the length of the genre, find the " after the /genre/
                        //In a string that looks like this href="/genre/Action"
                        $genreLength = strpos($this->movieData, "\"", $firstGenrePos)-$firstGenrePos;

                        //Get the genre by finding the html link to the genre.
                        $firstGenre = substr($this->movieData, $firstGenrePos, $genreLength);

                        //Return the genre
                        return $firstGenre;
                }
                else {
                        return "";
                }
        }

        function getSecondGenre()
        {
                $firstGenrePos = strpos($this->movieData, "href=\"/genre/");

                if($firstGenrePos > 0) {
                        //First Genre Found

                        //Add the genre string offset
                        $firstGenrePos += $this->genreOffset;

                        $secondGenrePos = strpos($this->movieData, "href=\"/genre/", $firstGenrePos);

                        if($secondGenrePos > 0) {
                                //Found second genre pos

                                //Add the genre offset again
                                $secondGenrePos += $this->genreOffset;

                                //Get the length of the genre, find the " after the /genre/
                                //In a string that looks like this href="/genre/Action"
                                $genreLength = strpos($this->movieData, "\"", $secondGenrePos)-$secondGenrePos;

                                //Get the genre by finding the html link to the genre.
                                $secondGenre = substr($this->movieData, $secondGenrePos, $genreLength);

                                //Return the genre
                                return $secondGenre;
                        }
                        else {
                                return "";
                        }
                }
                else {
                        return "";
                }
        }

        function getMovieRating()
        {
                $ratingPos = strpos($this->movieData, 'title="Users rated this ');

                if($ratingPos > 0) {
                        //Found rating position, proceed

                        $ratingOffset = strlen('title="Users rated this ');

                        //Add the offset to the position..
                        $ratingPos += $ratingOffset;

                        //Known length, always 3 (I think?)
                        $ratingLength = 3;

                        //Do the magic and get the rating from our data
                        $rating = substr($this->movieData, $ratingPos, $ratingLength);

                        return $rating;
                }
                else {
                        return "";
                }
        }

        function getMovieTitle()
        {
                $titlePos = strpos($this->movieData, '<h1 class="header" itemprop="name">');

                if($titlePos > 0) {
                        //Found the position of the title

                        //Add the offset
                        $titleOffset = strlen('<h1 class="header" itemprop="name">');
                        $titlePos += $titleOffset;

                        //Get the length of the title, find the newline after the needle
                        $titleLength = strpos($this->movieData, "\n", $titlePos+1)-$titlePos;

                        //Get the title
                        $title = substr($this->movieData, $titlePos, $titleLength);

                        //And return it..
                        return $title;
                }
                else {
                        return "";
                }
        }
}
?>

Simma lugnt!


Alla tider är GMT +2. Klockan är nu 02:08.

Programvara från: vBulletin® Version 3.8.2
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Svensk översättning av: Anders Pettersson