Repository: sunpy/sunpy

Issue:
Download data and add entries to database from a HEK query
This should also be handled by the `Database.download` method.

Notes:
- add a new method download_from_hek_query_result(query_result, path=None, progress=False) which translates the incoming HEK qr to a VSO qr and then calls download_from_vso_query_result and passes the parameters path and progress on to this method.



Discussion:
Notes:
    - add a new method `download_from_hek_query_result(query_result, path=None, progress=False)` which translates the incoming HEK qr to a VSO qr and then calls `download_from_vso_query_result` and passes the parameters `path` and `progress` on to this method.


Resolving patch:
diff --git a/sunpy/database/database.py b/sunpy/database/database.py
--- a/sunpy/database/database.py
+++ b/sunpy/database/database.py
@@ -799,6 +799,47 @@ def add_from_hek_query_result(self, query_result,
             H2VClient().translate_and_query(query_result))
         self.add_from_vso_query_result(vso_qr, ignore_already_added)
 
+    def download_from_hek_query_result(self, query_result, client=None, path=None, progress=False,
+                                       ignore_already_added=False, overwrite=False):
+        """
+        Add new database entries from a hek query result by converting it
+        into vso query and download the corresponding data files.
+
+        Parameters
+        ----------
+        query_result : `HEKTable` or `HEKRow`
+            The value returned by :meth:`sunpy.net.hek.HEKClient().search`
+        client : `sunpy.net.vso.VSOClient`, optional
+            VSO Client instance to use for search and download.
+            If not specified a new instance will be created.
+        path : `str`
+            Path to download the files.
+        progress : `bool`
+            If True, displays the progress bar during file download.
+        ignore_already_added : `bool`
+            See :meth:`sunpy.database.Database.add`.
+        overwrite : `bool`, optional
+            If True, matching database entries from the query results will be
+            deleted and replaced with new database entries, with all files
+            getting downloaded.
+            Otherwise, no new file download and update of matching database
+            entries takes place.
+        """
+        if not query_result:
+            return
+
+        iterator = itertools.chain.from_iterable(
+            H2VClient().translate_and_query(query_result))
+
+        vso_qr = []
+
+        for query in iterator:
+            vso_qr.append(query)
+
+        self.download_from_vso_query_result(vso_qr, client, path,
+                                            progress, ignore_already_added,
+                                            overwrite)
+
     def download_from_vso_query_result(self, query_result, client=None,
                                        path=None, progress=False,
                                        ignore_already_added=False, overwrite=False):


Test patch:
diff --git a/sunpy/database/tests/test_database.py b/sunpy/database/tests/test_database.py
--- a/sunpy/database/tests/test_database.py
+++ b/sunpy/database/tests/test_database.py
@@ -15,11 +15,15 @@
 from astropy import units
 from astropy.utils.exceptions import AstropyUserWarning
 
+from parfive.downloader import Downloader
+from parfive.results import Results
+
 import sunpy
 import sunpy.data.test
 from sunpy.io import fits
 from sunpy.net import Fido, hek, vso
 from sunpy.net import attrs as net_attrs
+from sunpy.database import tables
 from sunpy.database import (Database, NoSuchTagError, EntryNotFoundError, EntryAlreadyAddedError,
                             TagAlreadyAssignedError, EntryAlreadyStarredError,
                             EntryAlreadyUnstarredError, attrs, disable_undo, split_database)
@@ -441,6 +445,52 @@ def test_add_entry_from_hek_qr(database):
     assert len(database) > 1
 
 
+@pytest.mark.remote_data
+def test_hek_query_download(monkeypatch, database, tmpdir):
+
+    assert len(database) == 0
+
+    records = ['94_1331820530-1331820530', '94_1331820542-1331820542',
+               '94_1331820554-1331820554', '94_1331820566-1331820566',
+               '94_1331820578-1331820578', '94_1331820590-1331820590',
+               '94_1331820602-1331820602', '94_1331820614-1331820614',
+               '94_1331820626-1331820626', '94_1331820638-1331820638']
+
+    def mock_parfive_download(obj, *args, **kwargs):
+
+        assert obj.http_queue.qsize() == 10
+        assert obj.ftp_queue.qsize() == 0
+
+        queue = obj.http_queue
+        obj_records = []
+
+        while not queue.empty():
+            url = queue.get_nowait().keywords['url']
+            obj_records.append(url[-24:])
+
+        assert obj_records == records
+
+        result = Results()
+        result.append(str(tmpdir))
+        return result
+
+    def mock_entries_from_dir(*args, **kwargs):
+        for i in range(10):
+            yield DatabaseEntry()
+
+    monkeypatch.setattr(Downloader, "download", mock_parfive_download)
+    monkeypatch.setattr(tables, "entries_from_dir", mock_entries_from_dir)
+
+    query = hek.HEKClient().search(
+        hek.attrs.Time('2019/03/10 14:40:10', '2019/04/11 16:40:50'),
+        hek.attrs.EventType('FL')
+    )
+
+    database.download_from_hek_query_result(query[4], path=str(tmpdir))
+
+    assert len(database) == 10
+
+
 def num_entries_from_vso_query(db, query, path=None, file_pattern='',
                                overwrite=False):
     db.download_from_vso_query_result(