From 81c9e1fde60df1b85661d2a8b93098b5ee95e350 Mon Sep 17 00:00:00 2001 From: ngoomie Date: Sun, 2 Jul 2023 10:42:32 -0600 Subject: [PATCH] Module code thus far --- README.md | 26 ++++++++++++++++++++++++++ lib/StatusCafe.pm | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/StatusCafe.pm diff --git a/README.md b/README.md index 590d5fc..5822135 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,28 @@ # StatusCafe.pm +Perl module to fetch content directly from [status.cafe](https;//status.cafe) +I wanted to create a custom widget on my Mojolicious site listing off my status from status.cafe, so I wrote this module to fetch the info by directly scraping the given user's page. There's not really any reason I did that over Atom feed parsing, however! + +This isn't a proper Perl module yet that can be put on the CPAN; for now what I've personally been doing is using it under the `Model` namespace in the Mojolicious project I mentioned above. I plan on turning it into a proper CPAN module at a later date. + +## How to use + +```Perl +use utf8; +use StatusCafe; + +my ($emoji, $timeAgo, $content) = statuscafe('m15o'); +``` + +NOTE: You might want to set up some form of caching so it doesn't need refetched every time the script is triggered + +## Dependencies + +- HTTP::Tiny (for page fetching) +- Mojo::DOM (for parsing the fetched page for necessary info) +- Mojo::Util (so Mojo::DOM doesn't throw a fit over emoji usage) + +## To-do + +- Add an optional flag to output emoji information as text instead of an actual emoji, since that's easier to handle +- As mentioned above, modify into a proper module to be uploaded to CPAN diff --git a/lib/StatusCafe.pm b/lib/StatusCafe.pm new file mode 100644 index 0000000..e447f91 --- /dev/null +++ b/lib/StatusCafe.pm @@ -0,0 +1,40 @@ +package StatusCafe; + +use utf8; +use strict; +use warnings; + +use HTTP::Tiny; +use Mojo::DOM; +use Mojo::Util; + +use Exporter qw(import); +our @EXPORT = qw(statuscafe); + +sub statuscafe { + # fetch DOM from status.cafe + my $_response = + HTTP::Tiny->new(keep_alive => 0)->get('https://status.cafe/users/' . $_[0]); + return "$_response->{status} $_response->{reason}" + unless $_response->{success}; + bless $_response, __PACKAGE__; + my $_dom = + Mojo::DOM->new->parse(Mojo::Util::decode('UTF-8', $_response->{content})); + + # get the relevant text from the latest status + my $_info = + $_dom->find('div.status-username')->first->all_text; + my $_content = + $_dom->find('p.status-content')->first->all_text; + + # now turn $_info into a form we can use more easily + my ($_emoji) = + $_info =~ + /[\p{Emoji}\p{EMod}\p{EComp}\p{EBase}\p{EPres}\p{Emoticons}\p{ExtPict}]/g; + my ($_ago) = + $_info =~ /((\d{1,4})\s([a-z\s]{1,}))$/g; + + return ($_emoji, $_ago, $_content); +} + +1; \ No newline at end of file