From 01bd81cf3f943b4aff4e79f93910ab764a873548 Mon Sep 17 00:00:00 2001 From: Christian Cleberg Date: Wed, 7 Jan 2026 20:18:27 -0600 Subject: MASSIVE UPDATE TO PYTHON STATIC SERVICE --- src/Controller/FeedController.php | 47 ------ src/Controller/RouteController.php | 170 ---------------------- src/Model/ApiService.php | 286 ------------------------------------- src/Model/CacheService.php | 10 -- src/View/BaseTemplate.php | 35 ----- 5 files changed, 548 deletions(-) delete mode 100644 src/Controller/FeedController.php delete mode 100644 src/Controller/RouteController.php delete mode 100644 src/Model/ApiService.php delete mode 100644 src/Model/CacheService.php delete mode 100644 src/View/BaseTemplate.php (limited to 'src') diff --git a/src/Controller/FeedController.php b/src/Controller/FeedController.php deleted file mode 100644 index 8bba37f..0000000 --- a/src/Controller/FeedController.php +++ /dev/null @@ -1,47 +0,0 @@ -canonical_url = $canonical_url; - $this->description = $description; - $this->title = $title; - $this->content = $content; - $this->current_year = date("Y"); - } - - /** - * Request template to be presented to the user - * - * @access public - * @author Christian Cleberg - */ - public function render(): void - { - include_once 'src/View/BaseTemplate.php'; - } -} diff --git a/src/Controller/RouteController.php b/src/Controller/RouteController.php deleted file mode 100644 index e3b2236..0000000 --- a/src/Controller/RouteController.php +++ /dev/null @@ -1,170 +0,0 @@ -request = $request; - } - - /** - * Route the user to the appropriate function, based on the URL - * - * @access public - * @return void No return type; send user to FeedController->render() or a 404 error - * @author Christian Cleberg - */ - public function routeUser(): void - { - include_once 'src/Model/ApiService.php'; - $path = ltrim($this->request, '/'); - $elements = explode('/', $path); - - switch (array_shift($elements)) { - case '': - case 'top': - $feed = new FeedController( - $GLOBALS['full_domain'], - 'The top stories from Hacker News, proxied by hn.', - 'hn', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/topstories.json?limitToFirst=10&orderBy="$key"' - ), - 'Top' - ) - ); - - $feed->render(); - break; - - case 'best': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/Best/', - 'The best stories from Hacker News, proxied by hn.', - 'hn ~ best', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/beststories.json?limitToFirst=10&orderBy="$key"' - ), - 'Best' - ) - ); - - $feed->render(); - break; - - case 'new': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/new/', - 'The newest stories from Hacker News, proxied by hn.', - 'hn ~ new', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/newstories.json?limitToFirst=10&orderBy="$key"' - ), - 'New' - ) - ); - - $feed->render(); - break; - - case 'ask': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/ask/', - 'The top asks from Hacker News, proxied by hn.', - 'hn ~ ask', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/askstories.json?limitToFirst=10&orderBy="$key"' - ), - 'Ask' - ) - ); - - $feed->render(); - break; - - case 'show': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/show/', - 'The latest showcases from Hacker News, proxied by hn.', - 'hn ~ show', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/showstories.json?limitToFirst=10&orderBy="$key"' - ), - 'Show' - ) - ); - - $feed->render(); - break; - - case 'job': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/job/', - 'The latest jobs from Hacker News, proxied by hn.', - 'hn ~ jobs', - ParseStories( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/jobstories.json?limitToFirst=10&orderBy="$key"' - ), - 'Job' - ) - ); - - $feed->render(); - break; - - case 'user': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/user/' . $elements[0], - 'The Hacker News profile for ' . $elements[0] . ', proxied by hn.', - 'hn ~ ' . $elements[0], - ParseUser( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/user/' . $elements[0] . '.json' - ), - 'User: ' . $elements[0] - ) - ); - - $feed->render(); - break; - - case 'item': - $feed = new FeedController( - $GLOBALS['full_domain'] . '/item/' . $elements[0], - 'Hacker News story ' . $elements[0] . ', proxied by hn.', - 'hn ~ ' . $elements[0], - ParseItem( - GetApiResults( - 'https://hacker-news.firebaseio.com/v0/item/' . $elements[0] . '.json' - ) - ) - ); - - $feed->render(); - break; - - default: - header('HTTP/1.1 404 Not Found'); - } - } -} \ No newline at end of file diff --git a/src/Model/ApiService.php b/src/Model/ApiService.php deleted file mode 100644 index dce65e5..0000000 --- a/src/Model/ApiService.php +++ /dev/null @@ -1,286 +0,0 @@ - - */ -function GetApiResults(string $api_url): mixed -{ - $response = file_get_contents($api_url); - return json_decode($response, true); -} - -/** - * Formats a given set of API results into an HTML section - * - * @access public - * @param mixed $api_results The decoded API results - * @param string $inline_title The

title to use in the HTML - * @return string $html_output The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ParseStories(mixed $api_results, string $inline_title): string -{ - if ($api_results == "null") { - return '

ERROR: Stories not found. API returned `null`.

'; - } else { - $html_output = '

' . $inline_title . '

'; - for ($i = 0; $i < count($api_results); $i++) { - $story_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results[$i] . '.json'); - $html_output .= ConstructStory($story_api_results); - } - - return $html_output; - } -} - -/** - *Extract a user's profile from Hacker News API and format in HTML - * - * @access public - * @param mixed $api_results The decoded API results - * @param string $inline_title The

title to use in the HTML - * @return string $html_output The formatted HTML result of stories from the API - * @author Christian Cleberg - */ -function ParseUser(mixed $api_results, string $inline_title): string -{ - if ($api_results == "null") { - return '

ERROR: User not found.

'; - } else { - // TODO: Create function to format $about using the following guidelines - // : https://news.ycombinator.com/formatdoc - // : hint: nl2br() will solve the first formatting requirement - $about = $api_results['about']; - $karma = $api_results['karma']; - $created = date('Y-m-d h:m:s', $api_results['created']); - - $html_output = << -

$inline_title

-

About: $about

-

Karma: $karma

-

Created:

-
-

Recently Submitted

- - EOT; - - $limit = (count($api_results['submitted']) > 10) ? 10 : count($api_results['submitted']); - if (count($api_results['submitted']) > 0) { - for ($i = 0; $i < $limit; $i++) { - $user_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['submitted'][$i] . '.json'); - $html_output .= GetItem($user_api_results); - } - } else { - $html_output .= '

User has no submissions.

'; - } - - return $html_output; - } -} - - -/** - * Formats one specific item requested by the user - * - * @access public - * @param mixed $api_results The decoded API results - * @param string $inline_title The

title to use in the HTML - * @return string $html_output The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ParseItem(mixed $api_results): string -{ - if ($api_results == "null") { - return '

ERROR: User not found.

'; - } else { - $html_output = ''; - - if (in_array($api_results['type'], array("job", "story", "poll", "pollopt"))) { - $html_output .= ConstructStoryDiscussion($api_results); - } else { - if (array_key_exists('parent', $api_results)) { - $parent_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['parent'] . '.json'); - $html_output .= GetItem($parent_api_results); - $html_output .= '
'; - } - - $html_output .= GetItem($api_results); - - if ($api_results['descendants'] != 0) { - $html_output .= '
'; - $child_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['kids'][0] . '.json'); - $html_output .= GetItem($child_api_results); - } - } - - return $html_output; - } -} - - -/** - * Formats one item from the API to HTML - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function GetItem(mixed $api_results): string -{ - $type = $api_results['type']; - - return match ($type) { - 'story', 'job' => ConstructStory($api_results), - 'comment' => ConstructComment($api_results), - 'poll' => ConstructPoll($api_results), - 'pollopt' => ConstructPollOpt($api_results), - default => '[ERROR] Item type not found: ' . $type, - }; -} - - -/** - * Creates a story HTML element - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ConstructStory(mixed $api_results): string -{ - $id = $api_results['id']; - $url = $api_results['url']; - $title = $api_results['title']; - $time = date('Y-m-d h:m:s', $api_results['time']); - $by = $api_results['by']; - $score = $api_results['score']; - if (array_key_exists('descendants', $api_results)) { - $descendants = $api_results['descendants']; - } else { - $descendants = 'No'; - } - - return << - $title -

- - by $by - | $score points - | $descendants comments -

- - EOT; -} - -/** - * Creates a story discussion page with comments - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ConstructStoryDiscussion(mixed $api_results): string -{ - $id = $api_results['id']; - $url = $api_results['url']; - $title = $api_results['title']; - $time = date('Y-m-d h:m:s', $api_results['time']); - $by = $api_results['by']; - $score = $api_results['score']; - $descendants = $api_results['descendants']; - if (array_key_exists('text', $api_results)) { - $text = $api_results['text']; - } else { - $text = ''; - } - - $html_output = << -

$title

-

- - by $by - | $score points - | $descendants comments -

-

$text

- - EOT; - - // TODO: Add support for more than just top-level kids (i.e., recursive). - if ($api_results['descendants'] != 0) { - $html_output .= << -

Comments

- EOT; - for ($i = 0; $i < count($api_results['kids']); $i++) { - $child_api_results = GetApiResults('https://hacker-news.firebaseio.com/v0/item/' . $api_results['kids'][$i] . '.json'); - $html_output .= GetItem($child_api_results); - } - $html_output .= ''; - } - - return $html_output; -} - -/** - * Creates a comment HTML element - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ConstructComment(mixed $api_results): string -{ - $time = date('Y-m-d h:m:s', $api_results['time']); - $text = $api_results['text']; - $by = $api_results['by']; - $parent = $api_results['parent']; - - return << -

$text

-

Submitted in response to: $parent

-

by $by

- - EOT; -} - -/** - * Creates a poll HTML element - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ConstructPoll(mixed $api_results): string -{ - return 'TODO'; -} - -/** - * Creates a poll-option HTML element - * - * @access public - * @param mixed $api_results The decoded API results - * @return string The formatted HTML result of stories from the API or the error message - * @author Christian Cleberg - */ -function ConstructPollOpt(mixed $api_results): string -{ - return 'TODO'; -} diff --git a/src/Model/CacheService.php b/src/Model/CacheService.php deleted file mode 100644 index ccb0db4..0000000 --- a/src/Model/CacheService.php +++ /dev/null @@ -1,10 +0,0 @@ - - */ -function CacheService() {} \ No newline at end of file diff --git a/src/View/BaseTemplate.php b/src/View/BaseTemplate.php deleted file mode 100644 index 1aa25ab..0000000 --- a/src/View/BaseTemplate.php +++ /dev/null @@ -1,35 +0,0 @@ - - - - - <?php echo $this->title; ?> - - - - - - - - - - -
- - content; ?> -
- - - - - - -- cgit v1.2.3