mirror of
https://github.com/Alvin-Zilverstand/Twitter-X-Media-Batch-Downloader.git
synced 2026-12-02 19:11:28 +01:00
Update GetMetadata.py
This commit is contained in:
1 parent
f6a2986dbf
commit
72fb45fc8a
1 file changed
+50
-54
+50
-54
@@ -1,75 +1,71 @@
|
|||||||
import json
|
import json
|
||||||
import subprocess
|
import re
|
||||||
import sys
|
|
||||||
import os
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
from gallery_dl.extractor import twitter
|
||||||
|
|
||||||
def get_metadata(username):
|
def get_metadata(username):
|
||||||
if sys.platform == 'win32':
|
|
||||||
sys.stdout.reconfigure(encoding='utf-8')
|
|
||||||
|
|
||||||
url = f"https://x.com/{username}/timeline"
|
url = f"https://x.com/{username}/timeline"
|
||||||
|
|
||||||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
match = re.match(twitter.TwitterTimelineExtractor.pattern, url)
|
||||||
|
if not match:
|
||||||
|
raise ValueError(f"Invalid URL: {url}")
|
||||||
|
|
||||||
|
extractor = twitter.TwitterTimelineExtractor(match)
|
||||||
|
|
||||||
cookies_path = os.path.join(current_dir, 'cookies.json')
|
extractor.config = lambda key, default=None: {
|
||||||
output_path = os.path.join(current_dir, f'{username}.json')
|
"cookies": {
|
||||||
gallery_dl_path = os.path.join(current_dir, 'gallery-dl.exe')
|
"auth_token": ""
|
||||||
|
|
||||||
config = {
|
|
||||||
"extractor": {
|
|
||||||
"twitter": {
|
|
||||||
"cookies": {
|
|
||||||
"auth_token": ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}.get(key, default)
|
||||||
|
|
||||||
with open(cookies_path, 'w') as f:
|
extractor.initialize()
|
||||||
json.dump(config, f)
|
|
||||||
|
|
||||||
result = subprocess.run([gallery_dl_path, '-j', '--config', cookies_path, url],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
encoding='utf-8',
|
|
||||||
check=True)
|
|
||||||
|
|
||||||
os.remove(cookies_path)
|
|
||||||
|
|
||||||
data = json.loads(result.stdout)
|
|
||||||
output = {
|
output = {
|
||||||
'account_info': {},
|
'account_info': {},
|
||||||
|
'total_urls': 0,
|
||||||
'timeline': []
|
'timeline': []
|
||||||
}
|
}
|
||||||
|
|
||||||
for item in data:
|
for item in extractor:
|
||||||
if isinstance(item, list) and len(item) > 2 and isinstance(item[2], dict):
|
if isinstance(item, tuple) and len(item) >= 3:
|
||||||
if not output['account_info'] and 'user' in item[2]:
|
media_url = item[1]
|
||||||
user = item[2]['user']
|
tweet_data = item[2]
|
||||||
output['account_info'] = {
|
|
||||||
'date': user.get('date', ''),
|
if not output['account_info']:
|
||||||
'followers_count': user.get('followers_count', ''),
|
if 'user' in tweet_data:
|
||||||
'friends_count': user.get('friends_count', ''),
|
user = tweet_data['user']
|
||||||
'name': user.get('name', ''),
|
user_date = user.get('date', '')
|
||||||
'nick': user.get('nick', ''),
|
if isinstance(user_date, datetime):
|
||||||
'profile_image': user.get('profile_image', ''),
|
user_date = user_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
'statuses_count': user.get('statuses_count', '')
|
|
||||||
}
|
output['account_info'] = {
|
||||||
if isinstance(item[1], str):
|
'name': user.get('name', ''),
|
||||||
url = item[1]
|
'nick': user.get('nick', ''),
|
||||||
if ('pbs.twimg.com' in url and 'format=jpg&name=orig' in url) or 'video.twimg.com' in url:
|
'date': user_date,
|
||||||
tweet_date = item[2].get('date')
|
'followers_count': user.get('followers_count', 0),
|
||||||
if not tweet_date:
|
'friends_count': user.get('friends_count', 0),
|
||||||
tweet_date = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
'profile_image': user.get('profile_image', ''),
|
||||||
|
'statuses_count': user.get('statuses_count', 0)
|
||||||
output['timeline'].append({
|
}
|
||||||
'url': url,
|
|
||||||
'date': tweet_date
|
if 'pbs.twimg.com' in media_url or 'video.twimg.com' in media_url:
|
||||||
})
|
tweet_date = tweet_data.get('date', datetime.now())
|
||||||
|
if isinstance(tweet_date, datetime):
|
||||||
|
tweet_date = tweet_date.strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
output['timeline'].append({
|
||||||
|
'url': media_url,
|
||||||
|
'date': tweet_date
|
||||||
|
})
|
||||||
|
output['total_urls'] += 1
|
||||||
|
|
||||||
|
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
output_file = os.path.join(current_dir, f"{username}.json")
|
||||||
|
|
||||||
with open(output_path, 'w', encoding='utf-8') as f:
|
with open(output_file, 'w', encoding='utf-8') as f:
|
||||||
json.dump(output, f, ensure_ascii=False, indent=2)
|
json.dump(output, f, ensure_ascii=False, indent=2)
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
username = ""
|
username = ""
|
||||||
get_metadata(username)
|
get_metadata(username)
|
||||||
Reference in new issue
Block a user