DEV Community

rabbery001
rabbery001

Posted on

A Simple Reddit downloader with PHP

Image description
So, first step is to get source code of page from where we gonna extract video url, here is code how it would be done,

$uri = 'https://www.reddit.com/r/interestingasfuck/comments/130fgu1/interviewing_with_the_kkk_footage_might_disturbing/';
Enter fullscreen mode Exit fullscreen mode
$ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $uri);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3');
  $html = curl_exec($ch); // here in html page's source stored
  curl_close($ch);
Enter fullscreen mode Exit fullscreen mode

In the next we gonna find that video url by regex equation,

$video_regex = '/(https:\/\/v\.redd\.it\/[a-z0-9]+\/HLSPlaylist\.m3u8).*?/';
  preg_match($video_regex, $html, $videoUrl);
Enter fullscreen mode Exit fullscreen mode

and then from this equation we will get video id and print output

$url = $videoUrl[0];
    $regex1 = '/https:\/\/v\.redd\.it\/(\w+)\/HLSPlaylist\.m3u8/';
    preg_match($regex1, $url, $video);
    $video_id = $video[1];
     echo '<video controls src="https://v.redd.it/' . $video_id . '/DASH_720.mp4"  type="video/mp4" width="320"></video>';
Enter fullscreen mode Exit fullscreen mode

here is final output with GUI https://redditdownloader.org/

Top comments (1)

Collapse
 
spoofa1 profile image
mr spoofa

hello can you provide me the full code?