import requests, json, time, re, sqlite3 import urllib.parse from bs4 import BeautifulSoup DB_PATH = '/workspace/ecommerce_brands_research.db' def insert_source(title, url, source_type, platform, pub_year, category, summary, entities, http_status=200): if not url or not title: return False url = url.strip() title = title.strip() conn = sqlite3.connect(DB_PATH) c = conn.cursor() try: c.execute(''' INSERT OR IGNORE INTO sources (title, url, source_type, platform, publication_year, category, summary, entities_mentioned, http_status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?) ''', (title, url, source_type, platform, pub_year, category, summary, entities, http_status)) conn.commit() inserted = c.rowcount > 0 except Exception as e: inserted = False finally: conn.close() return inserted headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36' } digiato_queries = [ ('دیجی کالا', 'In-depth Media Analysis', 'دیجی کالا, تحلیل کسب و کار, ایکامرس ایران'), ('مصاحبه دیجی کالا', 'Executive Interview', 'مصاحبه مدیران دیجی کالا, حمید محمدی, سعید محمدی, مسعود طباطبایی'), ('دیجی استایل', 'Platform Pivots', 'دیجی استایل, بازار پوشاک, ادغام با دیجی کالا'), ('دیجی کالا جت', 'Platform Pivots', 'دیجی کالا جت, خرید آنلاین فوری, سوپرمارکت'), ('دیجی پی', 'Platform Pivots', 'دیجی پی, وام و اعتبار, BNPL, پرداخت دیجیتال'), ('گزارش سالانه دیجی کالا', 'Annual Reports', 'گزارش سالانه, آمار و ارقام, پرفروش ترین برندها'), ('ممنوعیت واردات لوازم خانگی', 'Regulatory and Legal', 'ممنوعیت لوازم خانگی, اسنوا, سامسونگ, ال جی'), ('رجیستری موبایل همتا', 'Regulatory and Legal', 'رجیستری گوشی, سامانه همتا, قاچاق تلفن همراه'), ('آیفون ۱۴ ممنوعیت رجیستری', 'Regulatory and Legal', 'آیفون ۱۴, آیفون ۱۵, ممنوعیت واردات, قاچاق'), ('کالای تقلبی در دیجی کالا', 'Marketplace and Quality', 'کالای تقلبی, نشان غیر اصل, شکایات خریداران, کمیسیون'), ('اسنوا گروه انتخاب', 'Brand Case Studies', 'اسنوا, لوازم خانگی ایرانی, تولید ملی, فروش آنلاین'), ('مادیران ایکس ویژن', 'Brand Case Studies', 'مادیران, ایکس ویژن, تی سی ال, بازار تلویزیون'), ('گلدیران جی پلاس', 'Brand Case Studies', 'گلدیران, جی پلاس, جایگزین ال جی'), ('کاله سولیکو', 'Brand Case Studies', 'کاله, گروه سولیکو, دیجی کالا فرش, مواد غذایی'), ('زر ماکارون', 'Brand Case Studies', 'زر ماکارون, بازار آنلاین, غلات و پاستا'), ('سینره طبیعت زنده', 'Brand Case Studies', 'سینره, اینلی, محصولات آرایشی بهداشتی'), ('پیندو دیجی کالا', 'Platform Pivots', 'پیندو, بازار C2C, اگهی انلاین, دیوار'), ('همراه اول دیجی کالا سهام', 'Platform Pivots', 'خرید سهام دیجی کالا, همراه اول, حرکت اول, سرآوا') ] def harvest_digiato(): print("=== HARVESTING DIGIATO ARCHIVES ===") total_added = 0 for q, cat, ents in digiato_queries: for page in range(1, 4): url = f"https://digiato.com/page/{page}?s={urllib.parse.quote(q)}" try: r = requests.get(url, headers=headers, timeout=10) if r.status_code != 200: break soup = BeautifulSoup(r.text, 'html.parser') added_page = 0 for a in soup.find_all('a', href=True): href = a['href'] title = a.get_text(strip=True) if not href.startswith('https://digiato.com/'): continue # Check if it's an article URL if any(x in href for x in ['/iran-technology-news/', '/topic/', '/promoted/', '/articles/', '/business/', '/report/', '/tech/']): if len(title) > 20 and not 'دیدگاه' in title and not 'تبلیغات' in title and not 'صفحه' in title: # Try to extract year from URL year_match = re.search(r'201\d|202\d|139\d|140\d', href + " " + title) pub_year = year_match.group(0) if year_match else "1395-1403" summary = f"مقاله تحلیلی دیجیاتو پیرامون {title}." if insert_source(title, href, cat if 'Interview' in cat or 'Report' in cat else 'In-depth Media Analysis', 'Digiato', pub_year, cat, summary, ents): added_page += 1 total_added += 1 time.sleep(0.5) except Exception as e: print(f"Error Digiato '{q}' p{page}: {e}") break print(f"[Digiato] '{q}' -> Total added so far: {total_added}") print(f"=== DIGIATO COMPLETE: Added {total_added} articles ===") shanbe_queries = [ ('دیجی کالا', 'In-depth Media Analysis', 'دیجی کالا, هفته نامه شنبه, استارتاپ های ایران'), ('مصاحبه حمید محمدی', 'Executive Interview', 'حمید محمدی, هفته نامه شنبه, داستان بنیانگذاران'), ('مصاحبه سعید محمدی', 'Executive Interview', 'سعید محمدی, هفته نامه شنبه, دیجی کالا'), ('دیجی استایل', 'Platform Pivots', 'دیجی استایل, هفته نامه شنبه, بازار پوشاک'), ('دیجی پی', 'Platform Pivots', 'دیجی پی, فین تک, شنبه مگ, لندتک'), ('گزارش سالانه دیجی کالا', 'Annual Reports', 'گزارش سالانه دیجی کالا, اکوسیستم استارتاپی'), ('سرآوا', 'Executive Interview', 'سرآوا پارس, سعید رحمانی, علی فیاض بخش, سرمایه گذاری خطرپذیر'), ('بامیلو', 'Platform Pivots', 'بامیلو, رامتین منزهیان, رقابت ایکامرس, شکست استارتاپ') ] def harvest_shanbe(): print("=== HARVESTING SHANBE MAG ARCHIVES ===") total_added = 0 for q, cat, ents in shanbe_queries: for page in range(1, 4): url = f"https://shanbemag.com/page/{page}/?s={urllib.parse.quote(q)}" try: r = requests.get(url, headers=headers, timeout=10) if r.status_code != 200: break soup = BeautifulSoup(r.text, 'html.parser') for a in soup.find_all('a', href=True): href = a['href'] title = a.get_text(strip=True) if href.startswith('https://shanbemag.com/') and len(title) > 20: if not any(x in href for x in ['/category/', '/tag/', '/author/', '/cart/']): year_match = re.search(r'201\d|202\d|139\d|140\d', href + " " + title) pub_year = year_match.group(0) if year_match else "1395-1403" summary = f"گزارش و تحلیل هفته‌نامه شنبه پیرامون {title}." if insert_source(title, href, cat if 'Interview' in cat or 'Report' in cat else 'In-depth Media Analysis', 'Shanbe Mag', pub_year, cat, summary, ents): total_added += 1 time.sleep(0.5) except Exception as e: print(f"Error Shanbe '{q}' p{page}: {e}") break print(f"[Shanbe] '{q}' -> Total added so far: {total_added}") print(f"=== SHANBE MAG COMPLETE: Added {total_added} articles ===") if __name__ == '__main__': harvest_digiato() harvest_shanbe()