Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Fuhrmann committed Mar 29, 2018
0 parents commit edd323a
Show file tree
Hide file tree
Showing 15 changed files with 581 additions and 0 deletions.
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# editorconfig.org
root = true

[*]
indent_style = space
indent_size = 4
end_of_line = crlf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.{md,rst,txt}]
indent_size = 2
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build
.idea
.vscode
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 <copyright holders>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
72 changes: 72 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<p align="center">
<img src="src/urlshortener.ico" width="100" height="100" />
</p>

# Keypirinha Plugin: URLShortener

This is URLShortener, a plugin for the
[Keypirinha](http://keypirinha.com) launcher.

Shorten your URLs by using multiple services directly through Keypirinha launcher.

![Demo](usage.gif)

## Download
https://github.com/Fuhrmann/keypirinha-url-shortener/releases/latest


## Install

#### Managed
[@ueffel](https://github.com/ueffel) wrote [PackageControl](https://github.com/ueffel/Keypirinha-PackageControl), a package manager that eases the install of third-party packages.
It must be installed manually.

#### Manual
Once the `URLShortener.keypirinha-package` file is installed,
move it to the `InstalledPackage` folder located at:

* `Keypirinha\portable\Profile\InstalledPackages` in **Portable mode**
* **Or** `%APPDATA%\Keypirinha\InstalledPackages` in **Installed mode** (the
final path would look like
`C:\Users\%USERNAME%\AppData\Roaming\Keypirinha\InstalledPackages`)


## Usage

Open Keypirinha and type 'URL Shortener'. There are three catalog items implemented by this plugin:

- `URL Shortener`: Use this to shorten a URL using the default service specified in the configuration. Select this item, paste or type the URL you want to shorten and wait for the output.
- `URL Shortener: History`: Navigate through list of your shortened URLs. The history can be disabled in configuration. Once this item is selected the history will appear as suggestions.
- `URL Shortener: Clear history`: Wipe the history of shortened URLs.

## Configuration

```
# Here you can specify the main service used to shorten the url
# Accepted values: google, tinyurl, isgood, bitly
# * Default: google
main_service = google
# Enabling history will allow you to see all your shortened urls
# * Default: yes
enable_history=yes
```

## Suported services
- **Google (default, http://goo.lg)** - This plugin already ships with a Google URL Shortener API key. This is my own personal key. All short URLs are public. You are not required, but you can create your own API KEY if for some reason this service stop working.
- **TinyURL (http://tinyurl.com)** - No further configuration necessary.
- **is.gd (http://is.gd)** - No further configuration necessary.
- **Bitly (http://bit.ly)** - An API key is required. Generate an API KEY on [bit.ly's website](https://bitly.com/a/oauth_apps).

## Change Log
### v1.0
* Released

## License

This package is distributed under the terms of the MIT license.


## Credits

Icon by [paomedia](https://www.iconfinder.com/paomedia)
57 changes: 57 additions & 0 deletions make.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
@echo off
setlocal

set PACKAGE_NAME=URLShortener
set INSTALL_DIR=%APPDATA%\Keypirinha\InstalledPackages

if "%1"=="" goto help
if "%1"=="-h" goto help
if "%1"=="--help" goto help
if "%1"=="help" (
:help
echo Usage:
echo make help
echo make clean
echo make build
echo make install
echo make py [python_args]
goto end
)

if "%BUILD_DIR%"=="" set BUILD_DIR=%~dp0build
if "%KEYPIRINHA_SDK%"=="" (
echo ERROR: Keypirinha SDK environment not setup.
echo Run SDK's "kpenv" script and try again.
exit /b 1
)

if "%1"=="clean" (
if exist "%BUILD_DIR%" rmdir /s /q "%BUILD_DIR%"
goto end
)

if "%1"=="build" (
if not exist "%BUILD_DIR%" mkdir "%BUILD_DIR%"
pushd "%~dp0"
call "%KEYPIRINHA_SDK%\cmd\kparch" ^
"%BUILD_DIR%\%PACKAGE_NAME%.keypirinha-package" ^
-r LICENSE* README* src
popd
goto end
)

if "%1"=="install" (
echo TODO: ensure the INSTALL_DIR variable declared at the top of this
echo script complies to your configuration and remove this message
exit /1

copy /Y "%BUILD_DIR%\*.keypirinha-package" "%INSTALL_DIR%\"
goto end
)

if "%1"=="py" (
call "%KEYPIRINHA_SDK%\cmd\kpy" %2 %3 %4 %5 %6 %7 %8 %9
goto end
)

:end
68 changes: 68 additions & 0 deletions src/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import datetime
import json
import os
import urllib.parse
import uuid


class History:
HISTORY_FILENAME = 'history.json'

def __init__(self, cache_path):
self.cache_path = cache_path

def add(self, long_url, short_url, service_name):
history = self.get_history_file()
with open(history, 'r+') as f:
try:
data = json.load(f)
except Exception:
data = {"items": []}

history_item = {
'id': str(uuid.uuid4()),
'longUrl': urllib.parse.quote_plus(long_url),
'shortUrl': short_url,
'service': service_name,
'date': datetime.datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%S%z')
}
data["items"].insert(0, history_item)
f.seek(0)
json.dump(data, f)
f.truncate()

def read(self):
history_file = self.get_history_file()
with open(history_file, 'r') as file:
try:
history = json.load(file)
if len(history['items']) == 0:
history = None
except Exception:
history = None

if history is None:
return False

return history

def remove(self, history_id):
with open(self.get_history_file(), 'r+') as f:
data = json.load(f)

new_data = list(filter(lambda item: item['id'] != history_id, data['items']))
new_data = {"items": new_data}

f.seek(0)
json.dump(new_data, f)
f.truncate()

def clear(self):
with open(self.get_history_file(), 'r+') as f:
new_data = {"items": []}
f.seek(0)
json.dump(new_data, f)
f.truncate()

def get_history_file(self):
return os.path.join(self.cache_path, self.HISTORY_FILENAME)
16 changes: 16 additions & 0 deletions src/services/bitly.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json

from .service import Service


class Bitly(Service):

def shorten(self, url):
response = self.get_request(url)
data = json.loads(response)
url_shortened = data['data']['url']

return url_shortened

def formatted_api_url(self):
return "{}{}{}".format(self.api_url, self.api_key, "&longUrl=")
12 changes: 12 additions & 0 deletions src/services/goodis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import json

from .service import Service


class GoodIs(Service):
def shorten(self, url):
response = self.get_request(url)
data = json.loads(response)
url_shortened = data['shorturl']

return url_shortened
16 changes: 16 additions & 0 deletions src/services/google.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import json

from .service import Service


class Google(Service):

def shorten(self, url):
post_data = json.dumps({'longUrl': url}).encode('utf-8')
data = self.post_request(post_data)
url_shortened = data['id']

return url_shortened

def formatted_api_url(self):
return "{}{}".format(self.api_url, self.api_key)
28 changes: 28 additions & 0 deletions src/services/service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import json
import urllib.parse
import urllib.request


class Service:
def __init__(self, api_url, api_key=None):
self.api_url = api_url
self.api_key = api_key

def get_request(self, url):
api_url = self.formatted_api_url()
req = urllib.request.Request("{}{}".format(api_url, urllib.parse.quote_plus(url)))

with urllib.request.urlopen(req) as response:
return response.read().decode('utf-8')

def post_request(self, post_data):
headers = {'Content-Type': 'application/json'}
req = urllib.request.Request(self.formatted_api_url(), post_data, headers)

with urllib.request.urlopen(req) as res:
data = json.load(res)

return data

def formatted_api_url(self):
return self.api_url
9 changes: 9 additions & 0 deletions src/services/tinyurl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .service import Service


class TinyURL(Service):

def shorten(self, url):
url_shortened = self.get_request(url)

return url_shortened
Binary file added src/urlshortener.ico
Binary file not shown.
35 changes: 35 additions & 0 deletions src/urlshortener.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# URLShortener Package configuration file
#

[main]
# Here you can specify the main service used to shorten the url
# Accepted values: google, tinyurl, isgood, bitly
# * Default: google
main_service=google

# Enabling history will allow you to see all your shortened urls
# * Default: yes
enable_history=yes

# The URL used by Google's service
# Generate your api key by accessing: https://console.developers.google.com/apis/credentials
# The API_KEY is required.
[google]
API_URL = https://www.googleapis.com/urlshortener/v1/url?key=
API_KEY = AIzaSyD_6wZK9xkE8JtJqzNGPgrLmWVqgqz4SEA

# The URL used by TinyURL's service
[tinyurl]
API_URL = http://tinyurl.com/api-create.php?url=

# The URL used by ISGOOD's service
[isgood]
API_URL = https://is.gd/create.php?format=json&url=

# The URL used by Bitly's service
# Generate your api key by accessing: https://bitly.com/a/oauth_apps
# The API_KEY is required.
[bitly]
API_URL = https://api-ssl.bitly.com/v3/shorten?access_token=
API_KEY =
Loading

0 comments on commit edd323a

Please sign in to comment.