Skip to content

Commit

Permalink
[http] get rid of requests.utils
Browse files Browse the repository at this point in the history
  • Loading branch information
midichef committed Jun 19, 2023
1 parent cc92ba4 commit c0bb84f
Showing 1 changed file with 28 additions and 2 deletions.
30 changes: 28 additions & 2 deletions visidata/loaders/http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from visidata import Path, RepeatFile, vd, VisiData
from visidata.loaders.tsv import splitter

Expand All @@ -20,7 +22,6 @@ def openurl_http(vd, path, filetype=None):
return openfunc(Path(schemes[-1]+'://'+path.given.split('://')[1]))

import urllib.request
import requests.utils
import urllib.error
import mimetypes

Expand Down Expand Up @@ -48,7 +49,7 @@ def _iter_lines(path=path, response=response, max_next=vd.options.http_max_next)
linkhdr = response.getheader('Link')
src = None
if linkhdr:
links = requests.utils.parse_header_links(linkhdr)
links = parse_header_links(linkhdr)
link_data = {}
for link in links:
key = link.get('rel') or link.get('url')
Expand All @@ -72,5 +73,30 @@ def _iter_lines(path=path, response=response, max_next=vd.options.http_max_next)

return vd.openSource(path, filetype=filetype)

def parse_header_links(value):
"""Return a list of parsed link headers proxies.
i.e. Link: <http:/.../front.jpeg>; rel=front; type="image/jpeg",<http://.../back.jpeg>; rel=back;type="image/jpeg"
:rtype: list
"""

links = []
replace_chars = ' \'"'
value = value.strip(replace_chars)
if not value:
return links
for val in re.split(', *<', value):
try:
url, params = val.split(';', 1)
except ValueError:
url, params = val, ''
link = {'url': url.strip('<> \'"')}
for param in params.split(';'):
try:
key, value = param.split('=')
except ValueError:
break
link[key.strip(replace_chars)] = value.strip(replace_chars)
links.append(link)
return links

VisiData.openurl_https = VisiData.openurl_http

0 comments on commit c0bb84f

Please sign in to comment.