Skip to content

Commit

Permalink
Inital commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stroebjo committed May 1, 2018
0 parents commit 23b57f3
Show file tree
Hide file tree
Showing 8 changed files with 422 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Cleaner/Amazon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

class Amazon implements URLCleaner
{
public function isApplicable($host)
{
return preg_match('/(www\.)?amazon\.([a-z]{2,3})/i', $host);
}

public function getShortURL($url_parts)
{
// get ASIN from the path segement
if (preg_match('/(dp|gp\/product)\/([a-z]+\/)?([a-z0-9]{10})/i', $url_parts['path'], $m)) {
$shorter_url = $url_parts['scheme'].'://'.$url_parts['host'].'/dp/'.$m[3];
return $shorter_url;
}

return null;
}

}
24 changes: 24 additions & 0 deletions Cleaner/Heise.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class Heise implements URLCleaner
{

public function isApplicable($host)
{
return preg_match('/(www\.)?heise\.de/i', $host);
}

public function getShortURL($url_parts)
{
// get ASIN from the path segement
if (preg_match('#-([0-9]+)\.html$#i', $url_parts['path'], $m)) {
$shorter_url = 'https://heise.de/-'.$m[1];
return $shorter_url;
}

return null;

}

}

23 changes: 23 additions & 0 deletions Cleaner/Reddit.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

class Reddit implements URLCleaner
{

public function isApplicable($host)
{
return preg_match('/(www\.)?reddit\.com/i', $host);
}

public function getShortURL($url_parts)
{
// get ASIN from the path segement
if (preg_match('#r/([a-z_]+)/comments/([a-z0-9]{6})#i', $url_parts['path'], $m)) {
$shorter_url = 'https://redd.it/'.$m[2];
return $shorter_url;
}

return null;

}

}
19 changes: 19 additions & 0 deletions Cleaner/YouTube.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class YouTube implements URLCleaner
{
public function isApplicable($host)
{
return preg_match('/(www\.)?youtube\.com/i', $host);
}

public function getShortURL($url_parts)
{
if (preg_match('#v=([a-zA-Z0-9_-]{11})#i', $url_parts['query'], $m)) {
$shorter_url = 'https://youtu.be/'.$m[1];
return $shorter_url;
}

return null;
}
}
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Alfred Clean URLs
=================

This worklow for [Alfred](https://www.alfredapp.com/) checks your current clipboard for an URL, if it finds one it will try to make the URL as short as possible, but without using third party URL shortening services. It will remove all unnecessary URL information that are not needed to get to the same page, or use an short URL provided by the same service.

[Download](https://github.com/stroebjo/alfred-cleanurls/releases)

Currently supported are:

- Amazon product detail pages
- Heise news articles
- reddit threads
- YoutTube videos

Add new URL
-----------

If you have a URL that is not currently supported, please create a pull request or open a new issue.


12 changes: 12 additions & 0 deletions URLCleaner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

interface URLCleaner {

/**
* Check's if this class can handle the given hostname.
*
*/
public function isApplicable($host);

public function getShortURL($url_parts);
}
67 changes: 67 additions & 0 deletions cleanurl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
error_reporting(E_ALL);

// get clipboard from STDIN (piped in Alfred Script Filter)
$url = fgets(STDIN);
$shorter_url = null;
$response = ['items' => []];


// check if message is an URL and if so get it's <title>
if (filter_var($url, FILTER_VALIDATE_URL)) {

// load all cleaner classes
require_once('URLCleaner.php');
foreach (glob("Cleaner/*.php") as $filename) {
require_once $filename;
}

$cleaners = array_filter(
get_declared_classes(),
function ($className) {
return in_array('URLCleaner', class_implements($className));
}
);

// check if we can handle the URL
$url_parts = parse_url($url);

foreach($cleaners as $c) {
$cleaner = new $c();

if ($cleaner->isApplicable($url_parts['host'])) {
$shorter_url = $cleaner->getShortURL($url_parts);
break;
}
}

if (!is_null($shorter_url)) {
$response['items'][] = [
'arg' => $shorter_url,
'title' => $shorter_url,
'subtitle' => sprintf('New URL is %s chars shorter.', ( strlen($url) - strlen($shorter_url) )),
'text' => [
'copy' => $shorter_url,
]
];
} else {
$response['items'][] = [
'arg' => 'new_url',
'title' => 'Sorry, I don\'t know this URL yet.',
'subtitle' => 'Hit enter for infos on how to add a new URL.',
];
}

} else {

// clipboard doesnt contain a valid URL.
$response['items'][] = [
'arg' => '',
'title' => 'Sorry, in your clipboard is no URL',
];

}

// Report back to Alfred
echo json_encode($response);

Loading

0 comments on commit 23b57f3

Please sign in to comment.