[PHP] code
- // Search links
- Function search_link ($ moviename)
- {
- // Construct a 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 the Url to UTF-8 encoding
- $ R = new HttpRequest ($ urlString, HttpRequest: METH_GET); // request
- $ Response = $ r-> send ();
- $ Result = $ r-> getResponseBody ();
- $ Obj = json_decode ($ result); // Parse to json format
- If ($ entry = @ $ obj-> {'entry '}){
- // Search for links and return results in an array
- For ($ I = 0; $ I $ Link = $ entry [$ I]-> {'link '};
- For ($ j = 0; $ j $ Arr = (array) $ link [$ j];
- $ Key = array_search ("self", $ arr );
- If ($ key) {// determines whether the key exists
- $ Str = $ arr ["@ href"]. '? Alt = json'; // Construct the format of the returned link
- Break;
- }
- }
- $ Link_array [] = $ str;
- }
- Return $ link_array; // return link
- } Else
- Echo "Not Found! ";
- }
- // Query the link and add the variables to the $ GLOBAL Array
- Function get_info ($ urlString)
- {
- $ R = new HttpRequest ($ urlString, HttpRequest: METH_GET );
- $ Response = $ r-> send ();
- $ Result = $ r-> getResponseBody ();
- // If... else... determines whether the link is empty
- If ($ obj = json_decode ($ result )){
- // Place the video 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 '};
- // Put the value in db: atri.pdf 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 video name]', '[alias]', '[Director]', '[editor ]', '[Official Website]', '[IMDB link]', '[production date ]',
- '[Country]', '[movie type]', '[release date]', '[display length]', '[number of sets ]', '[language]', '[performer ]');
- @ $ 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 the video information to a file
- Function write_info ($ fi, $ info)
- {
- // Fwrite ($ fi, "*******"); write the file
- Fwrite ($ fi, "[original video name ]");
- Foreach ($ info ["title"] as $ k => $ v)
- Fwrite ($ fi, "$ v \ r \ n ");
- Fwrite ($ fi, "\ r \ n ");
- Fwrite ($ fi, "[buyer ]");
- 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"] ['[performer]']) {
- 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 ");
- // Movie introduction
- 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 ");
- // Comment
- Fwrite ($ fi, '[comment ]');
- 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 ");
- }
- // Search for a movie in the specified folder and return the movie name
- Function find_dir ($ dir)
- {
- If (is_dir ($ dir) {// determines whether the given path name is a directory
- If ($ fdir = opendir ($ dir) {// open the Directory
- While (false! ==( $ File = readdir ($ fdir) {// traverses files in the directory
- If ($ file! = "." & $ File! = "..")
- $ Files [] = $ file; // puts the searched file into an array and returns
- }
- 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) {// determines whether the given path name is a directory
- If ($ fdir = opendir ($ dir) {// open the Directory
- While (false! ==( $ File = readdir ($ fdir) {// traverses files in the directory
- If ($ file! = '.' & $ File! = '..'){
- $ Dirname = $ dir. '\'. $ file;
- If (is_dir ($ dirname) & $ file ==$ moviename) {// find the specified folder
- $ Filename = $ dirname. '\ '.w.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) {// Obtain the movie name in the specified directory for search
- Foreach ($ movienames as $ key => $ moviename ){
- If ($ link_info = search_link ($ moviename )){
- // Search for the video name
- If ($ fi = write_to_dir ($ dir, $ moviename) {// write to the specified folder
- Fwrite ($ fi ," \ R \ n ");
- Fwrite ($ fi ,' ');
- Fwrite ($ fi, "\ r \ n ". ※※※※※※※※※※※※※※※※ ※※※※※'. "\ r \ n ");
- Foreach ($ link_info as $ k ){
- $ Info = get_info ($ k); // Get link information
- Write_info ($ fi, $ info); // write information to the txt file
- Fwrite ($ fi, "*************************************** **************************************** * ***************** \ r \ n ");
- }
- Fclose ($ fi); // the write is complete. close the file.
- }
- } Else
- Echo "Empty! ";
- }
- } Else
- Echo "Movies Not Exists! ";
- ?>
|