PlanOpticon

planopticon / video_processor / sources / twitter_source.py
Blame History Raw 130 lines
1
"""Twitter/X source connector -- stub requiring auth or gallery-dl."""
2
3
import logging
4
from pathlib import Path
5
from typing import List, Optional
6
7
from video_processor.sources.base import BaseSource, SourceFile
8
9
logger = logging.getLogger(__name__)
10
11
12
class TwitterSource(BaseSource):
13
"""
14
Fetch Twitter/X posts and threads.
15
16
Twitter API v2 requires authentication. This connector attempts to use
17
gallery-dl as a fallback for public tweets.
18
19
Auth options:
20
- Set TWITTER_BEARER_TOKEN env var for API v2 access
21
- Install gallery-dl for scraping public tweets: pip install gallery-dl
22
"""
23
24
def __init__(self, url: str):
25
self.url = url
26
self._bearer_token: Optional[str] = None
27
28
def authenticate(self) -> bool:
29
"""Check for Twitter API token or gallery-dl availability."""
30
import os
31
32
self._bearer_token = os.environ.get("TWITTER_BEARER_TOKEN")
33
if self._bearer_token:
34
return True
35
36
# Check for gallery-dl fallback
37
try:
38
import gallery_dl # noqa: F401
39
40
logger.info("Using gallery-dl for Twitter content extraction")
41
return True
42
except ImportError:
43
pass
44
45
logger.error(
46
"Twitter source requires either:\n"
47
" 1. TWITTER_BEARER_TOKEN env var (Twitter API v2)\n"
48
" 2. gallery-dl installed: pip install gallery-dl\n"
49
"Twitter API access: https://developer.twitter.com/en/portal/dashboard"
50
)
51
return False
52
53
def list_videos(
54
self,
55
folder_id: Optional[str] = None,
56
folder_path: Optional[str] = None,
57
patterns: Optional[List[str]] = None,
58
) -> List[SourceFile]:
59
"""Return a single SourceFile for the tweet/thread."""
60
return [
61
SourceFile(
62
name=self.url.split("/")[-1] or "tweet",
63
id=self.url,
64
mime_type="text/plain",
65
)
66
]
67
68
def download(self, file: SourceFile, destination: Path) -> Path:
69
"""Download tweet content as text."""
70
destination = Path(destination)
71
destination.parent.mkdir(parents=True, exist_ok=True)
72
text = self.fetch_text()
73
destination.write_text(text, encoding="utf-8")
74
logger.info(f"Saved Twitter content to {destination}")
75
return destination
76
77
def fetch_text(self) -> str:
78
"""Extract tweet text via API or gallery-dl."""
79
if self._bearer_token:
80
return self._fetch_via_api()
81
82
try:
83
return self._fetch_via_gallery_dl()
84
except ImportError:
85
raise RuntimeError(
86
"No Twitter extraction method available. See authenticate() for setup."
87
)
88
89
def _fetch_via_api(self) -> str:
90
"""Fetch tweet via Twitter API v2."""
91
import re
92
93
import requests
94
95
match = re.search(r"/status/(\d+)", self.url)
96
if not match:
97
raise ValueError(f"Could not extract tweet ID from: {self.url}")
98
99
tweet_id = match.group(1)
100
resp = requests.get(
101
f"https://api.twitter.com/2/tweets/{tweet_id}",
102
headers={"Authorization": f"Bearer {self._bearer_token}"},
103
params={"tweet.fields": "author_id,created_at,text"},
104
timeout=15,
105
)
106
resp.raise_for_status()
107
data = resp.json().get("data", {})
108
return f"{data.get('text', '')}\n\nCreated: {data.get('created_at', 'unknown')}"
109
110
def _fetch_via_gallery_dl(self) -> str:
111
"""Use gallery-dl to extract tweet metadata."""
112
import json
113
import subprocess
114
115
result = subprocess.run(
116
["gallery-dl", "--dump-json", self.url],
117
capture_output=True,
118
text=True,
119
timeout=30,
120
)
121
if result.returncode != 0:
122
raise RuntimeError(f"gallery-dl failed: {result.stderr}")
123
124
items = json.loads(result.stdout)
125
texts = []
126
for item in items if isinstance(items, list) else [items]:
127
if isinstance(item, dict):
128
texts.append(item.get("content", item.get("text", str(item))))
129
return "\n\n".join(texts) if texts else "No text content extracted."
130

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button