Skip to content

Commit

Permalink
🎉 Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sergius02 committed Dec 8, 2020
0 parents commit a33f061
Show file tree
Hide file tree
Showing 11 changed files with 901 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
__pycache__
.cache
.mypy_cache/
.idea/
674 changes: 674 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<p align="center">
<img src="screenshots/icon.png" alt="Icon" />
</p>

<h1 align="center">ShortIT</h1>

----------

This is an extension for [ULauncher](https://ulauncher.io/), it helps you to short your URLs with multiple services

| ![alt](screenshots/shortit.gif)|
|--------------------------------|

## Dependencies

This extensions needs `requests` and `validator-collection`, install it with pip3

* `pip3 install requests`
* `pip3 install validator-collection`

## Options

You have a list of shorteners using the option `?`

You can get your URL to your clipboard, just select and press enter!

### Services

| Service name | Service link |
|--------------|----------------------------|
| Bitly | [Link](https://bitly.com/) |
| T.ly | [Link](https://t.ly/) |

More services will be added in the future

----------

If you like my work you can

<a href="https://www.buymeacoffee.com/sergius02" target="_blank"><img src="https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png" alt="Buy Me A Coffee" style="height: 41px !important;width: 174px !important;box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;-webkit-box-shadow: 0px 3px 2px 0px rgba(190, 190, 190, 0.5) !important;" ></a>
Binary file added images/bitly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/tly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
150 changes: 150 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
import logging

import requests
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.client.Extension import Extension
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.SetUserQueryAction import SetUserQueryAction
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from validator_collection import checkers

logger = logging.getLogger(__name__)


class ShortIT(Extension):

def __init__(self):
super(ShortIT, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())

def short_url(self, url: str, service: str):
if checkers.is_url(url):
return self.short(url, service)
else:
return self.url_error()

def short(self, url: str, service: str):
if service == "tly":
tly_api_key = self.preferences.get("shortit_tly_apikey")
if tly_api_key != "":
return self.short_with_tly(tly_api_key, url)
else:
return self.apikey_error("Tly")
elif service == "bitly":
bitly_api_key = self.preferences.get("shortit_bitly_apikey")
if bitly_api_key != "":
return self.short_with_bitly(bitly_api_key, url)
else:
return self.apikey_error("Bitly")
else:
return self.unknownservice_error()

def short_with_tly(self, api_key, url):
logger.debug("Short with Tly")
request_params = {
'api_token': api_key,
'long_url': url
}
request = requests.post("https://t.ly/api/v1/link/shorten", params=request_params)

return [
ExtensionResultItem(
icon='images/tly.png',
name=request.json()["short_url"],
description='Made with Tly',
on_enter=CopyToClipboardAction(request.json()["short_url"])
)
]

def short_with_bitly(self, api_key, url):
logger.debug("Short with Bitly")
data = "{\"long_url\":\"" + url + "\"}"
headers = {'Authorization': api_key, 'Content-Type': 'application/json'}
request = requests.post("https://api-ssl.bitly.com/v4/shorten", headers=headers, data=data)

return [
ExtensionResultItem(
icon='images/bitly.png',
name=request.json()["link"],
description='Made with Bitly',
on_enter=CopyToClipboardAction(request.json()["link"])
)
]

def url_error(self):
return self.simple_error_message("Something went wrong...", "Incorrent URL format.")

def apikey_error(self, service: str):
return self.simple_error_message("Empty API key", f"No API key for {service}. Please, check preferences.")

def emptyurl_error(self):
return self.simple_error_message("Now write the URL to short", "For example: https://my-awesome.web.com")

def simple_error_message(self, title: str, message: str):
return [
ExtensionResultItem(
icon='images/icon.png',
name=title,
description=message
)
]

def emptyservice_error(self):
return self.service_error("Choose the short service")

def unknownservice_error(self):
return self.service_error("Unknown service")

def service_error(self, title: str):
keyword = self.preferences.get("shortit_keyword")
return [
ExtensionResultItem(
icon='images/icon.png',
name=title,
description=f"Check available service typing {keyword} ? or press enter",
on_enter=SetUserQueryAction(f"{keyword} ?")
)
]

def return_help(self):
keyword = self.preferences.get("shortit_keyword")
return [
ExtensionResultItem(
icon='images/tly.png',
name="tly",
description="Short using t.ly",
on_enter=SetUserQueryAction(f"{keyword} tly ")
),
ExtensionResultItem(
icon='images/bitly.png',
name="bitly",
description="Short using bitly",
on_enter=SetUserQueryAction(f"{keyword} bitly ")
)
]


class KeywordQueryEventListener(EventListener):

def on_event(self, event, extension):
items = []

# Split command and options
argument: str = event.get_argument() or ""
if argument != "":
service, *options = argument.split()

if service == "?":
return RenderResultListAction(extension.return_help())
elif len(options) > 0:
return RenderResultListAction(extension.short_url(options[0], service))
else:
return RenderResultListAction(extension.emptyurl_error())
else:
return RenderResultListAction(extension.emptyservice_error())


if __name__ == '__main__':
ShortIT().run()
30 changes: 30 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"required_api_version": "^2.0.0",
"name": "ShortIT",
"description": "ShortIT",
"developer_name": "Sergio Fernández Celorio",
"icon": "images/icon.png",
"options": {
"query_debounce": 2
},
"preferences": [
{
"id": "shortit_keyword",
"type": "keyword",
"name": "ShortIT",
"default_value": "short"
},
{
"id": "shortit_tly_apikey",
"type": "input",
"name": "Tly API key",
"description": "Your API key for Tly, you can generate one in https://t.ly"
},
{
"id": "shortit_bitly_apikey",
"type": "input",
"name": "Bitly API key",
"description": "Your API key for Bitly, you can generate one in https://app.bitly.com"
}
]
}
Binary file added screenshots/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added screenshots/shortit.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions versions.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
{ "required_api_version": "^2.0.0", "commit": "master" }
]

0 comments on commit a33f061

Please sign in to comment.