Repository navigation
Expand file tree
/
Copy pathtwitter_scraper.py
More file actions
73 lines (62 loc) · 2.39 KB
/
Copy pathtwitter_scraper.py
File metadata and controls
73 lines (62 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
"""Uses Apify to scrape tweets, based on https://twitter.wordware.ai/"""
import json
import os
import requests
API_TOKEN = os.environ.get("APIFY_API_TOKEN")
headers = {"Content-Type": "application/json"}
def read_cache(username: str, is_tweet: bool = False):
cache_folder = os.path.join("./caches", username)
if os.path.exists(cache_folder):
filename = "tweets.json" if is_tweet else "profile.json"
with open(os.path.join(cache_folder, filename), "r", encoding="utf-8") as file:
return json.load(file)
return None
def scrape_profile(username: str):
if res := read_cache(username):
return res
data = {
"startUrls": [f"https://twitter.com/{username}"],
"twitterHandles": [username],
"getFollowers": True,
"getFollowing": True,
"getRetweeters": False,
"includeUnavailableUsers": False,
"maxItems": 5,
}
print(data)
url = f"https://api.apify.com/v2/acts/V38PZzpEgOfeeWvZY/run-sync-get-dataset-items?token={API_TOKEN}"
try:
response = requests.post(url, json=data, headers=headers)
print("Full Response:", response.status_code, response.json())
result = response.json()
return result
except requests.exceptions.RequestException as error:
print("Full Error:", error.response)
def scrape_tweets(username: str):
if res := read_cache(username, is_tweet=True):
return res
data = {
"startUrls": [
f"https://www.x.com/{username}",
],
"maxItems": 12,
"sort": "Latest",
"tweetLanguage": "en",
"customMapFunction": "(object) => { return {...object} }",
}
print(data)
url = f"https://api.apify.com/v2/acts/61RPP7dywgiy0JPD0/run-sync-get-dataset-items?token={API_TOKEN}"
try:
response = requests.post(url, json=data, headers=headers)
response.raise_for_status() # Raise an exception for HTTP errors
print("Actor run successfully:", json.dumps(response.json()))
result = response.json()
return result
except requests.exceptions.HTTPError as errh:
print("HTTP Error:", errh)
except requests.exceptions.ConnectionError as errc:
print("Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print("Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print("Error:", err)