admin commited on
Commit
95b6e87
·
1 Parent(s): 2a70c6f
Files changed (1) hide show
  1. src/modules/bitly-api.min.py +17 -14
src/modules/bitly-api.min.py CHANGED
@@ -21,16 +21,18 @@ For more information, see
21
 
22
 
23
  BITLY_ACCESS_TOKEN = os.getenv('BITLY_ACCESS_TOKEN', None)
 
24
 
25
  class ShortenUrl:
26
- def __init__(self, bitly_access_token=None):
27
- self.bitly_access_token = bitly_access_token if bitly_access_token else BITLY_ACCESS_TOKEN
 
28
 
29
  def shorten(self, long_url):
30
  try:
31
- url = "https://api-ssl.bitly.com/v4/shorten"
32
  headers = {
33
- "Authorization": f"Bearer {self.bitly_access_token}",
34
  "Content-Type": "application/json"
35
  }
36
  data = {
@@ -42,16 +44,15 @@ class ShortenUrl:
42
  except requests.exceptions.RequestException as e:
43
  return None, {"error": str(e)}
44
 
45
- def generate_qr(self, shorten_url):
46
  try:
47
- qr_url = f"https://api-ssl.bitly.com/v4/bitlinks/{shorten_url}/qr"
48
  headers = {
49
- "Authorization": f"Bearer {self.bitly_access_token}",
50
- "Content-Type": "application/json",
51
  }
52
  params = {
53
  "customization": {
54
- "exclude_bitly_logo": True
55
  }
56
  }
57
  response = requests.get(qr_url, headers=headers, params=params)
@@ -87,8 +88,8 @@ class ShortenUrl:
87
  except Exception as e:
88
  return None, None, {"error": str(e)}
89
 
90
- def shorten_and_generate_qr(api_key, text, shorten, generate_qr):
91
- bitly = ShortenUrl(api_key)
92
  urls = re.findall(r'http[s]?://\S+', text)
93
  results = []
94
  ts, tmpd = Interface.get_tempdir()
@@ -114,7 +115,8 @@ def shorten_and_generate_qr(api_key, text, shorten, generate_qr):
114
 
115
  if generate_qr and url_to_process:
116
  if shorten_url:
117
- qr_html, qr_png, qr_json = bitly.generate_qr(url_to_process)
 
118
  else:
119
  qr_html, qr_png, qr_json = bitly.generate_qr_local(url_to_process)
120
 
@@ -215,7 +217,8 @@ with gr.Blocks() as demo:
215
  gr.Markdown(description)
216
  with gr.Row():
217
  with gr.Column():
218
- api_key = gr.Textbox(label="Bitly API Key", placeholder="API Key")
 
219
  url_input = gr.TextArea(label="Enter Text with URLs")
220
  with gr.Row():
221
  shorten_checkbox = gr.Checkbox(label="Shorten URL", value=True)
@@ -238,7 +241,7 @@ with gr.Blocks() as demo:
238
 
239
  submit_button.click(
240
  shorten_and_generate_qr,
241
- inputs=[api_key, url_input, shorten_checkbox, qr_checkbox],
242
  outputs=[preview_output, gallery_output, file_output, json_output]
243
  )
244
 
 
21
 
22
 
23
  BITLY_ACCESS_TOKEN = os.getenv('BITLY_ACCESS_TOKEN', None)
24
+ BITLY_API_URL = "https://api-ssl.bitly.com/v4"
25
 
26
  class ShortenUrl:
27
+ def __init__(self, access_token=None, api_url=None):
28
+ self.access_token = access_token if access_token else BITLY_ACCESS_TOKEN
29
+ self.api_url = api_url if api_url else BITLY_API_URL
30
 
31
  def shorten(self, long_url):
32
  try:
33
+ url = self.api_url + "/shorten"
34
  headers = {
35
+ "Authorization": f"Bearer {self.access_token}",
36
  "Content-Type": "application/json"
37
  }
38
  data = {
 
44
  except requests.exceptions.RequestException as e:
45
  return None, {"error": str(e)}
46
 
47
+ def generate_qr(self, shorten_url): # DEPRECATED
48
  try:
49
+ qr_url = self.api_url + f"/bitlinks/{shorten_url}/qr"
50
  headers = {
51
+ "Authorization": f"Bearer {self.access_token}",
 
52
  }
53
  params = {
54
  "customization": {
55
+ "exclude_bitly_logo": True # TODO
56
  }
57
  }
58
  response = requests.get(qr_url, headers=headers, params=params)
 
88
  except Exception as e:
89
  return None, None, {"error": str(e)}
90
 
91
+ def shorten_and_generate_qr(access_token, api_url, text, shorten, generate_qr):
92
+ bitly = ShortenUrl(access_token, api_url)
93
  urls = re.findall(r'http[s]?://\S+', text)
94
  results = []
95
  ts, tmpd = Interface.get_tempdir()
 
115
 
116
  if generate_qr and url_to_process:
117
  if shorten_url:
118
+ # qr_html, qr_png, qr_json = bitly.generate_qr(url_to_process)
119
+ qr_html, qr_png, qr_json = bitly.generate_qr_local(url_to_process)
120
  else:
121
  qr_html, qr_png, qr_json = bitly.generate_qr_local(url_to_process)
122
 
 
217
  gr.Markdown(description)
218
  with gr.Row():
219
  with gr.Column():
220
+ access_token = gr.Textbox(label="Access Token", placeholder="***")
221
+ api_url = gr.Textbox(label="API base URL", placeholder="https://")
222
  url_input = gr.TextArea(label="Enter Text with URLs")
223
  with gr.Row():
224
  shorten_checkbox = gr.Checkbox(label="Shorten URL", value=True)
 
241
 
242
  submit_button.click(
243
  shorten_and_generate_qr,
244
+ inputs=[access_token, api_url, url_input, shorten_checkbox, qr_checkbox],
245
  outputs=[preview_output, gallery_output, file_output, json_output]
246
  )
247