forked from johnwarne/reddit-top-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
postlist.php
93 lines (76 loc) · 3.69 KB
/
postlist.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
if (file_exists("config.php")) {
require_once "config.php";
} else {
require_once "config-default.php";
}
// Sort and filter
include "sort-and-filter.php";
// Check if requested subreddit is valid
$requestedSubreddit = "https://www.reddit.com/r/" . $subreddit . "/top/.json";
function get_http_response_code($requestedSubreddit) {
$reqOptions = array('http' => array('user_agent' => REDDIT_USER_AGENT));
$reqContext = stream_context_create($options);
$headers = get_headers($requestedSubreddit, false, $reqContext);
return substr($headers[0], 9, 3);
}
if(get_http_response_code($requestedSubreddit) != "200"){
echo "<div class='alert alert-warning' role='alert'>/r/" . $subreddit . " is not a valid subreddit</div>";
} else {
// Description text
$postListDescription = "<p>Hot posts in <strong>/r/";
$postListDescription .= $subreddit;
$postListDescription .= "</strong>";
if($thresholdScore) {
$postListDescription .= " at or above a score of <strong>";
$postListDescription .= $thresholdScore;
$postListDescription .= "</strong>";
if(isset($_GET["threshold"]) && $_GET["threshold"]) {
$postListDescription .= " <em>(";
$postListDescription .= $thresholdPercentage;
$postListDescription .= "% of monthly top posts' average score)</em>";
}
if(isset($_GET["averagePostsPerDay"]) && $_GET["averagePostsPerDay"]) {
$postListDescription .= " <em>(giving a rough average of ";
$postListDescription .= $_GET["averagePostsPerDay"];
$postListDescription .= " posts per day)</em>";
}
}
$postListDescription .= "</p>";
// Get subreddit hot posts
$jsonFeedFile = getFile("https://www.reddit.com/r/" . $subreddit . ".json", "redditJSON", "cache/reddit/$subreddit.json", 60 * 5);
$jsonFeedFileParsed = json_decode($jsonFeedFile, true);
$items = $jsonFeedFileParsed["data"]["children"];
usort($items, "sortByCreatedDate");
// List posts
$postListText = "";
foreach ($items as $item) {
// Only show posts at or above score threshold
if ($item["data"]["score"] >= $thresholdScore) {
$itemDataUrl = "https://www.reddit.com" . $item["data"]["permalink"];
$itemDataUrl = preg_replace("/&?utm_(.*?)\=[^&]+/", "", $itemDataUrl);
$thumbnailURL = "https://www.redditstatic.com/mweb2x/favicon/76x76.png";
if(!in_array($item["data"]["thumbnail"], ['default', 'nsfw', 'self', 'spoiler'])) {
$thumbnailURL = $item["data"]["thumbnail"];
}
$score = $item["data"]["score"];
if($score > 1000) {
$score = number_format($score / 1000, 1) . "k";
if(strpos($score, ".0") !== FALSE) {
$score = str_replace(".0", "", $score);
}
}
$postListText .= "<a class='media' href='$itemDataUrl' target='_blank'><div class='thumbnail'><img class='d-flex mr-3' data-src='$thumbnailURL'></div><div class='media-body'><h5 class='mt-0'><span class='badge badge-secondary'>$score</span><span class='hide' hidden>" . date("r", $item["data"]["created_utc"]) . "</span>" . $item["data"]["title"] . "</h5><time datetime='" . date("r", $item["data"]["created_utc"]) . "'>" . date("F jS", $item["data"]["created_utc"]) . " at " . date("g:i a", $item["data"]["created_utc"]) . "</time></div></a>";
}
}
// Echo post list or no posts message
if($postListText) {
echo $postListDescription . $postListText;
} else {
echo "<div class='alert alert-warning' role='alert'>No hot posts in <strong>/r/" . $subreddit . "</strong> match the filter above.</div>";
}
// Echo cache size
if(CACHE_REDDIT_JSON == true || CACHE_MERCURY_CONTENT == true || CACHE_RSS_FEEDS == true) {
echo "<div class='cache-size d-none' data-cache-size='" . sizeFormat(directorySize("cache")) . "'></div>";
}
}