[PHP] code
- //Search link
- function search_link($moviename)
- {
- //Construct url, where max-results can be changed as needed
- $urlString = 'http://api.douban. com/movie/subjects?q='.$moviename.'&start-index=1&max-results=1&alt=json';
- //print_r($urlString);
- $urlString=mb_convert_encoding($urlString, "UTF-8" , "GBK");//Convert Url to utf-8 encoding
- $r = new HttpRequest($urlString,HttpRequest::METH_GET);//Request
- $response = $r->send();
- $ result = $r->getResponseBody();
- $obj = json_decode($result);//Parse into json format
-
- if($entry = @$obj->{'entry'}){
- // Search the link and return it in the array
- for($i = 0;$i $link=$entry[$i]->{'link'};
- for( $j = 0;$j $arr = (array)$link[$j];
- $key = array_search("self",$arr);
- if($ key){ //Determine whether the key exists
- $str = $arr["@href"].'?alt=json'; //Construct the format of the return link
- break;
- }
- }
- $link_array[] = $ str;
- }
- return $link_array;//Return to the link
- } else
- echo "Not Found!";
- }
- //Query the link and put each variable into the $GLOBAL array
- function get_info($urlString)
- {
- $r = new HttpRequest($urlString,HttpRequest::METH_GET);
- $response = $r->send();
- $result = $r->getResponseBody();
-
- //if.. else.. Determine whether the open link is empty
- if ($obj = json_decode($result)){
- //Put the video information in the global array
- $title = $obj->{'title'};
- $author = $obj->{'author'};
- $summary = $obj->{'summary'};
- $ID=$obj->{'id'};
- $link = $obj ->{'link'};
- $gd = (array)$obj->{'gd:rating'};
- $db_array = array();
-
- $db = $obj->{'db :attribute'};
- //Put the value in db:atribute into the array
- foreach ($db as $value){
- $value_array = (array)($value);
- $v = $value_array["@name "];
- $k = $value_array["$t"];
- if (array_key_exists("@lang",$value_array)){
- $lang=$value_array["@lang"];
- $k=$k .'['.$lang.']';
- }
- $db_array[$v][]=$k;
- }
- //Set the key value of the corresponding array
- @$db_array_key = array('[Original name of the video 】','[Alias]','[Director]','[Screenwriter]','[Official Website]','[IMDB Link]','[Year of Production]',
- '[Country]',' [Movie Type]','[Release Date]','[Screen Length]','[Number of Episodes]','[Language]','[Actor]');
- @$db_array_value = array($db_array[ "title"],$db_array["aka"],$db_array["director"],$db_array["writer"],$db_array["website"],
- $db_array["imdb"],$db_array[" year"],$db_array["country"],$db_array["movie_type"],$db_array["pubdate"],
- $db_array["movie_duration"],$db_array["episodes"],$db_array["language "],$db_array["cast"]);
- @$db_array = array_combine($db_array_key,$db_array_value);
- } else{
- echo "Empty link!";
- }
- $info=array("title"= >$title,"author"=>$author,"summary"=>$summary,"ID"=>$ID,"link"=>$link,"gd"=>$gd, "db_array"=>$db_array);
- return $info;
- }
- //Write movie information to the file
- function write_info($fi,$info)
- {
- //fwrite($fi, "******");Write the file
- fwrite($fi, "【 Original title of the video】");
- foreach ($info["title"] as $k=>$v)
- fwrite($fi,"$vrn");
- fwrite($fi,"rn");
-
- fwrite($fi,"[producer]");
- foreach ($info["author"] as $value)
- foreach ($value as $key)
- foreach ($key as $v) {
- fwrite($ fi,"$vrn");
- }
- fwrite($fi,"rn");
-
- //db:attribute
- foreach ($info["db_array"] as $key=>$value){
- if (count($value)){
- fwrite($fi,$key);
- if($value==$info["db_array"]['[actor]']){
- foreach($value as $k= >$v)
- fwrite($fi,"$vrn ");
- } else{
- foreach($value as $k=>$v)
- fwrite($fi,"$v ");
- fwrite( $fi,"rn");
- }
- }
- }
- fwrite($fi,"rn");
-
- //Video introduction
- fwrite($fi, "[Introduction]rn");
- foreach ($info ["summary"] as $k=>$v){
- fwrite($fi," $v rn");
- }
- fwrite($fi,"rn");
-
- //ID
- fwrite($ fi,"[I D]");
- foreach ($info["ID"] as $k=>$v){
- fwrite($fi,$v);
- }
- fwrite($fi,"rn" );
-
- //Link
- fwrite($fi,"[Link]rn");
- foreach ($info["link"] as $value){
- foreach ($value as $k=>$v) {
- fwrite($fi, " $v ");
- }
- fwrite($fi,"rn");
- }
- fwrite($fi,"rn");
-
- //Comment
- fwrite($fi, '[Comment]');
- fwrite($fi,$info["gd"]["@numRaters"]);
- fwrite($fi,"rn");
- fwrite($fi,'[Highest]' ); fwrite($fi,$info["gd"]["@max"]);
- fwrite($fi," [minimum]");fwrite($fi,$info["gd"]["@ min"]);
- fwrite($fi,' [Average]');fwrite($fi, $info["gd"]["@average"]);
- fwrite($fi,"rn");
- }
- //Find the movie in the specified folder and return the movie name
- function find_dir($dir)
- {
- if(is_dir($dir)){ //Judge whether the given path name is a directory
- if($fdir =opendir($dir)){ //Open the directory
- while(false!==($file=readdir($fdir))){ //Traverse the files in the directory
- if ($file != "." && $ file != "..")
- $files[]=$file; //Put the found files into the array and return
- }
- closedir($fdir);
- print_r($file);
- return $files;
- }
- }
- }
- //Place the generated txt file in the specified directory
- function write_to_dir($dir,$moviename)
- {
- if (is_dir($dir)){ //Determine whether the given path name is For the directory
- if($fdir=opendir($dir)){ //Open the directory
- while(false!==($file=readdir($fdir))){ //Traverse the files in the directory
- if ($file != '.' && $file != '..') {
- $dirname=$dir.'\'.$file;
- if (is_dir($dirname)&&$file==$moviename) { //Find Specified folder
- $filename=$dirname.'\'.$moviename.'.txt';
- //if(!file_exists($filename)){
- if($fi=fopen($filename,'w+' )){ //Open the specified file. If it does not exist, create the file and return
- closedir($fdir);
- return $fi;
- }else
- echo "Fail to Open file!";
- //}
- }else
- echo "Dir Not Exists!";
- }
- }
- } else {
- echo 'Fail to Open!';
- return;
- }
- } else {
- echo 'Directory Not Exists!';
- return;
- }
- }
-
- $dir = 'E:film';
- if($movienames = @find_dir($dir)){ //Get the movie name in the specified directory and search
- foreach ($movienames as $key=> $moviename){
- if($link_info = search_link($moviename)){
-
- //Search for movie name
- if ($fi=write_to_dir($dir,$moviename)){ //Write to the specified folder
- fwrite( $fi,"rn");
- fwrite($fi,'');
- fwrite($fi,"rnrnrn".'※※※※※※※※※※※※※ ※※※※※※※※Video information※※※※※※※※※※※※※※※※※※※※※'."rn");
-
- foreach ($link_info as $k){
- $info=get_info($k); //Get link information
- write_info($fi,$info); //Write information in txt file
- fwrite($fi,"********** *************************************************** *************************************rn");
- }
- fclose($fi); //Writing completed, close the file
- }
- } else
- echo "Empty!";
- }
- } else
- echo "Movies Not Exists!";
- ?>
Copy code
|