Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update bitshortener.py #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
203 changes: 64 additions & 139 deletions bitshortener.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,154 +2,79 @@
import json
import requests


class BitShortener(Shortener):
"""Bit.ly Shortener Extended Implementation
"""Bit.ly Shortener Extended Implementation using a requests Session for efficiency.

Args:
api_key (str): bit.ly API key
Example:
>>> import pyshorteners
>>> s = pyshorteners.Shortener(api_key='YOUR_KEY')
>>> s.bitly.short('http://www.google.com')
'http://bit.ly/TEST'
>>> s.bitly.expand('https://bit.ly/TEST')
'http://www.google.com'
>>> s.bitly.total_clicks('https://bit.ly/TEST')
10
api_key (str): bit.ly API key.
"""

def _patch(self, url, data=None, json=None, params=None, headers=None):
"""Wrap a PATCH request with a url check.

def __init__(self, api_key):
super().__init__(api_key=api_key)
self.session = requests.Session() # Create a Session object.
self.session.headers.update({"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"}) # Set default headers.

def _patch(self, url, json=None):
"""Send a PATCH request.

Args:
url (str): URL shortener address.
Keyword Args:
data (dict, str): Form-encoded data, `Requests POST Data`_.
headers (dict): HTTP headers to add, `Requests Custom Headers`_.
json (dict): Python object to JSON encode for data, `Requests
POST Data`_.
params (dict): URL parameters, `Requests Parameters`_.
.. _Requests Custom Headers: http://requests.kennethreitz.org/en/master/user/quickstart/#custom-headers
.. _Requests Parameters: http://requests.kennethreitz.org/en/master/user/quickstart/#passing-parameters-in-urls
.. _Requests POST Data: http://requests.kennethreitz.org/en/master/user/quickstart/#more-complicated-post-requests
url (str): Endpoint URL (will be concatenated with base URL).
json (dict, optional): JSON data to send in the request.

Returns:
requests.Response: HTTP response.
requests.Response: The response object.
"""
url = self.bitly.clean_url(url)

response = requests.patch(
url,
data=data,
json=json,
# params=params,
headers=headers,
timeout=self.bitly.timeout,
verify=self.bitly.verify,
proxies=self.bitly.proxies,
# cert=self.bitly.cert,
)
return response

def update_link(self, **kwargs):
headers = {
"Authorization": f"Bearer {self.bitly.api_key}",
"Content-Type": "application/json"
}

id = "/bit.ly/3GB8YMP"

response = self.bitly._get(
'https://api-ssl.bitly.com/v4/bitlinks' + id,
headers=headers)

print(response, response.content)
data = response.content
print("type", type(data))
data = response.json()
print("type after", type(data))
del data["deeplinks"]
print(type(data['tags']), type(data['tags']))
print(data['tags'])

# updating the tags with another additional tag
# Does it accept duplicates?
# It doesn't - that's cool!
data['tags'].append('test')

response = self._patch(
'https://api-ssl.bitly.com/v4/bitlinks' + id,
json=data,
headers=headers)
print("--After update")
print(response, response.content)

def get_links(self, **kwargs):
"""get links for a default groupid and user
as Documented at https://dev.bitly.com/api-reference#getBitlinksByGroup
url = self.clean_url(url) # Assuming clean_url method exists and cleans the URL.
return self.session.patch(url, json=json) # Use session's patch method.

def update_link(self, bitlink_id, updates):
"""Update a bitlink with new data.

Args:
userinfo and groupid
Returns:
paginated bitlinks
bitlink_id (str): The unique identifier of the bitlink.
updates (dict): The updates to apply to the bitlink.
"""
groupid = self.user_info()["default_group_guid"]
bitlinks_url = f"{self.bitly.api_url}/groups/{groupid}/bitlinks"
headers = {"Authorization": f"Bearer {self.bitly.api_key}"}

params = (
('size', '100'),
('page', '1'),
#('keyword', 'learningToTeach'),
#('query', "python"),
# ('created_before', '1501027200'),
# ('created_after', '1501027200'),
# ('modified_after', '1501027200'),
('archived', 'both'),
('deeplinks', 'both'),
('domain_deeplinks', 'both'),
# ('campaign_guid', 'Ca1bcd2EFGh'),
# ('channel_guid', 'Ha1bc2DefGh'),
('custom_bitlink', 'both'),
('tags[0]', 'bitly'),
('tags[1]', 'api'),
# ('launchpad_ids[0]', 'M1234567890'),
# ('encoding_login[0]', 'chauncey'),
)

# response = requests.get(bitlinks_url, headers=headers, params=params)
print(f'bitlink_url: {bitlinks_url}')
response = self.bitly._get(
bitlinks_url, headers=headers, params=params)
if not response.ok:
raise BadAPIResponseException(response.content)

try:
data = response.json()
except json.decoder.JSONDecodeError:
raise BadAPIResponseException("API response could not be decoded")

return data

def user_info(self, **kwargs):
# return "ashok"
"""return or update info about a user by
calling the appropriate bitlyAPI endpoint.
Documented at https://dev.bitly.com/api-reference#getUser

url = f"https://api-ssl.bitly.com/v4/bitlinks/{bitlink_id}"
response = self._patch(url, json=updates)
if response.ok:
print("Update successful:", response.json())
else:
print("Update failed:", response.text)

def get_links(self, group_id):
"""Retrieve bitlinks for a specified group.

Args:
group_id (str): The unique identifier of the group.

Returns:
user information, including default_group_id
list: A list of bitlinks.
"""
#clicks_url = f"{self.api_url}/bitlinks/{url}/clicks"
user_url = f"{self.bitly.api_url}/user"
print(f'user_url: {user_url}')
headers = {"Authorization": f"Bearer {self.bitly.api_key}"}
response = self.bitly._get(user_url, headers=headers)
if not response.ok:
raise BadAPIResponseException(response.content)

try:
data = response.json()
except json.decoder.JSONDecodeError:
raise BadAPIResponseException("API response could not be decoded")

return data
url = f"https://api-ssl.bitly.com/v4/groups/{group_id}/bitlinks"
response = self.session.get(url)
if response.ok:
return response.json()['links']
else:
print("Failed to retrieve links:", response.text)
return []

def user_info(self):
"""Retrieve information about the user.

Returns:
dict: User information.
"""
url = "https://api-ssl.bitly.com/v4/user"
response = self.session.get(url)
if response.ok:
return response.json()
else:
print("Failed to retrieve user info:", response.text)
return {}

@staticmethod
def clean_url(url):
"""Placeholder method for cleaning URLs if necessary."""
# Implement any necessary URL cleaning here.
return url