File size: 1,246 Bytes
d9ce3dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from operator import le
from matplotlib.cbook import violin_stats
import streamlit as st
from fake_useragent import UserAgent
import requests #doenload json, mp4
import json

from telegram import Video #json parsing

ua = UserAgent()

st.title("Reddit Video Downloader")
reddit_url = st.text_input(label="Enter your reddit url here")

if reddit_url:
    if reddit_url[len(reddit_url)-1] == "/":
        json_url = reddit_url[:len(reddit_url)-1] + ".json"
    else:
        json_url = reddit_url + '.json'

    headers = {'User-Agent':str(ua.random)}

    json_response = requests.get(json_url, headers=headers)

    st.write(json_response.status_code)
    # st.write(headers)

    if json_response.status_code != 200:
        st.warning("Error Detected, check the URLl!!")
    else:
        mp4_url = json_response.json()[0]['data']['children'][0]['data']['secure_media']['reddit_video']['fallback_url']
        st.write(mp4_url)

    with st.spinner("Waiting to download the video"):
        mp4_response = requests.get(mp4_url,headers=headers )

        if mp4_response.status_code == 200:
            st.video(mp4_response.content)
        else:
            st.warning("Video Download Failed")

else:
    st.error("Enter a correct reddit url")