Skip to content

Commit 859a200

Browse files
committed
Update mylife.py for new website format
1 parent 45450ec commit 859a200

File tree

3 files changed

+100
-15
lines changed

3 files changed

+100
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fix youtube.py ISO time parse
1818
- Fix grammatical error in food sentence (beer)
1919
- Update youtube plugin to use proper contentRating API
20+
- Update mylife.py for website changes
2021

2122
## [1.3.0] 2020-03-17
2223
### Added

plugins/mylife.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99
fml_cache = []
1010

1111

12+
@hook.on_start()
1213
async def refresh_fml_cache(loop):
1314
""" gets a page of random FMLs and puts them into a dictionary """
14-
url = 'http://www.fmylife.com/random/'
15+
url = "http://www.fmylife.com/random/"
1516
_func = functools.partial(requests.get, url, timeout=6)
1617
request = await loop.run_in_executor(None, _func)
1718
soup = parse_soup(request.text)
1819

19-
for e in soup.find_all('p', {'class': 'block'}):
20-
# the /today bit is there to exclude fml news etc.
21-
a = e.find('a', {'href': re.compile('/article/today')})
22-
if not a:
23-
continue
24-
20+
# the /today bit is there to exclude fml news etc.
21+
articles = soup.find_all(
22+
"a", {"class": "article-link", "href": re.compile("/article/today")}
23+
)
24+
for a in articles:
2525
# the .html in the url must be removed before extracting the id
26-
fml_id = int(a['href'][:-5].split('_')[-1])
26+
fml_id = int(a["href"][:-5].split("_")[-1])
2727
text = a.text.strip()
2828

2929
# exclude lengthy submissions and FML photos
@@ -32,12 +32,6 @@ async def refresh_fml_cache(loop):
3232
fml_cache.append((fml_id, text))
3333

3434

35-
@hook.on_start()
36-
async def initial_refresh(loop):
37-
# do an initial refresh of the caches
38-
await refresh_fml_cache(loop)
39-
40-
4135
@hook.command(autohelp=False)
4236
async def fml(reply, loop):
4337
"""- gets a random quote from fmylife.com"""
@@ -48,7 +42,7 @@ async def fml(reply, loop):
4842
# grab the last item in the fml cache and remove it
4943
fml_id, text = fml_cache.pop()
5044
# reply with the fml we grabbed
51-
reply('(#{}) {}'.format(fml_id, text))
45+
reply("(#{}) {}".format(fml_id, text))
5246

5347
# refresh fml cache if its getting empty
5448
if len(fml_cache) < 3:

tests/plugin_tests/test_mylife.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import asyncio
2+
from unittest.mock import MagicMock
3+
4+
import pytest
5+
6+
from plugins import mylife
7+
8+
9+
@pytest.mark.asyncio()
10+
async def test_mylife(mock_requests):
11+
mock_requests.add(
12+
"GET",
13+
"http://www.fmylife.com/random/",
14+
body="""\
15+
<!DOCTYPE html>
16+
<html lang="us">
17+
<body>
18+
<main id="main">
19+
<div class="container site-content">
20+
<div id="content">
21+
<div class="row flex-block">
22+
<div class="col-sm-8">
23+
<div class="row">
24+
<article class="col-xs-12 article-panel">
25+
<div class="panel panel-classic">
26+
<div class="article-contents">
27+
<a class="article-link" href="/article/today-foo_155676.html">
28+
<span class="icon-piment"></span>&nbsp;
29+
Today, foo fml
30+
</a>
31+
</div>
32+
</div>
33+
</article>
34+
<article class="col-xs-12 article-panel">
35+
<div class="panel panel-classic">
36+
<div class="article-contents">
37+
<a class="article-link" href="/article/today-bar_132473.html">
38+
<span class="icon-piment"></span>&nbsp;
39+
Today, bar fml
40+
</a>
41+
</div>
42+
</div>
43+
</article>
44+
<article class="col-xs-12 article-panel">
45+
<div class="panel panel-classic">
46+
<div class="article-contents">
47+
<a class="article-link" href="/article/today-bar_132473.html">
48+
<span class="icon-piment"></span>&nbsp;
49+
Today, bar aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
50+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
51+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
52+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
53+
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
54+
aaaaaaaaaa fml
55+
</a>
56+
</div>
57+
</div>
58+
</article>
59+
<article class="col-xs-12 article-panel">
60+
<div class="panel panel-classic">
61+
<div class="article-contents">
62+
<a class="article-link" href="/article/today-bar_132473.html">
63+
<span class="icon-piment"></span>&nbsp;
64+
Today, bar
65+
</a>
66+
</div>
67+
</div>
68+
</article>
69+
<article class="col-xs-12 article-panel">
70+
<div class="panel panel-classic">
71+
<div class="article-contents">
72+
<a class="article-link" href="/article/news_142303.html">
73+
some news
74+
</a>
75+
</div>
76+
</div>
77+
</article>
78+
</div>
79+
</div>
80+
</div>
81+
</div>
82+
</main>
83+
</body>
84+
</html>""",
85+
)
86+
reply = MagicMock()
87+
loop = asyncio.get_event_loop()
88+
res = await mylife.fml(reply, loop)
89+
assert res is None
90+
reply.assert_called_with("(#132473) Today, bar fml")

0 commit comments

Comments
 (0)