hamed-dashboards/dashboards/ecommerce/harvest_contracts_and_levers.py

68 lines
3.1 KiB
Python
Executable file

import requests, json, time, re, sqlite3
import urllib.parse
from bs4 import BeautifulSoup
DB_PATH = '/workspace/ecommerce_research_sources/ecommerce_brands_research.db'
queries = [
"دیجی کالا جدول درصد کمیسیون فروشندگان",
"قرارداد فروشندگان دیجی کالا شرایط و جرایم",
"دیجی کالا افیلیت مارکتینگ سیستم همکاری در فروش",
"دیجی پلاس اشتراک درآمد دیجی کالا",
"دیجی کالا مگنت پلتفرم بررسی و سوشال کامرس",
"تبلیغات در دیجی کالا ریتل مدیا قیمت کلیک",
"دیجی پی اعتبار اقساطی کارمزد پذیرنده BNPL",
"دیجی کالا جت مدل درآمدی دارک استور",
"دیجی کالا بیزینس فروش سازمانی B2B",
"هزینه انبارداری و پردازش دیجی کالا FBD",
"الگوریتم بای باکس دیجی کالا Buy Box",
"پیشنهاد شگفت انگیز دیجی کالا شرایط تخفیف",
"جریمه کالای تقلبی غیر اصل دیجی کالا",
"چرخه تسویه حساب فروشندگان دیجی کالا"
]
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'}
conn = sqlite3.connect(DB_PATH)
c = conn.cursor()
new_sources = []
for q in queries:
url = f"https://digiato.com/?s={urllib.parse.quote(q)}"
try:
r = requests.get(url, headers=headers, timeout=10)
if r.status_code == 200:
soup = BeautifulSoup(r.text, 'lxml')
for a in soup.find_all('a', href=True):
href = a['href']
title = a.get_text(strip=True)
if 'digiato.com/' in href and len(title) > 20 and not any(x in href for x in ['/topic/', '/category/', '/author/']):
new_sources.append({
'title': title,
'url': href,
'source_type': 'Media Analysis / Factcheck',
'platform': 'Digiato',
'publication_year': '1401-1403',
'category': 'Contracts & Growth Levers',
'summary': f'تحلیل پیرامون {q}',
'entities_mentioned': 'دیجی‌کالا، فروشندگان، اهرم رشد',
'http_status': 200
})
except Exception as e:
pass
time.sleep(0.5)
print(f"Collected {len(new_sources)} new sources for contracts and levers.")
# Insert into DB
for s in new_sources:
c.execute('''
INSERT OR IGNORE INTO sources (title, url, source_type, platform, publication_year, category, summary, entities_mentioned, http_status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
''', (s['title'], s['url'], s['source_type'], s['platform'], s['publication_year'], s['category'], s['summary'], s['entities_mentioned'], s['http_status']))
conn.commit()
c.execute('SELECT count(*) FROM sources')
print('Total sources in DB now:', c.fetchone()[0])
conn.close()