-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 23b57f3
Showing
8 changed files
with
422 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
Oops, something went wrong.