-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
59 lines (47 loc) · 1.65 KB
/
main.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
import requests
from bs4 import BeautifulSoup
class FacebookAPIParser:
BASE_URL = 'https://developers.facebook.com/docs/marketing-api/reference/{}#fields'
def __init__(self):
self.parsed_endpoints = {}
def scan_entities(self, entities):
for entity in entities:
self.scan_entity_api(self.BASE_URL.format(entity))
for url, attrs in self.parsed_endpoints.items():
print(url)
for attr_name, attr_type in attrs:
print(f"{attr_name}: {attr_type}")
def scan_entity_api(self, url):
print("scanning", url)
if url in self.parsed_endpoints:
print("found in parsed, skipping...")
return
html_text = requests.get(url).text
soup = BeautifulSoup(html_text, 'html.parser')
p = soup.find("h3", text="Fields")
table = p.parent.find("table")
attrs = []
for row in table.find_all("tr"):
cells = row.find_all("td")
if not cells:
continue
divs = cells[0].find_all("div")
attr_name = divs[0].find("code").get_text()
href = divs[1].find("a", href=True)
if href:
print('Found link, navigating', href)
self.scan_entity_api(href['href'])
attr_type = divs[1].get_text()
attrs.append((attr_name, attr_type))
self.parsed_endpoints[url] = attrs
def main():
entities = [
'ad-creative',
'ad-campaign-group',
'ad-campaign',
'adgroup',
'ads-insights'
]
parser = FacebookAPIParser()
parser.scan_entities(entities)
main()