Want more features on Pastebin? Sign Up, it's FREE!
Guest

TPB backup to mysql (PHP)

By: a guest on Feb 9th, 2012  |  syntax: PHP  |  size: 3.13 KB  |  views: 248  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2. // This solution isn't elegant but works fine.
  3.  
  4. // CONFIGURATION ---------------------------------------------------
  5. // set memory limit
  6. ini_set('memory_limit', '-1');
  7. // raw TPB file
  8. $tpb_file = "complete";
  9.  
  10. // mySQL config ------------------------------
  11. $mysql_host = "HOST";
  12. $mysql_user = "USER";
  13. $mysql_password = "PASSWORD";
  14. $mysql_database = "DATABASE";
  15. // CREATE THE TABLE BEFORE START THAT SCRIPT!
  16. $mysql_table = "tpb"; //dafault
  17.  
  18. /* create the new table
  19. $query = "CREATE TABLE `my_onlinemanga`.`tpb` (
  20. `ID` DOUBLE NOT NULL ,
  21. `name` TEXT NOT NULL ,
  22. `size` DOUBLE NOT NULL ,
  23. `seeders` INT NOT NULL ,
  24. `leechers` INT NOT NULL ,
  25. `magnet` TEXT NOT NULL
  26. )";
  27. */
  28.  
  29. // ------------------------------------------
  30. // ----------------------------------------------------------------
  31.  
  32. // Connecting, selecting database
  33. $link = mysql_connect($mysql_host, $mysql_user, $mysql_password)
  34.     or die('Could not connect: ' . mysql_error());
  35. echo 'Connected successfully to MySQL';
  36. mysql_select_db($mysql_database) or die('Could not select database');
  37.  
  38. // Performing SQL query
  39. $query = "SELECT * FROM $mysql_table";
  40. $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  41.  
  42. // read file into array
  43. $file_array = file($tpb_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  44.  
  45. $file_row_num = count($file_array);
  46.  
  47. // start with code manipulation--------
  48. for($i=0; $i < $file_row_num; $i++){
  49.   $original = $file_array[$i];
  50.   // string lengh -1
  51.   $len = strlen($original);
  52.   $tpbid = substr($original, 0, 7); // TPD ID for database
  53.  
  54.   $original = substr($original, 8, $len);  // eliminate ID
  55.   $reverse = strrev($original); //reverse the string for check '|'
  56.   $pos = strpos($reverse, '|'); // last '|' [reversed]
  57.   $magnetlink = substr($reverse, 0, $pos); // magnet link [reversed]
  58.   $magnetlink = strrev($magnetlink); // MAGNET LINK for database
  59.  
  60.   $len = strlen($reverse);
  61.   $reverse = substr($reverse, $pos + 1, $len);
  62.   $pos = strpos($reverse, '|'); // last '|' [reversed]
  63.   $leechers = substr($reverse, 0, $pos); // leechers [reversed]
  64.   $leechers = strrev($leechers); // LEECHERS for database
  65.  
  66.   $len = strlen($reverse);
  67.   $reverse = substr($reverse, $pos + 1, $len);
  68.   $pos = strpos($reverse, '|'); // last '|' [reversed]
  69.   $seeders = substr($reverse, 0, $pos); // seeders [reversed]
  70.   $seeders = strrev($seeders); // SEEDERS for database
  71.  
  72.   $len = strlen($reverse);
  73.   $reverse = substr($reverse, $pos + 1, $len);
  74.   $pos = strpos($reverse, '|'); // last '|' [reversed]
  75.   $size = substr($reverse, 0, $pos); // size [reversed]
  76.   $size = strrev($size); // SIZE for database
  77.  
  78.   $len = strlen($reverse);
  79.   $name = substr($reverse, $pos + 1, $len); //name [reversed]
  80.   $name = strrev($name); // NAME for database
  81.  
  82.   // test
  83.   // echo ("ID: $tpbid\nName: $name\nSize: $size\nSeed: $seeders\nLeech: $leechers\nMagnet link: $magnet_header$magnetlink\n\n");
  84.  
  85.   // insert to sql
  86.   $query = "INSERT INTO tpb VALUES ('$tpbid','$name','$size','$seeders','$leechers','$magnetlink')";
  87.   $result = mysql_query($query) or die('Query failed: ' . mysql_error());
  88. }
  89.  
  90. // Closing connection
  91. mysql_close($link);
  92. ?>
clone this paste RAW Paste Data