This repository was archived by the owner on Nov 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcast_utils.py
More file actions
91 lines (67 loc) · 2.47 KB
/
cast_utils.py
File metadata and controls
91 lines (67 loc) · 2.47 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
"""
Utilities for castbot
"""
import requests
import re
import feedparser
import random
from bs4 import BeautifulSoup
from http import cookiejar
# https://stackoverflow.com/questions/17037668/how-to-disable-cookie-handling-with-the-python-requests-library
class BlockAll(cookiejar.CookiePolicy):
"""
Blocks all cookies. Purpose is to stop websites from blocking requests.
"""
return_ok = set_ok = domain_return_ok = path_return_ok = lambda self, *args, **kwargs: False
netscape = True
rfc2965 = hide_cookie2 = False
ses = requests.Session()
ses.cookies.set_policy(BlockAll())
def get_info(podcast=-1):
"""
Reads the RSS feed and formats it as a Discord message
podcast: int
Episode number (defaults to ``-1`` / latest)
"""
feed = feedparser.parse("https://nisercast.gitlab.io/rss.xml")
# Always in last-in order
pods = feed["entries"]
pods.reverse()
try:
pod = pods[podcast]
except IndexError:
return None
pod_num = len(pods)
pod_title = pod.title
pod_published = pod.published
pod_longdesc = BeautifulSoup(pod.summary, "html.parser").get_text()
pod_shortdesc = re.search('(.+?)Episode Notes:', pod_longdesc).group(1)
return pod_num, pod_title, pod_published, pod_shortdesc
def get_links():
"""
Gets links to new episodes from various platforms
"""
# Google
google_show = r"https://podcasts.google.com/feed/aHR0cHM6Ly9uaXNlcmNhc3QuZ2l0bGFiLmlvL3Jzcy54bWw"
google_page = ses.get(google_show).text
google_soup = BeautifulSoup(google_page, "html.parser")
google_latest = google_show + "/episode/" + re.search(
"/episode/(.*?)\?", str(google_soup)).group(1)
# Spotify
spotify_show = r"https://open.spotify.com/show/6b9PbZU6siLA5tPE1m1Gve"
spotify_page = ses.get(spotify_show).text
spotify_soup = BeautifulSoup(spotify_page, "html.parser")
spotify_latest = re.search('spotify":"(.*?)"', str(spotify_soup)).group(1)
# Apple
apple_show = r"https://podcasts.apple.com/in/podcast/nisercast/id1561466001"
apple_page = ses.get(apple_show).text
apple_soup = BeautifulSoup(apple_page, "html.parser")
apple_latest = "https://podcasts.apple.com/in/podcast/id1561466001?i=" + re.search(
'\/id1561466001\?i=([0-9]+)"', str(apple_soup)).group(1)
links = [google_latest, spotify_latest, apple_latest]
return links
def rcol():
"""
Returns random RGB triplets
"""
return random.sample(range(255), 3)