facebookresearch / Ad-Library-API-Script-Repository

GitHub repository of commonly used python scripts that allows everyone to pull data via the Ad Library API
Other
220 stars 87 forks source link

Query issue #28

Open noralegrand opened 1 year ago

noralegrand commented 1 year ago

Hi, Has anyone else run into this issue before ? Traceback (most recent call last): File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 147, in main() File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 138, in main get_operators()[opts.action]( File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_operators.py", line 35, in count_ads for ad_archives in generator_ad_archives: File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api.py", line 80, in _get_ad_archives_from_url start_time_cutoff_after = datetime.strptime(after_date, "%Y-%m-%d").timestamp() OSError: [Errno 22] Invalid argument

Here is the query that i ran : py fb_ads_library_api_cli.py -t (access-token) -f "id,page_id,page_name,spend,demographic_distribution,bylines" -c "CA" -s "." --after-date "2023-07-01" save_to_csv data.csv

kimberlyzhu commented 1 year ago

Hi @noradioactive,

For your specific error OSError: [Errno 22] Invalid argument, this stack overflow post should provide some context. This seems to be an issue for specific Python versions as well as Windows where our default start date of 1970-01-01 is too early. You should keep the --after-date argument to prevent this issue.

If the result you get back when including the --after-date argument is an empty CSV, then it's because you need to include ad_delivery_start_time as a request column in the input.

The following query works:

python3 fb_ads_library_api_cli.py -t <access-token> -f "id,page_id,page_name,spend,demographic_distribution,bylines,ad_delivery_start_time" -c "CA" -s "." --after-date "2023-07-01" save_to_csv data.csv
noralegrand commented 1 year ago

Hi Kimberly,

Thanks for the tip. However, i'm facing a new error with the query you suggested :

Traceback (most recent call last): File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 11, in from fb_ads_library_api import FbAdsLibraryTraversal File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api.py", line 13, in import requests ModuleNotFoundError: No module named 'requests'

kimberlyzhu commented 1 year ago

Hi @noradioactive ,

Looks like you need to install the module requests. You can find instructions in the README's Requirements section to install requests

noralegrand commented 1 year ago

Hi Kimberly,

here's new error after installing requests : Traceback (most recent call last): File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 13, in from fb_ads_library_api_utils import get_country_code, is_valid_fields File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_utils.py", line 9, in from iso3166 import countries ModuleNotFoundError: No module named 'iso3166'

PS C:\Ad-Library-API-Script-Repository-main\python> pip install iso3166 Requirement already satisfied

kimberlyzhu commented 1 year ago

Hi @noradioactive,

Can you try pip3 install iso3166. Otherwise, this seems to be a setup issue for your python modules.

These posts might help you debug your setup

From what i understand:

noralegrand commented 1 year ago

Hi Kimberly,

It worked, however it raised another error. I couldn't find a solution on stack overflow that is accurate with my case :

Traceback (most recent call last): File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 147, in main() File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_cli.py", line 134, in main save_to_csv( File "C:\Ad-Library-API-Script-Repository-main\python\fb_ads_library_api_operators.py", line 97, in save_to_csv csvfile.write(output) File "C:\Users\norad\AppData\Local\Programs\Python\Python311\Lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ UnicodeEncodeError: 'charmap' codec can't encode characters in position 23868-23874: character maps to

kimberlyzhu commented 1 year ago

Hi @noradioactive ,

You might need to modify the code to write with utf-8 encoding: https://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters

noralegrand commented 1 year ago

Hi Kimberly, I was able to modify the code with the utf-8 encoding and run a query. But it keeps loading until my token expires and I get an error. Here's the query :

py fb_ads_library_api_cli.py -t (token) -f "id,page_id,page_name,spend,demographic_distribution,bylines,ad_delivery_start_time" -c "CA" -s "." --after-date "2023-08-15" save_to_csv data.csv

kimberlyzhu commented 1 year ago

@noradioactive if your access token doesn't last long enough, you may need to modify your query to search for a more specific search.

noralegrand commented 1 year ago

Hi Kimberly, This has been working so far, thanks a lot. Is it possible to change the default "ALL" ad_type value to "POLITICAL_AND_ISSUE_ADS" in my query ? or any other default values ? Thanks