Module code thus far

This commit is contained in:
ngoomie 2023-07-02 10:42:32 -06:00
parent 705d8b315b
commit 81c9e1fde6
2 changed files with 66 additions and 0 deletions

View File

@ -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

40
lib/StatusCafe.pm Normal file
View File

@ -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;