Repository: sunpy/sunpy

Issue:
SunPy’s HEK module

I'm new in Sunpy and I am tring to learn how to use the HEK module in order to obtain a list of events (for example FL ).

When I compile I obtain 
"'utf-8' codec can't decode byte 0xc5 in position 10559: invalid continuation byte",

The code used is
1. from sunpy.net import hek
2. client = hek.HEKClient()  
3. tstart = '2011/08/09 07:23:56'
4.tend = '2011/08/09 12:40:29'
5.event_type = 'FL'
6.result = client.search(hek.attrs.Time(tstart,tend),hek.attrs.EventType(event_type)) 

System details are:

  - SunPy Version: 1.1.1
  - Astropy Version: 4.0
  - Python Version: 3.7.6
  - OS information: Windows 10 10.0.18362 (Intel64 Family 6 Model 58 Stepping 9, GenuineIntel)



Discussion:
It would seem that whatever is returned from the servers is unable to be turned from bytes into a string. Why, I am unsure at the moment.

Resolving patch:
diff --git a/sunpy/net/hek/hek.py b/sunpy/net/hek/hek.py
--- a/sunpy/net/hek/hek.py
+++ b/sunpy/net/hek/hek.py
@@ -1,15 +1,9 @@
-# -*- coding: utf-8 -*-
-# Author: Florian Mayer <florian.mayer@bitsrc.org>
-#
-# This module was developed with funding provided by
-# the ESA Summer of Code (2011).
-#
-# pylint: disable=C0103,R0903
-
-""" Facilities to interface with the HEK. """
+"""
+Facilities to interface with the Heliophysics Events Knowledgebase.
+"""
 
 import json
-import codecs
+
 import urllib
 from itertools import chain
 
@@ -19,14 +13,13 @@
 from sunpy.net import attr
 from sunpy.util import dict_keys_same, unique
 from sunpy.net.hek import attrs
-from sunpy.net.vso import attrs as v_attrs
 import sunpy.net._attrs as core_attrs
 from sunpy.util.xml import xml_to_dict
 
 
 __all__ = ['HEKClient']
 
-DEFAULT_URL = 'http://www.lmsal.com/hek/her'
+DEFAULT_URL = 'http://www.lmsal.com/hek/her?'
 
 
 def _freeze(obj):
@@ -61,14 +54,14 @@ def _download(self, data):
         """ Download all data, even if paginated. """
         page = 1
         results = []
-        reader = codecs.getreader("utf-8")
 
         while True:
             data['page'] = page
-            fd = urllib.request.urlopen(
-                self.url, urllib.parse.urlencode(data).encode('utf-8'))
+            fd = urllib.request.urlopen(self.url+urllib.parse.urlencode(data))
             try:
-                result = json.load(reader(fd))
+                result = json.load(fd)
+            except Exception as e:
+                raise IOError("Failed to load return from the HEKClient.") from e
             finally:
                 fd.close()
             results.extend(result['result'])


Test patch:
diff --git a/sunpy/net/tests/test_hek.py b/sunpy/net/tests/test_hek.py
--- a/sunpy/net/tests/test_hek.py
+++ b/sunpy/net/tests/test_hek.py
@@ -169,4 +169,21 @@ def test_mixed_results_get():
     # To check that the following bug is fixed:
     # https://github.com/sunpy/sunpy/issues/3238
     client = hek.HEKClient()
-    client.search(hek.attrs.Time(parse_time('2013/02/01 00:00:00'), parse_time('2013/02/01 23:30:00')), hek.attrs.FRM.Name == 'SPoCA')
+    result = client.search(hek.attrs.Time(parse_time('2013/02/01 00:00:00'),
+                                          parse_time('2013/02/01 23:30:00')), hek.attrs.FRM.Name == 'SPoCA')
+    assert isinstance(result, hek.hek.HEKTable)
+    assert len(result) == 89
+    assert result[0]["SOL_standard"] == 'SOL2013-01-31T20:13:31L219C160'
+
+
+@pytest.mark.remote_data
+def test_mixed_results_get_2():
+    # To check that the following bug is fixed:
+    # # https://github.com/sunpy/sunpy/issues/3898
+    client = hek.HEKClient()
+    result = client.search(hek.attrs.Time(parse_time('2011/08/09 07:23:56'),
+                                          parse_time('2011/08/09 12:40:29')),
+                           hek.attrs.EventType("FL"))
+    assert isinstance(result, hek.hek.HEKTable)
+    assert len(result) == 19
+    assert result[0]["SOL_standard"] == 'SOL2011-08-08T01:30:04L247C075'