|
1 | 1 | # Ads-Library-API-Script-Repository
|
2 |
| -Ads-Library-API-Script-Repository is a set of code examples to help user/researchers understand how the Facebook Ads Library API works. It also provides a simple command-line interface(CLI) for users to easily use the Facebook Ads Library API. |
| 2 | +Ads-Library-API-Script-Repository is a set of code examples to help user/researchers understand how the Meta Ad Library API works. It also provides a simple command-line interface(CLI) for users to easily use the Meta Ad Library API. |
3 | 3 |
|
4 |
| -## Examples |
5 |
| -Here's an example on how to use the CLI: |
| 4 | +## Setup |
6 | 5 |
|
7 |
| - $ python fb_ads_library_api_cli.py -t {access_token} -f 'page_id,ad_snapshot_url,funding_entity,ad_delivery_start_time' -c 'CA' -s '.' -v count |
| 6 | +### Make sure you have Python 3 installed |
8 | 7 |
|
9 |
| -It would count the number of all polictical ads in CA(Canada); |
| 8 | +This command should show you a path to the executable, like `/usr/bin/python3` |
| 9 | +```bash |
| 10 | +which python3 |
| 11 | +``` |
10 | 12 |
|
11 |
| -Note: please replace the '{access_token}' with your [Facebook Developer access token](https://developers.facebook.com/tools/accesstoken/). |
| 13 | +If Python isn't installed and you're on an Apple computer, [install homebrew](https://brew.sh/) and use it to install python3 |
| 14 | +```bash |
| 15 | +brew install python |
| 16 | +``` |
12 | 17 |
|
13 |
| -## Requirements |
14 |
| -Ads-Library-API-Script-Repository requires or works with |
15 |
| -* Mac OS X or Linux or Window |
16 |
| -* Python 3.0+ |
17 |
| -* Python Requests Library ([installation](https://docs.python-requests.org/en/master/user/install/#install)) |
18 |
| -* Python iso3166 Library ([installation](https://pypi.org/project/iso3166/)) |
| 18 | +You can check [Python's downloads page](https://www.python.org/downloads/) for instructions on installing on other operating systems. |
19 | 19 |
|
| 20 | +### Start a virtual environment |
| 21 | + |
| 22 | +Create the environment |
| 23 | +```bash |
| 24 | +python3 -m venv venv |
| 25 | +``` |
| 26 | + |
| 27 | +Activate it |
| 28 | +```bash |
| 29 | +source venv/bin/activate |
| 30 | +``` |
| 31 | + |
| 32 | +### Install the required packages |
| 33 | +```bash |
| 34 | +python3 -m pip install -r requirements.txt |
| 35 | +``` |
| 36 | + |
| 37 | +## Usage |
| 38 | + |
| 39 | +To use these scripts to access the [Meta Ad Library API](https://www.facebook.com/ads/library/api), you must have a Facebook developer account, which will require you to confirm your identity (by uploading identifying documents such as a drivers license or passport) and mailing address (by entering a code that Meta sends you in the physical mail.) |
| 40 | + |
| 41 | +Once those details are confirmed, you can create a new app (an app of type "Business" will work) which will allow you to generate an access token. That token is required by these scripts to authenticate with the API. The access token can be found on the [Graph API Explorer](https://developers.facebook.com/tools/explorer/) or the [Access Token Tool](https://developers.facebook.com/tools/accesstoken/), where it's described as the "User Token". |
| 42 | + |
| 43 | +The access token can be passed to the script using the `-t` flag, or saved in a `.env` file with the key `ACCESS_TOKEN`. You can copy the `.env-sample` file in this repository to `.env` and fill in your token there. |
| 44 | + |
| 45 | +```bash |
| 46 | +cp .env-sample .env |
| 47 | +``` |
| 48 | + |
| 49 | +If you choose to save the results of your query to a file, they will be saved in the `output` directory, in a folder time-stamped with the time you started the query. |
| 50 | + |
| 51 | +Here are some examples on how to use the CLI: |
| 52 | + |
| 53 | +### Count the number of political ads in Canada (CA) |
| 54 | +replace `{access_token}` with your token |
| 55 | +```python |
| 56 | +python3 python/fb_ads_library_api_cli.py -t {access_token} -f 'page_id,ad_snapshot_url,funding_entity,ad_delivery_start_time' -c 'CA' -s '.' -v count |
| 57 | +``` |
| 58 | + |
| 59 | +### Search US political ads delivered after 7/20 for "coconut" and save them to a CSV file |
| 60 | +Assuming you've put your access token in `.env` |
| 61 | +```python |
| 62 | +python3 python/fb_ads_library_api_cli.py -f 'id,ad_creation_time,ad_creative_bodies,ad_creative_link_captions,ad_creative_link_descriptions,ad_creative_link_titles,ad_delivery_start_time,ad_delivery_stop_time,ad_snapshot_url,age_country_gender_reach_breakdown,beneficiary_payers,bylines,currency,delivery_by_region,demographic_distribution,estimated_audience_size,eu_total_reach,impressions,languages,page_id,page_name,publisher_platforms,spend,target_ages,target_gender,target_locations' -c 'US' --ad-type 'POLITICAL_AND_ISSUE_ADS' -s 'coconut' --batch-size 250 --after-date 2024-07-20 -v save_to_csv coconut_after_07_20 |
| 63 | +``` |
| 64 | + |
| 65 | +### Options |
| 66 | + |
| 67 | +You can see the available arguments by passing `--help` |
| 68 | + |
| 69 | +```bash |
| 70 | +python3 python/fb_ads_library_api_cli.py --help |
| 71 | +``` |
| 72 | + |
| 73 | +``` |
| 74 | +The Meta Ad Library API CLI Utility |
| 75 | +
|
| 76 | +positional arguments: |
| 77 | + action Action to take on the ads, possible values: count,save,save_to_csv,start_time_trending |
| 78 | + args The parameter for the specific action |
| 79 | +
|
| 80 | +options: |
| 81 | + -h, --help show this help message and exit |
| 82 | + -t ACCESS_TOKEN, --access-token ACCESS_TOKEN |
| 83 | + The Facebook developer access token |
| 84 | + -f FIELDS, --fields FIELDS |
| 85 | + Fields to retrieve from the Ad Library API, comma-separated, no spaces |
| 86 | + -s SEARCH_TERMS, --search-terms SEARCH_TERMS |
| 87 | + The terms you want to search for, space-separated |
| 88 | + -c COUNTRY, --country COUNTRY |
| 89 | + Country code(s), comma-separated, no spaces |
| 90 | + --search-page-ids SEARCH_PAGE_IDS |
| 91 | + A specific Facebook Page you want to search |
| 92 | + --ad-active-status AD_ACTIVE_STATUS |
| 93 | + Filter by the current status of the ads at the moment the script runs, can be ALL (default), ACTIVE, INACTIVE |
| 94 | + --ad-type AD_TYPE Return this type of ad, can be ALL (default), CREDIT_ADS, EMPLOYMENT_ADS, HOUSING_ADS, POLITICAL_AND_ISSUE_ADS |
| 95 | + --media-type MEDIA_TYPE |
| 96 | + Return ads that contain this type of media, can be ALL (default), IMAGE, MEME, VIDEO, NONE |
| 97 | + --after-date AFTER_DATE |
| 98 | + Only return ads that started delivery after this date, in the format YYYY-MM-DD |
| 99 | + --batch-size BATCH_SIZE |
| 100 | + Request records in batches of this size, default is 250 |
| 101 | + --retry-limit RETRY_LIMIT |
| 102 | + How many times to retry when an error occurs, default is 3 |
| 103 | + -v, --verbose |
| 104 | +``` |
20 | 105 |
|
21 | 106 | ## How Ads-Library-API-Script-Repository works
|
22 |
| -The script will query the [Facebook Ads library API](https://www.facebook.com/ads/library/api) to get all the Ads Library information on the Facebook platform; |
| 107 | +The script will query the [Meta Ad Library API](https://www.facebook.com/ads/library/api) to get all the Ad Library information on the Facebook platform; |
23 | 108 |
|
24 |
| -## Full documentation |
25 |
| -You can find the full documentation here: (--to-be-added--) |
26 | 109 |
|
27 |
| -## More about Facebook Ads Library |
| 110 | +## More about Meta Ad Library |
28 | 111 | * Website: https://www.facebook.com/ads/library
|
29 | 112 | * Report: https://www.facebook.com/ads/library/report
|
30 | 113 | * API: https://www.facebook.com/ads/library/api
|
31 | 114 |
|
32 | 115 | See the [CONTRIBUTING](CONTRIBUTING.md) file for how to help out.
|
33 | 116 |
|
| 117 | + |
34 | 118 | ## License
|
35 | 119 | Ads-Library-API-Script-Repository is licensed under the Facebook Platform License, as found in the LICENSE file.
|
0 commit comments