forked from johnwarne/reddit-top-rss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-and-filter.php
97 lines (82 loc) · 4.04 KB
/
sort-and-filter.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
94
95
96
97
<?php
// Functions
include "functions.php";
// Get requested subreddit
// If none is specified, set a default
if(isset($_GET["subreddit"])) {
$subreddit = strip_tags(trim($_GET["subreddit"]));
} else {
$subreddit = DEFAULT_SUBREDDIT;
}
// Set default threshold score
$thresholdScore = 0;
// Average posts per day
if(isset($_GET["averagePostsPerDay"]) && $_GET["averagePostsPerDay"]) {
$thresholdPostsPerDay = $_GET["averagePostsPerDay"];
$jsonFeedFileTopMonth = getFile("https://www.reddit.com/r/" . $subreddit . "/top/.json?t=month&limit=1", "redditJSON", "cache/reddit/$subreddit-top-?t=month&limit=1.json", 60 * 5);
$jsonFeedFileTopMonthParsed = json_decode($jsonFeedFileTopMonth, true);
$jsonFeedFileTopMonthItemsCount = count($jsonFeedFileTopMonthParsed["data"]["children"][0]["data"]) ?: 0;
$jsonFeedFileTopMonthScore = getFile($jsonFeedFileTopMonthParsed["data"]["children"][0]["data"]["score"], "redditScore", "cache/scores/$subreddit-TopMonthScore", 60 * 60);
if($thresholdPostsPerDay <= 3) {
$thresholdPostsPerDaySize = 'low';
$jsonFeedFileTop = getFile("https://www.reddit.com/r/" . $subreddit . "/top/.json?t=month&limit=" . $thresholdPostsPerDay * 30, "redditJSON", "cache/reddit/$subreddit-top-?t=month&limit=" . $thresholdPostsPerDay * 30 . ".json", 60 * 5);
} elseif($thresholdPostsPerDay <= 14) {
$thresholdPostsPerDaySize = 'medium';
$jsonFeedFileTop = getFile("https://www.reddit.com/r/" . $subreddit . "/top/.json?t=week&limit=" . $thresholdPostsPerDay * 7, "redditJSON", "cache/reddit/$subreddit-top-?t=week&limit=" . $thresholdPostsPerDay * 7 . ".json", 60 * 5);
} else {
$thresholdPostsPerDaySize = 'high';
$jsonFeedFileTop = getFile("https://www.reddit.com/r/" . $subreddit . "/top/.json?t=day&limit=" . $thresholdPostsPerDay, "redditJSON", "cache/reddit/$subreddit-top-?t=day&limit=" . $thresholdPostsPerDay . ".json", 60 * 5);
}
$jsonFeedFileTopParsed = json_decode($jsonFeedFileTop, true);
$jsonFeedFileTopItems = $jsonFeedFileTopParsed["data"]["children"];
usort($jsonFeedFileTopItems, "sortByScoreDesc");
$jsonFeedFileTopParsedScore = $jsonFeedFileTopParsed["data"]["children"][0]["data"]["score"];
$scoreMultiplier = getFile($jsonFeedFileTopMonthScore / $jsonFeedFileTopParsedScore, "redditScore", "cache/scores/$subreddit-averagePostsPerDay-$thresholdPostsPerDaySize-multiplier", 60 * 60);
if($jsonFeedFileTopMonthItemsCount) {
$thresholdScore = round(array_values(array_slice($jsonFeedFileTopItems, -1))[0]["data"]["score"] * $scoreMultiplier);
} else {
$thresholdScore = 1000000;
}
}
// Threshold percentage
if(isset($_GET["threshold"]) && $_GET["threshold"]) {
$thresholdPercentage = $_GET["threshold"];
$totalFeedScore = 0;
$jsonFeedFileTopMonth = getFile("https://www.reddit.com/r/" . $subreddit . "/top/.json?t=month&limit=100", "redditJSON", "cache/reddit/$subreddit-top-?t=month&limit=100.json", 60 * 5);
$jsonFeedFileTopMonthParsed = json_decode($jsonFeedFileTopMonth, true);
$jsonFeedFileTopMonthItemsCount = count($jsonFeedFileTopMonthParsed["data"]["children"]) ?: 0;
$jsonFeedFileTopMonthItems = $jsonFeedFileTopMonthParsed["data"]["children"];
foreach ($jsonFeedFileTopMonthItems as $feedItem) {
$totalFeedScore += $feedItem["data"]["score"];
}
$averageFeedScore = getFile($totalFeedScore/count($jsonFeedFileTopMonthItems), "redditScore", "cache/scores/$subreddit-averageFeedScore", 60 * 60);
if($jsonFeedFileTopMonthItemsCount) {
$thresholdScore = floor($averageFeedScore * $thresholdPercentage/$jsonFeedFileTopMonthItemsCount);
} else {
$thresholdScore = 1000000;
}
}
// Threshold score
if(isset($_GET["score"]) && $_GET["score"]) {
$thresholdScore = $_GET["score"];
}
// Sort by Created Date
function sortByCreatedDate($a, $b) {
if ($a["data"]["created_utc"] > $b["data"]["created_utc"]) {
return 1;
} else if ($a["data"]["created_utc"] < $b["data"]["created_utc"]) {
return -1;
} else {
return 0;
}
}
// Sort by Score
function sortByScoreDesc($a, $b) {
if ($a["data"]["score"] < $b["data"]["score"]) {
return 1;
} else if ($a["data"]["score"] > $b["data"]["score"]) {
return -1;
} else {
return 0;
}
}