[PHP] Code
- Search Links
- function Search_link ($moviename)
- {
- Constructs a URL where max-results can change 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 link and exist in array to return
- for ($i = 0; $i<>
- $link = $entry [$i]->{' link '};
- for ($j = 0; $j<>
- $arr = (array) $link [$j];
- $key = Array_search ("Self", $arr);
- if ($key) {//Determine if key exists
- $STR = $arr ["@href"]. Alt=json '; Constructs a return link format
- Break
- }
- }
- $link _array[] = $str;
- }
- Return $link _array;//Back link
- } else
- echo "Not found!";
- }
- Query links and put variables into the $global array
- function Get_info ($urlString)
- {
- $r = new HttpRequest ($urlString, httprequest::meth_get);
- $response = $r->send ();
- $result = $r->getresponsebody ();
- If.. else.. Determine if open link is empty
- if ($obj = Json_decode ($result)) {
- Put the movie's information in a 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 '};
- To put the values in Db:atribute into an 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;
- }
- Sets the key value of the corresponding array
- @ $db _array_key = Array (' "movie formerly known as" ', ' "' Alias ' ', '" director "', '" screenwriter "', '" "Official website" ', ' "" IMDb link "', '" vintage ",
- ' Country ' ', ' ' movie type ' ', ' ' Release date ', ' ' show length ' ', ' Set Number ' ', ' ' language ' ', ' ' ' ' ' ' ' '
- @ $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" and $title, "author" and "$author", "Summary" and "$summary", "ID" and "$ID", "link" and "$link", "gd" = > $gd, "db_array" = $db _array);
- return $info;
- }
- Writing movie information to a file
- function Write_info ($fi, $info)
- {
- Fwrite ($fi, "******"); writing files
- Fwrite ($fi, "the original title of the film");
- foreach ($info ["title"] as $k = + $v)
- Fwrite ($fi, "$v \ r \ n");
- Fwrite ($fi, "\ r \ n");
- Fwrite ($fi, "producer");
- foreach ($info ["Author"] as $value)
- foreach ($value as $key)
- foreach ($key as $v) {
- Fwrite ($fi, "$v \ r \ n");
- }
- Fwrite ($fi, "\ r \ n");
- 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, "$v \ r \ n");
- } else{
- foreach ($value as $k = $v)
- Fwrite ($fi, "$v");
- Fwrite ($fi, "\ r \ n");
- }
- }
- }
- Fwrite ($fi, "\ r \ n");
- About the film
- Fwrite ($fi, "Introduction" \ r \ n ");
- foreach ($info ["Summary"] as $k = + $v) {
- Fwrite ($fi, "$v \ r \ n");
- }
- Fwrite ($fi, "\ r \ n");
- Id
- Fwrite ($fi, "I D" ");
- foreach ($info ["ID"] as $k = + $v) {
- Fwrite ($fi, $v);
- }
- Fwrite ($fi, "\ r \ n");
- Link
- Fwrite ($fi, "link" \ r \ n ");
- foreach ($info ["link"] as $value) {
- foreach ($value as $k = = $v) {
- Fwrite ($fi, "$v");
- }
- Fwrite ($fi, "\ r \ n");
- }
- Fwrite ($fi, "\ r \ n");
- Comments
- Fwrite ($fi, ' comments ');
- Fwrite ($fi, $info ["GD"] ["@numRaters"]);
- Fwrite ($fi, "\ r \ n");
- 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, "\ r \ n");
- }
- Find a movie in the specified folder and return to the movie name
- function Find_dir ($dir)
- {
- if (Is_dir ($dir)) {//Determine if the given pathname is a directory
- if ($fdir =opendir ($dir)) {//Open Directory
- while (false!== ($file =readdir ($fdir))) {//Traverse files in Directory
- if ($file! = "." && $file! = "...")
- $files []= $file; Put the found file 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 if the given pathname is a directory
- if ($fdir =opendir ($dir)) {//Open Directory
- while (false!== ($file =readdir ($fdir))) {//Traverse files in Directory
- if ($file! = '. ' && $file! = ' ... ') {
- $dirname = $dir. ' \ \ '. $file;
- if (Is_dir ($dirname) && $file = = $moviename) {//search for the specified folder
- $filename = $dirname. ' \ \ '. $moviename. ' TXT ';
- if (!file_exists ($filename)) {
- if ($fi =fopen ($filename, ' w+ ')) {//Opens the specified file, if it does not exist, creates the file and returns
- 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 name of the movie in the specified directory, search
- foreach ($movienames as $key = = $moviename) {
- if ($link _info = Search_link ($moviename)) {
- Search for movie names
- if ($fi =write_to_dir ($dir, $moviename)) {//write to the specified folder
- Fwrite ($fi, " \ r \ n");
- Fwrite ($fi, " );
- Fwrite ($fi, "\r\n\r\n\r\n". ※※※※※※※※※※※※※※※※※※※※※ film Information ※※※※※※※※※※※※※※※※※※※※※ '. \ r \ n ");
- foreach ($link _info as $k) {
- $info =get_info ($k); Get link Information
- Write_info ($fi, $info); Write information in TXT file
- Fwrite ($fi, "*************************************************************************************************\ r\n ");
- }
- Fclose ($FI);//write complete, close file
- }
- } else
- echo "empty!";
- }
- } else
- echo "Movies not exists!";
- ?>
Copy Code |