📜 ⬆️ ⬇️

Making a personal collection of radio charts

VKontakte is unloved by many, but it provides a wonderful and stable storehouse of music. As part of the script described below, we will learn to download a song from there by name, to parse the charts of radio stations and to pack the songs we downloaded into a zip-archive. The songs will be numbered according to the place in the chart, and 100 songs are usually easily put on a CD - motorists will like it.
Also in one of the main roles is the moskva.fm site, which has carefully collected in one place a list of a large number of radio stations with charts. Everything is very trivial, but I think many novice programmers will be interested.
Screen of what happened:


Clicking on the logo downloads a fresh zip-archive of the top 100 of this station containing files in mp3 format.
So, let's go from smaller to larger:
error_reporting(E_ALL); //    simplehtmldom include_once('simplydom.php'); //100   50  = 5000  //  - 9000 set_time_limit(9000); 

The Simpl e HTMLDOM project is an unpretentious counterpart to SimplyXML and other HTML / DOM libraries. Distinctive features: wrote a Chinese, as seen by the name, absolutely spits on the errors in the markup, trying to disassemble the file as it will; crashes into segmentation fault after a couple of uses; fiercely eats memory, because for each node, it keeps parents and children in the very object, the insides can be visually viewed by outputting the object through print_r, there are also small, imperceptible but very cool things, for example, you can search by negative indices, i.e. if we need to get the third div from the end of the page, then we will not have any difficulties.
Let's go ahead - we will write a function that will receive the song by name and save it in a specific place.
 /* *          $title *        $path, *      "$number $title.mp3" */ function getSongByTitle($title,$path,$number) { //    UserAgent $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13)'; $cookie = ''; //     $login = '*******@gmail.com'; $password = '********'; // cURL $ch = curl_init(); //   POST  curl_setopt($ch, CURLOPT_POST, true); // UserAgent curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); //        //         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //     curl_setopt($ch, CURLOPT_TIMEOUT, 10); //    Location: ... curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //        curl_setopt($ch, CURLOPT_URL, 'http://login.vk.com/?act=login'); // POST- $post = array( 'act' => 'login', 'q' => '', 'al_frame' => '1', 'expire' => '', 'captcha_sid' => '', 'captcha_key' => '', 'from_host' => 'vkontakte.ru', 'email' => $login, 'pass' => $password ); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post)); // $answer = curl_exec($ch); //  -    $sid = substr($answer, strpos($answer, "setCookieEx('sid', '") + 20, 60); $cookie = 'remixsid=' . $sid; //    GET curl_setopt($ch, CURLOPT_POST, false); // cookie curl_setopt($ch, CURLOPT_COOKIE, $cookie); // URL curl_setopt($ch, CURLOPT_URL, 'http://vkontakte.ru/al_search.php?al=1&c[q]='.urlencode($title).'&c[section]=audio'); //    $answer = curl_exec($ch); curl_close($ch); //     json    html //       $answer = substr($answer,strpos($answer,'{"section":"audio"')); $answer = substr($answer,strpos($answer,'<!> ')+4); $answer = trim($answer); //     ,     if (!file_exists($path . $number . ' ' . $title . '.mp3')) { if (strpos($answer,'      ')==false) { //  simplydom, $html = str_get_html($answer); //   input,  99%   //     $filelink = $html->find('input',0)->attr['value']; //      http://....mp3,bitrate // -  -    bitrate $filelink = explode(",",$filelink); $filelink = $filelink[0]; echo "   $filelink \n"; $ch = curl_init(); //    (  ,   ) $fp = fopen($path . $number . ' ' . $title . '.mp3', 'w'); //    cURL    curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_URL, $filelink); curl_setopt($ch, CURLOPT_COOKIE, $cookie); $answer = curl_exec($ch); fclose($fp); echo "  $filelink \n"; //   ,     PHP  seg.fault $html->clear(); unset($html); } else { echo "  \n"; } } } /* *   zip-,         */ function create_zip($files = array(),$destination = '') { if(file_exists($destination)) { return false; } $valid_files = array(); if(is_array($files)) { foreach($files as $file) { if(file_exists($file)) { $valid_files[] = $file; } } } if(count($valid_files)) { $zip = new ZipArchive(); if($zip->open($destination,ZIPARCHIVE::CREATE) !== true) { return false; } foreach($valid_files as $file) { $zip->addFile($file,$file); } $zip->close(); return file_exists($destination); } else { return false; } } $filename = array(); $filename[] = array('href'=>'http://www.moskva.fm/stations/FM_90.8/top100','fm'=>'90_8'); //    ,       //           $filename[] = array('href'=>'http://www.moskva.fm/stations/FM_101.2/top100','fm'=>'101_2'); foreach ($filename as $item) { //    -     $user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.2.13) Gecko/20101203'; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $user_agent); curl_setopt($ch, CURLOPT_URL, $item['href']); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); $answer = curl_exec($ch); $answer = trim($answer); curl_close($ch); echo '  ' . $item['href'] . ' '; $html = str_get_html($answer); // $filesarray            $filesarray = array(); for ($i = 0; $i < 100; $i++) { echo '----' . $i . '-------- '; $song = $html->find('td[class=name] pa[class=song]', $i)->innertext; $artist = $html->find('td[class=name] pa[class=artist]', $i)->innertext; $fullsongname = $artist . ' - ' . $song; echo '  ' . $i . ' ' . $fullsongname . "\n"; if (!is_dir($_SERVER['DOCUMENT_ROOT'].'/mp3/'.$item['fm'])) { mkdir($_SERVER['DOCUMENT_ROOT'].'/mp3/'.$item['fm'],0755); } //   ,        //        if ($i+1<10) { $number = '0'.$i+1; } else { $number = $i+1; } //  getSongByTitle($fullsongname,$_SERVER['DOCUMENT_ROOT'].'/mp3/'.$item['fm'].'/',$number); //      $filesarray[] = $_SERVER['DOCUMENT_ROOT'].'/mp3/'.$item['fm'].'/' . $number . ' ' . $fullsongname . '.mp3'; echo " \n"; } if (!is_dir($_SERVER['DOCUMENT_ROOT'].'/zip')) { mkdir($_SERVER['DOCUMENT_ROOT'].'/zip',0755); } //      create_zip($filesarray,$_SERVER['DOCUMENT_ROOT'].'/mp3/zip/'.$item['fm'].'.zip'); //   ,     PHP  seg.fault $html->clear(); unset($html); } 


Selection of logos indicating the Moscow frequency of stations + beautiful png

')

Source: https://habr.com/ru/post/125084/


All Articles