From fb3af995a68598cf1ca05498fea42a5a43a13bed Mon Sep 17 00:00:00 2001 From: Aaron Parecki Date: Thu, 5 Dec 2019 15:18:23 -0800 Subject: [PATCH] return rel-canonical if present --- lib/XRay/Formats/HTML.php | 12 + tests/ParseTest.php | 12 + tests/data/source.example.com/rel-canonical | 881 ++++++++++++++++++++ 3 files changed, 905 insertions(+) create mode 100644 tests/data/source.example.com/rel-canonical diff --git a/lib/XRay/Formats/HTML.php b/lib/XRay/Formats/HTML.php index 430bb94..5846266 100644 --- a/lib/XRay/Formats/HTML.php +++ b/lib/XRay/Formats/HTML.php @@ -60,6 +60,11 @@ class HTML extends Format { $mf2 = \mf2\Parse($html, $url); + $canonical = false; + + if(isset($mf2['rels']['canonical'][0])) + $canonical = $mf2['rels']['canonical'][0]; + // Check for a rel=alternate link to a Microformats JSON representation, and use that instead if(isset($mf2['rel-urls'])) { $alternates = [ @@ -141,6 +146,13 @@ class HTML extends Format { $result['source-format'] = 'mf2+html'; } } + + if($canonical) { + $result['data']['rels'] = [ + 'canonical' => $canonical, + ]; + } + return $result; } diff --git a/tests/ParseTest.php b/tests/ParseTest.php index 2d94a6a..f64c017 100644 --- a/tests/ParseTest.php +++ b/tests/ParseTest.php @@ -1099,4 +1099,16 @@ class ParseTest extends PHPUnit_Framework_TestCase { $this->assertEquals('https://realize.be/', $data['data']['follow-of']); $this->assertEquals('follow', $data['data']['post-type']); } + + public function testRelCanonical() { + $url = 'http://source.example.com/rel-canonical'; + $response = $this->parse(['url' => $url]); + + $body = $response->getContent(); + $this->assertEquals(200, $response->getStatusCode()); + $data = json_decode($body, true); + + $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['url']); + $this->assertEquals('https://aaronparecki.com/2019/12/01/10/homeautomation', $data['data']['rels']['canonical']); + } } diff --git a/tests/data/source.example.com/rel-canonical b/tests/data/source.example.com/rel-canonical new file mode 100644 index 0000000..9f7fe5f --- /dev/null +++ b/tests/data/source.example.com/rel-canonical @@ -0,0 +1,881 @@ +HTTP/1.1 200 OK +Server: Apache +Date: Wed, 09 Dec 2015 03:29:14 GMT +Content-Type: text/html; charset=utf-8 +Connection: keep-alive + + + + + + + + + + + I finally started documenting all the home automation devices and apps I use, with the main goal of everything running locally! Here's a fun list to browse through if you're looking for some holiday shopping ideas! https://aaronparecki.com/home-automation/ + + + + + + + + + + + + + + + +
+

Micro.blog

+ + + + + + + +
+ + +
+ +
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ I finally started documenting all the home automation devices and apps I use, with the main goal of everything running locally! Here's a fun list to browse through if you're looking for some holiday shopping ideas!

https://aaronparecki.com/home-automation/ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + tiffany + +
+ +
+
+ tiffany +
+
+

@aaronpk yay! Glad I'm not the only one 😉

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + tiffany + +
+ +
+
+ tiffany +
+
+

@aaronpk I'm going to read it tonight if I can stay awake. I'm looking forward to seeing what you're doing with it!

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + tiffany + +
+ +
+
+ tiffany +
+
+

@aaronpk you're in an apartment as well? Sweet!

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @tiffany Yeah it definitely has its challenges for smart home automation! +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + danj + +
+ +
+
+ danj +
+
+

@aaronpk ooh, gonna give this a read! I’m just getting in to home automation. 🤓

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + tiffany + +
+ +
+
+ tiffany +
+
+

@aaronpk the stuff you've shared I'd have never thought of and are on my list to buy.

+ +

Thank you so, so much!

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + tiffany + +
+ +
+
+ tiffany +
+
+

@danj definitely a good read and plenty of recs too

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @tiffany awesome so glad to help! I look forward to hearing how it works out! +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + danj + +
+ +
+
+ danj +
+
+

@tiffany Yes! I’m happy Hue makes the list, because that’s probably what I’ll start with. 🤓

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + gerwitz + +
+ +
+
+ gerwitz +
+
+

@aaronpk thanks! I have similar priorities as you, so this has helped me make a few decisions.

+ +

You mention UniFi Video but not what your camera setup looks like?

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @gerwitz ah yeah I forgot to add cameras there! I'll add that section soon. The short version is I'm all in on UniFi video, I have a few of their cameras, and I installed their software in a VM. +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + gerwitz + +
+ +
+
+ gerwitz +
+
+

@aaronpk 👍 I have a Synology NAS and UniFi network so am…conflicted.

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @gerwitz conflicted? I also have a Synology NAS and UniFi network! +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + gerwitz + +
+ +
+
+ gerwitz +
+
+

@aaronpk between unifi’s software and synology’s, I mean. ¯_(ツ)_/¯

+ +

The NAS is my only “server” unless you count a CloudKey and Apple TV. I think it’s time for a HASS box, though.

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @gerwitz You could always run Home Assistant on the Synology NAS https://www.home-assistant.io/docs/installation/synology/ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + gerwitz + +
+ +
+
+ gerwitz +
+
+

@aaronpk I am! For experimenting. But the Z-wave USB stick is too fiddly.

+ +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ + +
+
+ + aaronpk + +
+ +
+
+ aaronpk +
+
+ @gerwitz yeah, the weak link for this seems to be USB hardware. Thankfully I haven't had any trouble since switching to the Intel NUC +
+
+ +
+ + +
|
+
Reply
+ +
+ + +
+
+ +
+
+ + + + + + + + + + + + + + + +