Ad-Library-API-Script-Repository icon indicating copy to clipboard operation
Ad-Library-API-Script-Repository copied to clipboard

Query issue

Open noralegrand opened this issue 2 years ago • 10 comments

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

noralegrand avatar Jul 31 '23 17:07 noralegrand

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

kimberlyzhu avatar Aug 07 '23 22:08 kimberlyzhu

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'

noralegrand avatar Aug 08 '23 09:08 noralegrand

Hi @noradioactive ,

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

kimberlyzhu avatar Aug 08 '23 17:08 kimberlyzhu

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

noralegrand avatar Aug 09 '23 12:08 noralegrand

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

  • https://stackoverflow.com/questions/74256342/python-module-not-found-even-though-its-installed
  • https://stackoverflow.com/questions/45693420/python-modulenotfounderror-but-module-is-installed
  • https://stackoverflow.com/questions/66728011/module-not-found-even-after-pip-install

From what i understand:

  • If you run pip -V in the cli it will display where pip will install your module.
  • If you run import sysconfig; print(sysconfig.get_paths()["purelib"]) in python, it will show where python looks for packages.
  • You should be able to install the module in the right place by running python3 -m pip install iso3166 .

kimberlyzhu avatar Aug 09 '23 17:08 kimberlyzhu

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

noralegrand avatar Aug 10 '23 09:08 noralegrand

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

kimberlyzhu avatar Aug 15 '23 17:08 kimberlyzhu

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

noralegrand avatar Aug 31 '23 11:08 noralegrand

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

kimberlyzhu avatar Sep 12 '23 20:09 kimberlyzhu

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

noralegrand avatar Oct 30 '23 10:10 noralegrand