Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- Dockerfile +13 -0
- main.py +25 -0
- requirements.txt +5 -0
- scrape.py +65 -0
- templates/user.html +47 -0
Dockerfile
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.9
|
| 2 |
+
|
| 3 |
+
RUN useradd -m -u 1000 user
|
| 4 |
+
USER user
|
| 5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 6 |
+
|
| 7 |
+
WORKDIR /app
|
| 8 |
+
|
| 9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
| 10 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 11 |
+
|
| 12 |
+
COPY --chown=user . /app
|
| 13 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, Request, Form
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
from scrape import get_filtered_user_media
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
templates = Jinja2Templates(directory="templates")
|
| 8 |
+
|
| 9 |
+
@app.get("/", response_class=HTMLResponse)
|
| 10 |
+
def form(request: Request):
|
| 11 |
+
return templates.TemplateResponse("user.html", {"request": request})
|
| 12 |
+
|
| 13 |
+
@app.post("/scrape", response_class=HTMLResponse)
|
| 14 |
+
async def scrape_user(request: Request, username: str = Form(...)):
|
| 15 |
+
try:
|
| 16 |
+
user_data = get_filtered_user_media(username)
|
| 17 |
+
return templates.TemplateResponse("user.html", {
|
| 18 |
+
"request": request,
|
| 19 |
+
"user_data": user_data
|
| 20 |
+
})
|
| 21 |
+
except Exception as e:
|
| 22 |
+
return templates.TemplateResponse("user.html", {
|
| 23 |
+
"request": request,
|
| 24 |
+
"error": str(e)
|
| 25 |
+
})
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
python-dotenv
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
jinja2
|
| 5 |
+
python-multipart
|
scrape.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import http.client
|
| 2 |
+
import os
|
| 3 |
+
import json
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load environment variables
|
| 7 |
+
load_dotenv()
|
| 8 |
+
RAPIDAPI_KEY = os.getenv("RAPIDAPI_KEY")
|
| 9 |
+
|
| 10 |
+
def get_filtered_user_media(username: str) -> dict:
|
| 11 |
+
if not RAPIDAPI_KEY:
|
| 12 |
+
raise EnvironmentError("RAPIDAPI_KEY not found in environment variables")
|
| 13 |
+
|
| 14 |
+
conn = http.client.HTTPSConnection("twitter-api45.p.rapidapi.com")
|
| 15 |
+
headers = {
|
| 16 |
+
'x-rapidapi-key': RAPIDAPI_KEY,
|
| 17 |
+
'x-rapidapi-host': "twitter-api45.p.rapidapi.com"
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
endpoint = f"/usermedia.php?screenname={username}"
|
| 21 |
+
conn.request("GET", endpoint, headers=headers)
|
| 22 |
+
res = conn.getresponse()
|
| 23 |
+
|
| 24 |
+
if res.status != 200:
|
| 25 |
+
raise Exception(f"Request failed: {res.status} {res.reason}")
|
| 26 |
+
|
| 27 |
+
raw_data = res.read().decode("utf-8")
|
| 28 |
+
try:
|
| 29 |
+
json_data = json.loads(raw_data)
|
| 30 |
+
|
| 31 |
+
user_info = json_data.get("user", {})
|
| 32 |
+
timeline = json_data.get("timeline", [])
|
| 33 |
+
|
| 34 |
+
# Extract user-level info
|
| 35 |
+
filtered_data = {
|
| 36 |
+
"name": user_info.get("name"),
|
| 37 |
+
"screen_name": user_info.get("profile"),
|
| 38 |
+
"avatar": user_info.get("avatar"),
|
| 39 |
+
"blue_verified": user_info.get("blue_verified"),
|
| 40 |
+
"media_count": user_info.get("media_count"),
|
| 41 |
+
"posts": []
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Extract post-level info
|
| 45 |
+
for post in timeline:
|
| 46 |
+
post_data = {
|
| 47 |
+
"text": post.get("text"),
|
| 48 |
+
"media_url": None,
|
| 49 |
+
"created_at": post.get("created_at")
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
# Get the first media URL if available (photo or video)
|
| 53 |
+
media = post.get("media", {})
|
| 54 |
+
if "photo" in media:
|
| 55 |
+
post_data["media_url"] = media["photo"][0]["media_url_https"]
|
| 56 |
+
elif "video" in media:
|
| 57 |
+
post_data["media_url"] = media["video"][0]["variants"][0]["url"]
|
| 58 |
+
|
| 59 |
+
filtered_data["posts"].append(post_data)
|
| 60 |
+
|
| 61 |
+
return filtered_data
|
| 62 |
+
|
| 63 |
+
except json.JSONDecodeError:
|
| 64 |
+
raise ValueError("Invalid JSON response from API")
|
| 65 |
+
|
templates/user.html
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html>
|
| 3 |
+
<head>
|
| 4 |
+
<title>Twitter Scraper</title>
|
| 5 |
+
<style>
|
| 6 |
+
body { font-family: Arial; margin: 2rem; }
|
| 7 |
+
.profile { margin-bottom: 2rem; }
|
| 8 |
+
.tweet { margin-bottom: 1.5rem; border-bottom: 1px solid #ccc; padding-bottom: 1rem; }
|
| 9 |
+
img.avatar { border-radius: 50%; height: 80px; }
|
| 10 |
+
img.media { max-width: 400px; display: block; margin-top: 0.5rem; }
|
| 11 |
+
</style>
|
| 12 |
+
</head>
|
| 13 |
+
<body>
|
| 14 |
+
<h1>Twitter Media Scraper</h1>
|
| 15 |
+
|
| 16 |
+
<form method="post" action="/scrape">
|
| 17 |
+
<input type="text" name="username" placeholder="Enter Twitter username" required>
|
| 18 |
+
<button type="submit">Fetch</button>
|
| 19 |
+
</form>
|
| 20 |
+
|
| 21 |
+
{% if error %}
|
| 22 |
+
<p style="color: red;">Error: {{ error }}</p>
|
| 23 |
+
{% endif %}
|
| 24 |
+
|
| 25 |
+
{% if user_data %}
|
| 26 |
+
<div class="profile">
|
| 27 |
+
<h2>{{ user_data.name }} (@{{ user_data.screen_name }})</h2>
|
| 28 |
+
<img class="avatar" src="{{ user_data.avatar }}" alt="avatar">
|
| 29 |
+
<p>Blue Verified: {{ user_data.blue_verified }}</p>
|
| 30 |
+
<p>Total Media Count: {{ user_data.media_count }}</p>
|
| 31 |
+
</div>
|
| 32 |
+
|
| 33 |
+
<div class="tweets">
|
| 34 |
+
<h3>Tweets with Media</h3>
|
| 35 |
+
{% for post in user_data.posts %}
|
| 36 |
+
<div class="tweet">
|
| 37 |
+
<p><strong>{{ post.created_at }}</strong></p>
|
| 38 |
+
<p>{{ post.text }}</p>
|
| 39 |
+
{% if post.media_url %}
|
| 40 |
+
<img class="media" src="{{ post.media_url }}" alt="media">
|
| 41 |
+
{% endif %}
|
| 42 |
+
</div>
|
| 43 |
+
{% endfor %}
|
| 44 |
+
</div>
|
| 45 |
+
{% endif %}
|
| 46 |
+
</body>
|
| 47 |
+
</html>
|