Skip to content

Commit

Permalink
Merge pull request #2 from rbrtjns90/rbrtjns90-patch-3
Browse files Browse the repository at this point in the history
Update utils.py
  • Loading branch information
rbrtjns90 authored Mar 11, 2023
2 parents 2e9e62a + a24b494 commit 42746ca
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@
import requests, json
from colorama import init , Style,Fore
import http.client
import string
import random
init()
def create_url():
N = 7
res = ''.join(random.choices(string.ascii_uppercase +
string.digits, k=N))
return str(res)

class utils:
# Functions 1to get is right
Expand Down Expand Up @@ -145,14 +152,67 @@ def checkUrl(url):
c.close()
return False

# Goo.gl shortener service
# Removed Goo.gl shortener service
@staticmethod
def gShortener(api_key, p_url):
def _gShortener(api_key, p_url):
url = "https://www.googleapis.com/urlshortener/v1/url?key=" + api_key
payload = '{"longUrl":"' + p_url + '"}'
headers = {'content-type': 'application/json'}
r = requests.post(url, data=payload, headers=headers)
print(api_key, p_url, r.content)
return r

#Short.io shortener service
@staticmethod
def gShortener(api_key, p_url):

url = "https://api.short.io/links"

payload = {
"domain": "83t0.short.gy",
"originalURL": p_url,
"path": create_url(),
}
headers = {
"accept": "application/json",
"content-type": "application/json",
"Authorization": api_key,
}

response = requests.post(url, json=payload, headers=headers)
'''Google Shortener Example API
response = {"kind": "urlshortener#url",
"id": "http://goo.gl/fbsS",
"longUrl": "https://www.rebrandly.com/",
"status": "OK"
}'''
'''Short Example API
originalURL': 'http://9e3a-205-204-27-195.ngrok.io/www.instagram.com/',
'DomainId': 652469,
'archived': False,
'lcpath': 'fvfbqtj',
'source': 'api',
'cloaking': False,
'createdAt': '2023-03-11T08:19:19.685Z',
'updatedAt': '2023-03-11T08:19:19.685Z',
'OwnerId': 806676,
'tags': [],
'path': 'FVFBQTJ',
'idString': 'lnk_2JJH_9dCPkor7AOX',
'shortURL': 'https://83t0.short.gy/FVFBQTJ',
'secureShortURL': 'https://83t0.short.gy/FVFBQTJ',
'duplicate': False}'''
# Convert the Short.io Response to match the Google Response
parse_response = json.loads(str(response.content)[2:][:-1])
fixed_response = {}
fixed_response['longUrl'] = parse_response['originalURL']
fixed_response['id'] = parse_response['shortURL']
fixed_response['kind'] = parse_response['shortURL']
fixed_response['status'] = 'OK'

return json.dumps(fixed_response)



# Autocompletion
@staticmethod
Expand Down

0 comments on commit 42746ca

Please sign in to comment.