alex9smith / gdelt-doc-api

A Python client for the GDELT 2.0 Doc API
MIT License
91 stars 20 forks source link

multi_repeat function is not working #11

Closed risto-trajanov closed 2 years ago

risto-trajanov commented 2 years ago

My code

from gdeltdoc import Filters, near, repeat, GdeltDoc, multi_repeat

f = Filters(
    start_date = "2017-05-01",
    end_date = "2022-02-02",
    num_records = 250,
    keyword = "low carbon",
    near = near(10, 'emission', 'airport'),
    repeat = multi_repeat([(2, "airline"), (3, "airport")], "AND")
)
gd = GdeltDoc()

# Search for articles matching the filters
articles = gd.article_search(f)

The output:

TypeError                                 Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_1576\752362330.py in <module>
      2 
      3 # Search for articles matching the filters
----> 4 articles = gd.article_search(f)
      5 
      6 # Get a timeline of the number of articles matching the filters

~\.conda\envs\newEnv\lib\site-packages\gdeltdoc\api_client.py in article_search(self, filters)
     77         """
     78         articles = self._query("artlist", filters.query_string)
---> 79         if "articles" in articles:
     80             return pd.DataFrame(articles["articles"])
     81         else:

TypeError: argument of type 'int' is not iterable

Do you have an idea what is wrong?

alex9smith commented 2 years ago

Hi @risto-trajanov.

Thanks for your patience with this.

There was a bug in generating the query string with multi_repeat which was fixed in #12.

I've just released version 1.3.2, so if you update to that your query should now work:

(venv) gdelt-doc-api (main) e1b2bf8 $ ipython
Python 3.9.4 (default, May 10 2021, 13:51:57)
Type 'copyright', 'credits' or 'license' for more information
IPython 8.2.0 -- An enhanced Interactive Python. Type '?' for help.

In [1]: from gdeltdoc import Filters, near, repeat, GdeltDoc, multi_repeat
   ...:
   ...: f = Filters(
   ...:     start_date = "2017-05-01",
   ...:     end_date = "2022-02-02",
   ...:     num_records = 250,
   ...:     keyword = "low carbon",
   ...:     near = near(10, 'emission', 'airport'),
   ...:     repeat = multi_repeat([(2, "airline"), (3, "airport")], "AND")
   ...: )
   ...: gd = GdeltDoc()
   ...:
   ...: # Search for articles matching the filters
   ...: articles = gd.article_search(f)
   ...:

In [2]: articles
Out[2]:
                                                 url url_mobile                                              title  ...                 domain language sourcecountry
0  https://centreforaviation.com/analysis/reports...             Aviation Sustainability and the Environment , ...  ...  centreforaviation.com  English         China

[1 rows x 8 columns]