Skip to content

Commit

Permalink
Merge pull request #154 from DerDreschner/feature/use-encoding-from-w…
Browse files Browse the repository at this point in the history
…ebserver

Replace abandoned httplib2, use charset from web server and use mocked http requests
  • Loading branch information
DerDreschner authored Jan 17, 2025
2 parents 8434545 + 0570410 commit 4db8471
Show file tree
Hide file tree
Showing 6 changed files with 1,881 additions and 76 deletions.
35 changes: 17 additions & 18 deletions icalevents/icaldownload.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Downloads an iCal url or reads an iCal file.
"""

from httplib2 import Http
import urllib3
import logging


Expand Down Expand Up @@ -33,25 +33,15 @@ class ICalDownload:
Downloads or reads and decodes iCal sources.
"""

def __init__(self, http=None, encoding="utf-8"):
def __init__(self, http=None):
# Get logger
logger = logging.getLogger()

# default http connection to use
if http is None:
try:
http = Http(".cache")
except (PermissionError, OSError) as e:
# Cache disabled if no write permission in working directory
logger.warning(
(
"Caching is disabled due to a read-only working directory: {}"
).format(e)
)
http = Http()
http = urllib3.PoolManager()

self.http = http
self.encoding = encoding

def data_from_url(self, url, apple_fix=False):
"""
Expand All @@ -64,12 +54,19 @@ def data_from_url(self, url, apple_fix=False):
if apple_fix:
url = apple_url_fix(url)

_, content = self.http.request(url)
response = self.http.request("GET", url)

if not content:
if not response.data:
raise ConnectionError("Could not get data from %s!" % url)

return self.decode(content, apple_fix=apple_fix)
content_type = response.headers.get("content-type")

try:
encoding = content_type.split("charset=")[1]
except (AttributeError, IndexError):
encoding = "utf-8"

return self.decode(response.data, encoding, apple_fix=apple_fix)

def data_from_file(self, file, apple_fix=False):
"""
Expand All @@ -93,15 +90,17 @@ def data_from_string(self, string_content, apple_fix=False):

return self.decode(string_content, apple_fix=apple_fix)

def decode(self, content, apple_fix=False):
@staticmethod
def decode(content, encoding="utf-8", apple_fix=False):
"""
Decode content using the set charset.
:param content: content do decode
:param encoding: the used charset for decoding the content
:param apple_fix: fix Apple txdata bug
:return: decoded (and fixed) content
"""
content = content.decode(self.encoding)
content = content.decode(encoding)
content = content.replace("\r", "")

if apple_fix:
Expand Down
Loading

0 comments on commit 4db8471

Please sign in to comment.