# Daily sync

Pull campaign data into your team's database every day without duplicates.

The recommended pattern: pull active campaigns once or a few times a day, then read their content until `has_more` runs out.

## The flow

1. Get the campaign list with `status=active`.
2. For each campaign, read content until `has_more` is `false`.
3. Store rows with the content `id` as a unique key, so a re-pull updates the same row.
4. A campaign that has ended only needs one more pull after its `status` becomes `ended`.

## Full example

```python
import os, time, requests

API = "https://api.wefluence.app/v1"
HEADERS = {"Authorization": f"Bearer {os.environ['WEFLUENCE_API_KEY']}"}

def get_all(path, **params):
    while True:
        r = requests.get(f"{API}{path}", headers=HEADERS, params=params, timeout=30)
        if r.status_code == 429:
            time.sleep(int(r.headers.get("Retry-After", "60")))
            continue
        r.raise_for_status()
        page = r.json()
        yield from page["data"]
        if not page["has_more"]:
            return
        params["cursor"] = page["next_cursor"]

for campaign in get_all("/campaigns", status="active", limit=100):
    for content in get_all(f"/campaigns/{campaign['id']}/content", limit=100):
        print(campaign["title"], content["url"], content["views"]["verified"])
```
```javascript
const API = "https://api.wefluence.app/v1";
const headers = { Authorization: `Bearer ${process.env.WEFLUENCE_API_KEY}` };

async function* getAll(path, params = {}) {
  while (true) {
    const res = await fetch(`${API}${path}?${new URLSearchParams(params)}`, { headers });
    if (res.status === 429) {
      const wait = Number(res.headers.get("retry-after") || 60);
      await new Promise((r) => setTimeout(r, wait * 1000));
      continue;
    }
    const page = await res.json();
    yield* page.data;
    if (!page.has_more) return;
    params.cursor = page.next_cursor;
  }
}

for await (const campaign of getAll("/campaigns", { status: "active", limit: 100 })) {
  for await (const content of getAll(`/campaigns/${campaign.id}/content`, { limit: 100 })) {
    console.log(campaign.title, content.url, content.views.verified);
  }
}
```
## Scheduling

One to four times a day is enough, because views update when content is verified. Use cron, GitHub Actions, or whatever scheduler you already have.

If you manage several brands, run the same flow for each key.
