-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvxreddit.py
More file actions
282 lines (251 loc) · 12.4 KB
/
vxreddit.py
File metadata and controls
282 lines (251 loc) · 12.4 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
74
75
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
from flask import Flask, render_template, request, redirect, send_file, abort
from flask_cors import CORS
import config
import requests
import videoCombiner
import base64
import io
import urllib.parse
app = Flask(__name__)
CORS(app)
import os
from discordWorkaround import fixUrlForDiscord
embed_user_agents = [
"facebookexternalhit/1.1",
"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.57 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/1596241936; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"Mozilla/5.0 (Windows; U; Windows NT 10.0; en-US; Valve Steam Client/default/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/601.2.4 (KHTML, like Gecko) Version/9.0.1 Safari/601.2.4 facebookexternalhit/1.1 Facebot Twitterbot/1.0",
"facebookexternalhit/1.1",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; Valve Steam FriendsUI Tenfoot/0; ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36",
"Slackbot-LinkExpanding 1.0 (+https://api.slack.com/robots)",
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:38.0) Gecko/20100101 Firefox/38.0",
"Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)",
"TelegramBot (like TwitterBot)",
"Mozilla/5.0 (compatible; January/1.0; +https://gitlab.insrt.uk/revolt/january)",
"test"]
r_headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0"}
if os.environ.get('REDDIT_COOKIE') is not None:
r_headers['cookie'] = os.environ.get('REDDIT_COOKIE')
def message(text):
return render_template(
'message.html',
message=text,
appname=config.currentConfig["MAIN"]["appName"]
)
def getVideoFromPostURL(url):
url = url+".json"
response = requests.get(url,headers=r_headers)
if response.status_code != 200:
return None
resp=response.json()
post_info = resp[0]["data"]["children"][0]["data"]
post_responses = resp[1]["data"]["children"]
is_reply = len(post_responses) == 1 and 'body' in post_responses[0]['data'] and 'url' not in post_responses[0]['data'] and f"/{post_responses[0]['data']['id']}" in url
if is_reply:
newTitle = "RE: "+post_info['title']
post_info = post_responses[0]['data']
post_info['title'] = newTitle
# determine post type (video, image, text, gif, link, image gallery)
post_type = "unknown"
if "media_metadata" in post_info:
post_type = "gallery"
elif "url" in post_info and post_info["url"].endswith((".jpg",".png",".gif",".jpeg")):
post_type = "image"
elif ("is_video" in post_info and post_info["is_video"]) or ("post_hint" in post_info and post_info["post_hint"] == "hosted:video"):
post_type = "video"
elif "url_overridden_by_dest" in post_info:
post_type = "link"
elif "selftext" in post_info and post_info["selftext"] != "":
post_type = "text"
elif "body" in post_info and post_info["body"] != "":
post_type = "text"
if not post_info:
return None
vxData = {
"post_type": post_type,
"title": post_info["title"],
"author": post_info["author"],
"subreddit": post_info["subreddit_name_prefixed"],
"permalink": post_info["permalink"],
"upvotes": post_info["ups"],
"comments": 'num_comments' in post_info and post_info["num_comments"] or 0,
"awards": post_info["total_awards_received"],
"created": post_info["created_utc"],
"url": "https://www.reddit.com"+post_info["permalink"]
}
if (post_type == "video"):
hls_url = post_info["media"]["reddit_video"]["hls_url"]
base_url = "/".join(hls_url.split("/")[:-1]) + "/"
def get_final_media_url(m3u8_url):
response = requests.get(m3u8_url, headers=r_headers)
if response.status_code != 200:
return None
content = response.text
# non comments are usually filenames
lines = content.split('\n')
for line in reversed(lines):
if line.strip() and not line.startswith('#'):
if line.strip().endswith('.m3u8'):
return None
#return get_final_media_url(base_url + line.strip()) # we need to go deeper
return base_url + line.strip()
return None
# get playlist
playlist_response = requests.get(hls_url, headers=r_headers)
if playlist_response.status_code != 200:
return None
playlist_content = playlist_response.text
video_url = None
max_bandwidth = 0
selected_audio_group = None
video_m3u8_url = None
# find highest bandwidth video
lines = playlist_content.split('\n')
for i, line in enumerate(lines):
if '#EXT-X-STREAM-INF:' in line:
bandwidth = int(line.split('BANDWIDTH=')[1].split(',')[0])
if bandwidth > max_bandwidth:
max_bandwidth = bandwidth
# get audio group associated with this stream
if 'AUDIO=' in line:
selected_audio_group = line.split('AUDIO="')[1].split('"')[0]
video_stream = next(filter(lambda x: x.strip() and not x.startswith('#'),
lines[i+1:]))
video_m3u8_url = base_url + video_stream.strip()
# final video URL
if video_m3u8_url:
video_url = get_final_media_url(video_m3u8_url)
# find audio and get its final URL
audio_url = None
if selected_audio_group:
for line in lines:
if '#EXT-X-MEDIA:' in line and 'TYPE=AUDIO' in line and f'GROUP-ID="{selected_audio_group}"' in line:
uri = line.split('URI="')[1].split('"')[0]
audio_m3u8_url = base_url + uri
audio_url = get_final_media_url(audio_m3u8_url)
break
vxData["video_url"] = video_url
vxData["video_width"] = post_info["media"]["reddit_video"]["width"]
vxData["video_height"] = post_info["media"]["reddit_video"]["height"]
vxData["audio_url"] = audio_url
# get thumbnail
if 'preview' in post_info:
vxData["thumbnail_url"] = post_info["preview"]["images"][0]["source"]["url"].replace("&","&")
else:
vxData["thumbnail_url"] = post_info["thumbnail"]
elif (post_type == "image"):
vxData["images"] = [post_info["url"]]
# get thumbnail
vxData["thumbnail_url"] = post_info["thumbnail"]
elif (post_type == "gallery"):
vxData["images"] = []
for imageo in post_info["gallery_data"]["items"]:
postUrl = post_info["media_metadata"][imageo["media_id"]]["s"]["u"]
if postUrl.startswith("https://preview.redd.it"):
postUrl = postUrl.replace("https://preview.redd.it","https://i.redd.it")
vxData["images"].append(postUrl)
# get thumbnail
vxData["thumbnail_url"] = post_info["thumbnail"]
#elif (post_type == "link"):
#vxData["link_url"] = post_info["url_overridden_by_dest"]
# get thumbnail
#vxData["thumbnail_url"] = post_info["thumbnail"]
else:
body = ''
if 'selftext' in post_info:
body = post_info["selftext"]
elif 'body' in post_info:
body = post_info['body']
vxData["text"] = body
# get thumbnail
vxData["thumbnail_url"] = 'thumbnail' in post_info and post_info["thumbnail"] or ''
if vxData["text"] == "" and vxData["title"] != "":
vxData["text"] = post_info["title"]
if vxData["post_type"] == "link" and vxData["url"] != "":
url=vxData["url"]
vxData["text"] = f"【🌐 {url} 】\n\n"+vxData['text']
return vxData
def build_stats_line(post_info):
upvotes = post_info["upvotes"]
comments = post_info["comments"]
awards = post_info["awards"]
stats_line = f"⬆️ {upvotes} | 💬 {comments} | 🏆 {awards}"
return stats_line
@app.route('/redditvideo.mp4')
def get_video():
# get video_url and audio_url from query string
video_url = request.args.get('video_url')
audio_url = request.args.get('audio_url')
if video_url is None:
abort (400)
# check if video_url and audio_url are valid
if not video_url.startswith("https://v.redd.it/") or (audio_url is not None and not audio_url.startswith("https://v.redd.it/")):
abort (400)
if audio_url is None:
return redirect(video_url)
if config.currentConfig["MAIN"]["videoConversion"] == "local":
# combine video and audio into one file using ffmpeg
b64 = videoCombiner.generateVideo(video_url,audio_url)
# return video file
return send_file(io.BytesIO(base64.b64decode(b64)), mimetype='video/mp4')
else:
renderer=config.currentConfig["MAIN"]["videoConversion"]
# url encode video_url and audio_url
video_url = urllib.parse.quote(video_url, safe='')
audio_url = urllib.parse.quote(audio_url, safe='')
return redirect(f"{renderer}?video_url={video_url}&audio_url={audio_url}",code=307)
def embed_reddit(post_link,isDiscordBot=False):
videoInfo = getVideoFromPostURL(post_link)
if videoInfo is None:
return message("Failed to get data from Reddit")
statsLine = build_stats_line(videoInfo)
if videoInfo["post_type"] == "unknown":
return message("Unknown post type")
elif videoInfo["post_type"] == "text" or videoInfo["post_type"] == "link":
return render_template("text.html", vxData=videoInfo,appname=config.currentConfig["MAIN"]["appName"], statsLine=statsLine, domainName=config.currentConfig["MAIN"]["domainName"])
elif videoInfo["post_type"] == "image":
return render_template("image.html", vxData=videoInfo,appname=config.currentConfig["MAIN"]["appName"], statsLine=statsLine, domainName=config.currentConfig["MAIN"]["domainName"])
elif videoInfo["post_type"] == "gallery":
imageCount = str(len(videoInfo["images"]))
return render_template("image.html", vxData=videoInfo,appname=config.currentConfig["MAIN"]["appName"]+" - Gallery with "+imageCount+" image(s)", statsLine=statsLine, domainName=config.currentConfig["MAIN"]["domainName"])
#elif videoInfo["post_type"] == "link":
# return redirect(videoInfo["link_url"]) # this might need to be improved later
elif videoInfo["post_type"] == "video":
if videoInfo["audio_url"] is None:
convertedUrl = videoInfo["video_url"]
else:
encodedVideoURL = urllib.parse.quote(videoInfo["video_url"], safe='')
encodedAudioURL = urllib.parse.quote(videoInfo["audio_url"], safe='')
convertedUrl = "https://"+config.currentConfig["MAIN"]["domainName"]+"/redditvideo.mp4?video_url="+encodedVideoURL+"&audio_url="+encodedAudioURL
if isDiscordBot:
convertedUrl = fixUrlForDiscord(convertedUrl)
return render_template("video.html", vxData=videoInfo,appname=config.currentConfig["MAIN"]["appName"], statsLine=statsLine, domainName=config.currentConfig["MAIN"]["domainName"],mp4URL=convertedUrl)
else:
return videoInfo
@app.route('/')
def main():
return redirect(config.currentConfig["MAIN"]["repoURL"])
@app.route('/owoembed')
def alternateJSON():
return {
"author_name": request.args.get('text'),
"author_url": request.args.get('url'),
"provider_name": request.args.get('provider_name'),
"provider_url": config.currentConfig["MAIN"]["repoURL"],
"title": "Reddit",
"type": "link",
"version": "1.0"
}
@app.route('/<path:sub_path>')
def embedReddit(sub_path):
user_agent = request.headers.get('user-agent')
post_link = "https://www.reddit.com/" + sub_path
r = requests.get(post_link, allow_redirects=False, headers=r_headers)
if 'location' in r.headers and r.headers['location'].startswith("https"):
post_link = r.headers['location']
if "?" in post_link:
post_link = post_link.split("?")[0]
return embed_reddit(post_link,'Discordbot' in user_agent)
if __name__ == "__main__":
app.run(host='0.0.0.0')