mkvmanager /tmp/allocinescraper.php

Language PHP Lines 219
MD5 Hash ed1896dca32260bb3194d453b943c268 Estimated Cost $4,464 (why?)
Repository https://github.com/bdunogier/mkvmanager.git View Raw File View Project SPDX
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
if ( !isset( $_GET['q'] ) and !isset( $_GET['movie'] ) )
{
    exit('q|movie GET parameter missing');
}

// search
if ( isset( $_GET['q'] ) )
{
    $query = $_GET['q'];
    if ( preg_match( '#([^\(]+) \(([0-9]{4})\)#', $query, $queryMatches ) )
    {
        $query = $queryMatches[1];
        $queryYear = $queryMatches[2];
    }
    echo "query: $query<br />";
    echo "queryYear: $queryYear<br />";

    $searchURL = 'http://www.allocine.fr/recherche/?q=' . urlencode( $query );
    $searchPage = file_get_contents( $searchURL );

    $searchPage = substr( $searchPage, strpos( $searchPage, '<h3><b>Films <h4>' ) );
    $searchPage = substr( $searchPage, 0, strpos( $searchPage, '<h3><b>', 1 ) );

    // @todo match more details (year, director, actors, image)
    preg_match_all( '#<a href="/film/fichefilm_gen_cfilm=([0-9]+)\.html" class="link1">(.*?)</a>#', $searchPage, $matches, PREG_SET_ORDER );
    preg_match_all( '#&nbsp;\(([^)]+)\)</a>#', $searchPage, $origTitleMatches, PREG_SET_ORDER );
    preg_match_all( '#<h4 style="color: \#808080">([0-9]{4})</h4>#', $searchPage, $yearMatches, PREG_SET_ORDER );
    foreach ( $matches as $key => $match )
    {
        $movieTitle = strip_tags( $match[2] );
        $originalTitle = strip_tags( $origTitleMatches[$key][1] );
        $year = strip_tags( $yearMatches[$key][1] );
        $perfectMatch = false;
        if ( strtolower( $movieTitle ) == strtolower( $query ) or ( strtolower( $originalTitle ) == strtolower( $query ) ) )
        {
            if ( isset( $queryYear ) )
            {
                if ( $queryYear == $year )
                    $perfectMatch = true;
            }
            else
                $perfectMatch = true;
        }
        elseif ( isset( $queryYear ) && ( $queryYear == $year ) )
        {
            $perfectMatch = true;
        }
        $movieID = $match[1];
        $url = $_SERVER['PHP_SELF'] . '?movie=' . urlencode( $movieID );
        if ( $perfectMatch ) echo "<b>";
        echo "<a href=\"$url\">$movieTitle ($year)</a> (<i>$originalTitle</i>)";
        if ( $perfectMatch ) echo "</b>";
        echo "<br />";
    }
}
// details page
elseif ( isset( $_GET['movie'] ) )
{
    $movieData = array();
    $detailsPage = utf8_encode( file_get_contents( 'http://www.allocine.fr/film/fichefilm_gen_cfilm=' . $_GET['movie'] . '.html' ) );

    // title
    if ( preg_match( '|<h1 class="TitleFilm">([^<]+)</h1>|', $detailsPage, $matches ) )
    {
        $movieData['title'] = $matches[1];
    }

    // original title
    if ( preg_match( '|<h3 class="SpProse">Titre original : <i>([^<]+)</i></h3>|', $detailsPage, $matches ) )
    {
        $movieData['original-title'] = $matches[1];
    }
    else
    {
        $movieData['original-title'] = $movieData['title'];
    }

    // release date
    if ( preg_match( '|<h4>Date de sortie : <b>(.*?)</b>|', $detailsPage, $matches ) )
    {
        $movieData['release-date'] = $matches[1];
    }

    // genre
    $substring = substr( $detailsPage, strpos( $detailsPage, 'Genre : ' ) + 8 );
    $substring = substr( $substring, 0, strpos( $substring, '</h3>' ) );
    if ( preg_match_all( '#<a href="(/film/alaffiche_genre_gen_genre[^"]+)" class="link1">([^<]+)</a>#', $substring, $genreMatches, PREG_SET_ORDER ) )
    {
        foreach( $genreMatches as $genreMatch )
        {
            $movieData['genre'][] = $genreMatch[2];
        }
    }

    // director(s)
    $substring = substr( $detailsPage, strpos( $detailsPage, 'Réalisé par ' ) + 12 );
    $substring = substr( $substring, 0, strpos( $substring, '</h3>' ) );
    if ( preg_match_all( '#<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>#', $substring, $matches, PREG_SET_ORDER ) )
    {
        foreach ( $matches as $match )
        {
            $movieData['directors'][] = array( 'name' => $match[2], 'url' => $match[1] );
        }
    }

    // distributed by...
    if ( strstr( $detailsPage, 'Distribué par ' ) !== false )
    {
        $substring = substr( $detailsPage, strpos( $detailsPage, 'Distribué par ' ) + 14 );
        $substring = substr( $substring, 0, strpos( $substring, '</h3>' ) );
        if ( preg_match_all( '#<a[^>]*href="([^"]+)"[^>]*>(.*?)</a>#', $substring, $matches, PREG_SET_ORDER ) )
        {
            foreach ( $matches as $match )
            {
                $movieData['distributed-by'][] = array( 'name' => $match[2], 'url' => $match[1] );
            }
        }
    }

    // runtime
    if ( preg_match( '#Durée : ([^\.]+).#', $detailsPage, $runtimeMatches ) )
    {
        sscanf( $runtimeMatches[1], '%dh %dmin', $hours, $minutes );
        $movieData['runtime'] = $hours * 60 + $minutes;
    }

    // synopsis
    $substring = substr( $detailsPage, strpos( $detailsPage, '<h2 class="SpBlocTitle" >Synopsis</h2>' ) + 38 );
    // echo "$substring";
    $substring = substr( $substring, strpos( $substring, '<h4>' ) + 4 );
    $substring = substr( $substring, 0, strpos( $substring, '</h4>' ) );
    $movieData['synopsis'] = utf8_decode( $substring );
    unset( $substring );

    // images
    $imagesPage = utf8_encode( file_get_contents( 'http://www.allocine.fr/film/galerievignette_gen_cfilm=' . $_GET['movie'] . '.html' ) );
    if ( preg_match( '#<img id=\'imgNormal\' class=\'photo\' src=\'([^\']+)\'#', $imagesPage, $matches ) )
    {
        $movieData['cover'] = $matches[1];
    }

    // trailers
    if ( preg_match( '#<a href="/video/player_gen_cmedia=([0-9]+)&cfilm=[0-9]+\.html" class="link5">#', $detailsPage, $trailerLinkMatches ) )
    {
        $videoID = $trailerLinkMatches[1];
        $videoListURL = "http://www.allocine.fr/webtv/film.html?cfilm={$_GET['movie']}";
        $trailersListPage = utf8_encode( file_get_contents( $videoListURL ) );
        // $trailersPage = utf8_encode( file_get_contents( 'http://www.allocine.fr/webtv/acvision.asp?nopub=1&emission=&player=ASF&debit=HD&cvid=' . $videoID ) );
        if ( preg_match_all( '#<a href="(acvision\.asp\?cvid=[0-9]+)" [^>]+>([^<]+)</a>#', $trailersListPage, $trailersMatches, PREG_SET_ORDER ) )
        {
            foreach( $trailersMatches as $trailerMatch )
            {
                $trailerLabel = strtolower( utf8_decode( $trailerMatch[2] ) );
                if ( strpos( $trailerLabel, 'annonce' ) !== false )
                {
                    $trailerURL = 'http://www.allocine.fr/webtv/' . $trailerMatch[1] . '&nopub=1&player=ASF';
                    $trailerHTML = utf8_encode( file_get_contents( $trailerURL . '&debit=HD' ) );
                    if ( !$trailerFileURL = GetTrailerFileURL( $trailerHTML ) )
                    {
                        $trailerHTML = utf8_encode( file_get_contents( $trailerURL . '&debit=H' ) );
                        if ( !$trailerFileURL = GetTrailerFileURL( $trailerHTML ) )
                        {
                            continue;
                        }
                    }
                    // $trailerPage = utf8_encode( file_get_contents( $trailerURL ) );
                    $movieData['trailers'][] = array( 'name' => $trailerLabel, 'url' => $trailerFileURL );
                }
            }
        }
    }

    // casting
    $movieData['actors'] = array();
    $castingURL = "http://www.allocine.fr/film/casting_gen_cfilm={$_GET['movie']}.html";
    $castingHTML = utf8_encode( file_get_contents( $castingURL ) );
    $substring = substr( $castingHTML, strpos( $castingHTML, '<h2 class="SpProse" style="color: #D20000; font-weight:bold;">Acteurs</h2>' ) + 74 );
    $substring = substr( $substring, 0, strpos( $substring, '</table' ) );
    if ( preg_match_all( '#<h5><a href="(/personne/fichepersonne_gen_cpersonne=[0-9]+\.html)" class="link1">([^<]+)</a></h5>#', $substring, $actorsMatches, PREG_SET_ORDER ) )
    {
        preg_match_all( '#<h5>([^<]+)</h5>#', $substring, $actorsRolesMatches );
        foreach ( $actorsMatches as $key => $actorMatch )
        {
            $actor = array();
            $actor['link'] = $actorMatch[1];
            $actor['name'] = $actorMatch[2];
            $actor['role'] = $actorsRolesMatches[1][$key];

            // actor role
            $movieData['actors'][] = $actor;
            unset( $actor );
        }
    }

    echo "<b>MOVIE DATA</b><br /><pre>";
    print_r( $movieData );
    echo "</pre>";
}

function GetTrailerFileURL( $trailerHTML )
{
    if ( preg_match( '|<PARAM name="URL" value="([^"]+)">|', $trailerHTML, $trailerGeneratorMatch ) )
    {
        // get generation page to get the MMS URL
        $generationURL = $trailerGeneratorMatch[1];
        $generationHTML = utf8_encode( file_get_contents( $generationURL ) );
        if ( preg_match( '|<REF HREF = "(mms://[^"]+)" />|', $generationHTML, $generationMatch ) )
        {
            $trailerURL = $generationMatch[1];
            $trailerURL = substr( $trailerURL, strpos( $trailerURL, 'mediaplayer.allocine.fr' ) );
            $trailerURL = str_replace( '.wmv', '.flv', $trailerURL );
            $trailerURL = 'http://h.fr.' . $trailerURL;
            return $trailerURL;
        }
    }
    return false;
}
?>
Back to Top