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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
"""
📊 SlideShare Harvester - Scraper til Neo4j Knowledge Graph
Henter præsentationer om: Cybersecurity, AI, Cloud, Strategy, NIS2, OSINT
"""
import json
import time
import hashlib
import re
from pathlib import Path
from datetime import datetime
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from webdriver_manager.chrome import ChromeDriverManager
from neo4j import GraphDatabase

class SlideShareHarvester:
    """SlideShare præsentation harvester"""
    
    NEO4J_URI = "neo4j+s://054eff27.databases.neo4j.io"
    NEO4J_USER = "neo4j"
    NEO4J_PASSWORD = "Qrt37mkb0xBZ7_ts5tG1J70K2mVDGPMF2L7Njlm7cg8"
    
    BASE_URL = "https://www.slideshare.net"
    
    # Søgetermer - fokuseret på WidgeTDC relevante emner
    SEARCH_TERMS = [
        # Cybersecurity
        "cybersecurity strategy",
        "SOC security operations",
        "threat intelligence CTI",
        "incident response",
        "NIS2 directive",
        "OSINT investigation",
        "penetration testing",
        "ransomware defense",
        "zero trust architecture",
        
        # AI & ML
        "AI cybersecurity",
        "machine learning security",
        "GPT enterprise",
        "generative AI business",
        "AI strategy enterprise",
        "LLM applications",
        
        # Cloud
        "cloud security architecture",
        "Azure security",
        "AWS security best practices",
        "multi-cloud strategy",
        "cloud migration security",
        
        # Business Strategy
        "digital transformation strategy",
        "IT strategy roadmap",
        "enterprise architecture",
        "technology roadmap",
        
        # Compliance & Governance
        "GDPR compliance",
        "ISO 27001",
        "cyber risk management",
        "security governance",
        
        # Specific Tech
        "SIEM implementation",
        "EDR endpoint detection",
        "MDR managed detection",
        "XDR security",
        "SOAR automation"
    ]
    
    def __init__(self):
        self.output_dir = Path("data/slideshare_harvest")
        self.output_dir.mkdir(parents=True, exist_ok=True)
        
        # Chrome options
        chrome_options = Options()
        chrome_options.add_argument("--start-maximized")
        chrome_options.add_argument("--disable-blink-features=AutomationControlled")
        chrome_options.add_experimental_option("excludeSwitches", ["enable-automation"])
        chrome_options.add_argument("--disable-notifications")
        
        print("🌐 Starter Chrome til SlideShare...")
        self.driver = webdriver.Chrome(
            service=Service(ChromeDriverManager().install()),
            options=chrome_options
        )
        
        # Neo4j connection
        self.neo4j = GraphDatabase.driver(
            self.NEO4J_URI,
            auth=(self.NEO4J_USER, self.NEO4J_PASSWORD)
        )
        
        self.presentations = []
        self.stats = {
            "searches": 0,
            "presentations_found": 0,
            "saved_to_neo4j": 0,
            "errors": 0
        }
    
    def search_presentations(self, query: str, max_results: int = 20) -> list:
        """Søg efter præsentationer"""
        results = []
        
        try:
            # Encode search query
            search_url = f"{self.BASE_URL}/search?searchfrom=header&q={query.replace(' ', '+')}"
            self.driver.get(search_url)
            time.sleep(3)
            
            self.stats["searches"] += 1
            
            # Scroll to load more results
            for _ in range(3):
                self.driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
                time.sleep(1)
            
            # Find presentation cards
            selectors = [
                'div[data-testid="slideshow-card"]',
                '.slideshow-card',
                'a[href*="/slideshow/"]',
                '.search-result-item',
                'article.slideshow'
            ]
            
            cards = []
            for selector in selectors:
                cards = self.driver.find_elements(By.CSS_SELECTOR, selector)
                if cards:
                    break
            
            # Also try finding by link pattern
            if not cards:
                cards = self.driver.find_elements(By.CSS_SELECTOR, 'a[href*="slideshare.net"]')
            
            print(f"      Found {len(cards)} potential results")
            
            for card in cards[:max_results]:
                try:
                    # Extract presentation info
                    presentation = self._extract_presentation_info(card, query)
                    if presentation and presentation.get('url'):
                        # Avoid duplicates
                        if not any(p['url'] == presentation['url'] for p in results):
                            results.append(presentation)
                            self.stats["presentations_found"] += 1
                except Exception as e:
                    continue
            
        except Exception as e:
            print(f"      ⚠️ Search error: {str(e)[:100]}")
            self.stats["errors"] += 1
        
        return results
    
    def _extract_presentation_info(self, element, search_query: str) -> dict:
        """Udtræk info fra et præsentationselement"""
        try:
            # Find link
            link = None
            url = ""
            title = ""
            
            # Try to get href directly or from child
            if element.tag_name == 'a':
                url = element.get_attribute('href') or ''
                title = element.text.strip() or element.get_attribute('title') or ''
            else:
                try:
                    link = element.find_element(By.CSS_SELECTOR, 'a[href*="/slideshow/"], a[href*="slideshare"]')
                    url = link.get_attribute('href') or ''
                    title = link.text.strip() or link.get_attribute('title') or ''
                except:
                    pass
            
            if not url or '/slideshow/' not in url:
                return None
            
            # Clean URL
            if '?' in url:
                url = url.split('?')[0]
            
            # Try to get description
            description = ""
            try:
                desc_elem = element.find_element(By.CSS_SELECTOR, '.description, .summary, p')
                description = desc_elem.text.strip()[:500]
            except:
                pass
            
            # Try to get author
            author = ""
            try:
                author_elem = element.find_element(By.CSS_SELECTOR, '.author, [data-testid="author"], .username')
                author = author_elem.text.strip()
            except:
                pass
            
            # Try to get views/downloads
            views = 0
            try:
                views_elem = element.find_element(By.CSS_SELECTOR, '.views, .stats')
                views_text = views_elem.text
                views_match = re.search(r'(\d+[,.\d]*)\s*(?:views|downloads)', views_text, re.I)
                if views_match:
                    views = int(views_match.group(1).replace(',', '').replace('.', ''))
            except:
                pass
            
            return {
                "title": title[:300] if title else f"Presentation from {url.split('/')[-1]}",
                "url": url,
                "description": description,
                "author": author,
                "views": views,
                "search_query": search_query,
                "source": "slideshare",
                "harvested_at": datetime.now().isoformat()
            }
            
        except Exception as e:
            return None
    
    def get_presentation_details(self, url: str) -> dict:
        """Hent detaljer fra en præsentationsside"""
        details = {}
        
        try:
            self.driver.get(url)
            time.sleep(2)
            
            # Title
            try:
                title_elem = self.driver.find_element(By.CSS_SELECTOR, 'h1, .slideshow-title, [data-testid="title"]')
                details['title'] = title_elem.text.strip()
            except:
                pass
            
            # Description
            try:
                desc_elem = self.driver.find_element(By.CSS_SELECTOR, '.slideshow-description, .description, [data-testid="description"]')
                details['description'] = desc_elem.text.strip()[:1000]
            except:
                pass
            
            # Author info
            try:
                author_elem = self.driver.find_element(By.CSS_SELECTOR, '.author-name, [data-testid="author"], .profile-name')
                details['author'] = author_elem.text.strip()
            except:
                pass
            
            # Categories/Tags
            try:
                tags = self.driver.find_elements(By.CSS_SELECTOR, '.tag, .category, [data-testid="tag"]')
                details['tags'] = [t.text.strip() for t in tags[:10] if t.text.strip()]
            except:
                details['tags'] = []
            
            # Slides count
            try:
                slides_elem = self.driver.find_element(By.CSS_SELECTOR, '.slides-count, [data-testid="slides-count"]')
                slides_match = re.search(r'(\d+)', slides_elem.text)
                if slides_match:
                    details['slide_count'] = int(slides_match.group(1))
            except:
                pass
            
            # Date
            try:
                date_elem = self.driver.find_element(By.CSS_SELECTOR, '.date, time, [data-testid="date"]')
                details['published_date'] = date_elem.text.strip()
            except:
                pass
            
        except Exception as e:
            print(f"      ⚠️ Details error: {str(e)[:50]}")
        
        return details
    
    def save_to_neo4j(self, presentation: dict):
        """Gem præsentation i Neo4j"""
        content_hash = hashlib.md5(presentation['url'].encode()).hexdigest()
        
        try:
            with self.neo4j.session() as session:
                session.run("""
                    MERGE (p:SlideSharePresentation {contentHash: $hash})
                    ON CREATE SET
                        p.title = $title,
                        p.url = $url,
                        p.description = $description,
                        p.author = $author,
                        p.views = $views,
                        p.searchQuery = $search_query,
                        p.tags = $tags,
                        p.slideCount = $slide_count,
                        p.harvestedAt = datetime()
                    ON MATCH SET
                        p.lastSeen = datetime(),
                        p.views = $views
                    
                    MERGE (ds:DataSource {name: 'SlideShare'})
                    ON CREATE SET ds.type = 'presentation_platform', ds.url = 'https://slideshare.net'
                    MERGE (p)-[:HARVESTED_FROM]->(ds)
                """,
                hash=content_hash,
                title=presentation.get('title', ''),
                url=presentation.get('url', ''),
                description=presentation.get('description', '')[:1000],
                author=presentation.get('author', ''),
                views=presentation.get('views', 0),
                search_query=presentation.get('search_query', ''),
                tags=presentation.get('tags', []),
                slide_count=presentation.get('slide_count', 0)
                )
                
                # Add category relationships based on search query
                category = self._categorize_presentation(presentation)
                if category:
                    session.run("""
                        MATCH (p:SlideSharePresentation {contentHash: $hash})
                        MERGE (c:Category {name: $category})
                        MERGE (p)-[:BELONGS_TO]->(c)
                    """, hash=content_hash, category=category)
                
                self.stats["saved_to_neo4j"] += 1
                
        except Exception as e:
            print(f"      ❌ Neo4j error: {str(e)[:50]}")
            self.stats["errors"] += 1
    
    def _categorize_presentation(self, presentation: dict) -> str:
        """Kategoriser præsentation baseret på indhold"""
        text = f"{presentation.get('title', '')} {presentation.get('description', '')} {presentation.get('search_query', '')}".lower()
        
        if any(kw in text for kw in ['cyber', 'security', 'threat', 'soc', 'incident', 'malware', 'ransomware']):
            return 'CYBERSECURITY'
        elif any(kw in text for kw in ['ai', 'artificial intelligence', 'machine learning', 'gpt', 'llm']):
            return 'ARTIFICIAL_INTELLIGENCE'
        elif any(kw in text for kw in ['cloud', 'azure', 'aws', 'migration']):
            return 'CLOUD_COMPUTING'
        elif any(kw in text for kw in ['strategy', 'roadmap', 'transformation', 'architecture']):
            return 'STRATEGY'
        elif any(kw in text for kw in ['compliance', 'gdpr', 'nis2', 'iso', 'governance']):
            return 'COMPLIANCE'
        elif any(kw in text for kw in ['osint', 'intelligence', 'investigation']):
            return 'THREAT_INTELLIGENCE'
        else:
            return 'GENERAL'
    
    def run(self):
        """Kør fuld harvest"""
        print("\n" + "=" * 60)
        print("📊 SLIDESHARE HARVESTER")
        print("=" * 60)
        print(f"Søgetermer: {len(self.SEARCH_TERMS)}")
        print(f"Output: {self.output_dir}")
        print("=" * 60)
        
        # Navigate to SlideShare
        self.driver.get(self.BASE_URL)
        time.sleep(3)
        
        # Handle any popups/cookies
        try:
            cookie_btn = self.driver.find_element(By.CSS_SELECTOR, '[data-testid="accept-cookies"], .accept-cookies, #onetrust-accept-btn-handler')
            cookie_btn.click()
            time.sleep(1)
        except:
            pass
        
        all_presentations = []
        
        # Search for each term
        for i, term in enumerate(self.SEARCH_TERMS, 1):
            print(f"\n🔍 [{i}/{len(self.SEARCH_TERMS)}] Søger: {term}")
            
            results = self.search_presentations(term, max_results=15)
            print(f"   Found {len(results)} presentations")
            
            for pres in results:
                # Avoid duplicates
                if not any(p['url'] == pres['url'] for p in all_presentations):
                    all_presentations.append(pres)
                    self.save_to_neo4j(pres)
                    print(f"   ✓ {pres['title'][:60]}...")
            
            # Small delay between searches
            time.sleep(2)
        
        # Save local JSON
        output_file = self.output_dir / "slideshare_harvest.json"
        with open(output_file, 'w', encoding='utf-8') as f:
            json.dump({
                "timestamp": datetime.now().isoformat(),
                "stats": self.stats,
                "presentations": all_presentations
            }, f, indent=2, ensure_ascii=False)
        
        # Summary
        print("\n" + "=" * 60)
        print("📊 HARVEST COMPLETE")
        print("=" * 60)
        print(f"   🔍 Searches performed:      {self.stats['searches']}")
        print(f"   📄 Presentations found:     {self.stats['presentations_found']}")
        print(f"   💾 Saved to Neo4j:          {self.stats['saved_to_neo4j']}")
        print(f"   ⚠️  Errors:                  {self.stats['errors']}")
        print(f"   📁 Output: {output_file}")
        print("=" * 60)
        
        self.driver.quit()
        self.neo4j.close()
        
        return all_presentations


if __name__ == "__main__":
    harvester = SlideShareHarvester()
    harvester.run()