-
Notifications
You must be signed in to change notification settings - Fork 0
/
random_link.py
66 lines (44 loc) · 1.64 KB
/
random_link.py
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
import datetime
import random
import re
import bs4
import requests
SITEMAP_URL = 'http://pyvideo.org/sitemap.xml'
PATTERN = re.compile(r'^http://pyvideo.org/(?!speaker)(?!tag)(?!events)(?!pages).*/.+$')
def get_video_links():
response = requests.get(SITEMAP_URL)
soup = bs4.BeautifulSoup(response.content, 'lxml')
one_year_ago = (datetime.datetime.now() - datetime.timedelta(days=365)).date()
links = set()
for url in soup.find_all('url'):
loc = url.find('loc').string
if PATTERN.match(loc):
mod_string = url.find('lastmod').string
if datetime.datetime.strptime(mod_string, '%Y-%m-%d').date() > one_year_ago:
links.add(loc)
return links
def get_used_links(used_links_file):
try:
with open(used_links_file) as fp:
lines = fp.readlines()
return set(line.strip() for line in lines)
except FileNotFoundError:
return set()
def save_newly_used_links(used_links_file, newly_used_links):
with open(used_links_file, 'a') as fp:
for link in newly_used_links:
fp.write(link + '\n')
def main(used_links_file):
links = get_video_links()
used_links = get_used_links(used_links_file)
unused_links = links - used_links
newly_used_links = random.sample(unused_links, 3)
save_newly_used_links(used_links_file, newly_used_links)
for link in newly_used_links:
print(link)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('used_links_file', help='TXT with on link per line')
args = parser.parse_args()
main(args.used_links_file)