Files
Brancheneinstufung2/import_competitive_radar.py

136 lines
5.5 KiB
Python

import json
import os
import requests
import sys
# Configuration
JSON_FILE = 'analysis_robo-planet.de-4.json'
TOKEN_FILE = 'notion_token.txt'
PARENT_PAGE_ID = "2e088f42-8544-8024-8289-deb383da3818"
# Database Titles
DB_TITLE_HUB = "📦 Competitive Radar (Companies) v6"
DB_TITLE_LANDMINES = "💣 Competitive Radar (Landmines) v6"
DB_TITLE_REFS = "🏆 Competitive Radar (References) v6"
DB_TITLE_PRODUCTS = "🤖 Competitive Radar (Products) v6"
def load_json_data(filepath):
with open(filepath, 'r') as f:
return json.load(f)
def load_notion_token(filepath):
with open(filepath, 'r') as f:
return f.read().strip()
def create_database(token, parent_id, title, properties):
url = "https://api.notion.com/v1/databases"
headers = {"Authorization": f"Bearer {token}", "Notion-Version": "2022-06-28", "Content-Type": "application/json"}
payload = {"parent": {"type": "page_id", "page_id": parent_id}, "title": [{"type": "text", "text": {"content": title}}], "properties": properties}
r = requests.post(url, headers=headers, json=payload)
if r.status_code != 200:
print(f"Error creating DB '{title}': {r.text}")
sys.exit(1)
return r.json()['id']
def create_page(token, db_id, properties):
url = "https://api.notion.com/v1/pages"
headers = {"Authorization": f"Bearer {token}", "Notion-Version": "2022-06-28", "Content-Type": "application/json"}
payload = {"parent": {"database_id": db_id}, "properties": properties}
r = requests.post(url, headers=headers, json=payload)
if r.status_code != 200:
print(f"Error creating page: {r.text}")
return r.json().get('id')
def main():
token = load_notion_token(TOKEN_FILE)
data = load_json_data(JSON_FILE)
print("🚀 Level 5 Import starting (v6 Databases)...")
# 1. Create Databases
hub_id = create_database(token, PARENT_PAGE_ID, DB_TITLE_HUB, {
"Name": {"title": {}},
"Website": {"url": {}},
"Target Industries": {"multi_select": {}}
})
lm_id = create_database(token, PARENT_PAGE_ID, DB_TITLE_LANDMINES, {
"Question": {"title": {}},
"Topic": {"select": {}},
"Related Competitor": {"relation": {"database_id": hub_id, "dual_property": {"synced_property_name": "Landmines"}}}
})
prod_id = create_database(token, PARENT_PAGE_ID, DB_TITLE_PRODUCTS, {
"Product": {"title": {}},
"Category": {"select": {}},
"Purpose": {"rich_text": {}},
"Related Competitor": {"relation": {"database_id": hub_id, "dual_property": {"synced_property_name": "Products"}}}
})
ref_id = create_database(token, PARENT_PAGE_ID, DB_TITLE_REFS, {
"Customer": {"title": {}},
"Industry": {"select": {}},
"Quote": {"rich_text": {}},
"Related Competitor": {"relation": {"database_id": hub_id, "dual_property": {"synced_property_name": "References"}}}
})
# 2. Import Companies & Products
comp_map = {}
for analysis in data.get('analyses', []):
c = analysis['competitor']
name = c['name']
# v5: 'target_industries' is at root level of analysis object
industries = analysis.get('target_industries', [])
props = {
"Name": {"title": [{"text": {"content": name}}]},
"Website": {"url": c['url'] or "https://google.com"},
"Target Industries": {"multi_select": [{"name": i[:100].replace(',', '')} for i in industries if i]}
}
pid = create_page(token, hub_id, props)
if pid:
comp_map[name] = pid
print(f" - Created Company: {name}")
for prod in analysis.get('portfolio', []):
p_props = {
"Product": {"title": [{"text": {"content": prod['product'][:100]}}]},
"Category": {"select": {"name": prod.get('category', 'Other')[:100]}},
"Purpose": {"rich_text": [{"text": {"content": prod.get('purpose', '')[:2000]}}]},
"Related Competitor": {"relation": [{"id": pid}]}
}
create_page(token, prod_id, p_props)
# 3. Import Battlecards (Landmines)
for card in data.get('battlecards', []):
cid = comp_map.get(card['competitor_name'])
if not cid: continue
for q in card.get('landmine_questions', []):
# Handle both string and object formats from LLM
text = q['text'] if isinstance(q, dict) else q
cat = q.get('category', 'General') if isinstance(q, dict) else 'General'
create_page(token, lm_id, {
"Question": {"title": [{"text": {"content": text[:100]}}]},
"Topic": {"select": {"name": cat}},
"Related Competitor": {"relation": [{"id": cid}]}
})
# 4. Import References
for ref_analysis in data.get('reference_analysis', []):
cid = comp_map.get(ref_analysis['competitor_name'])
if not cid: continue
for ref in ref_analysis.get('references', []):
create_page(token, ref_id, {
"Customer": {"title": [{"text": {"content": ref['name'][:100]}}]},
"Industry": {"select": {"name": ref.get('industry', 'Unknown')[:100].replace(',', '')}},
"Quote": {"rich_text": [{"text": {"content": ref.get('testimonial_snippet', '')[:2000]}}]},
"Related Competitor": {"relation": [{"id": cid}]}
})
print("✅ DONE")
if __name__ == "__main__":
main()