-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfb_adlist.py
105 lines (83 loc) · 2.82 KB
/
fb_adlist.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
"""
Fbook Remove Adlist
"""
import logging
import click
from rich.logging import RichHandler
from api.fbook import FbAuth, Fbook
logging.basicConfig(level=logging.INFO, handlers=[RichHandler()])
log = logging.getLogger()
@click.group()
@click.pass_context
def cli(context: click.Context):
"""Main CLI function
Load FB cookies into context
Args:
context (click.Context): click Context
"""
log.info("Loading cookies from environment variable.")
cookies = FbAuth().load_cookies()
context.obj = Fbook(cookies)
@cli.command(help="Opt-out of business adlists")
@click.pass_obj
def opt_out(fb_: Fbook):
"""Opt-out of business adlist
Args:
fb_ (Fbook): Fbook helper obj
"""
log.info("Loading business adlist.")
business_adlist = fb_.get_business_adlist()
log.info(f"Found '{len(business_adlist)}' business(es).")
for business in business_adlist:
name_ = business["name"]
id_ = business["business_id"]
status = fb_.opt_out_business(business["business_id"])
log.info(f"Opting out business '{name_}' with id '{id_}'. Status: '{status}'.")
@cli.command(help="Remove interests")
@click.pass_obj
def rm_interest(fb_: Fbook):
"""Remove all interests
Args:
fb_ (Fbook): Fbook helper obj
"""
log.info("Loading interest list.")
interest_list = fb_.get_interest_list()
log.info(f"Found '{len(interest_list)}' interest(s).")
for interest in interest_list:
name_ = interest["name"]
id_ = interest["interest_id"]
status = fb_.remove_interest(interest["interest_id"])
log.info(f"Removing interest '{name_}' with id '{id_}'. Status: '{status}'.")
@cli.command(help="Delete advertisers")
@click.pass_obj
@click.option(
"-c", "--count", type=int, default=10, help="Number of advertiser list to load"
)
def del_ad(fb_: Fbook, count: int):
"""Delete advertisers
Args:
fb_ (Fbook): Fbook helper obj
count (int): number of advertiser list to load
"""
logging.info(f"Load {count} list(s) of advertisers.")
for counter in range(count):
log.info(f"Getting Advertisers list no {counter+1}.")
ad_list = fb_.get_advertiser_list()
if len(ad_list) == 0:
log.info("No advertiser found!")
break
for ad_page in ad_list:
page_id = ad_page["advertiser_id"]
page_name = ad_page["name"]
status = fb_.hide_advertiser(page_id)
log.info(f"Hiding page '{page_name}', id '{page_id}'. Hidden: '{status}'")
# Run all commands
@cli.command(name="all", help="Run all commands")
@click.pass_context
def run_all_commands(ctx):
"""Run all commands"""
ctx.invoke(del_ad)
ctx.invoke(opt_out)
ctx.invoke(rm_interest)
if __name__ == "__main__":
cli() # pylint: disable=no-value-for-parameter