A simple Python API for allpoetry.com.
Retrieve poems, authors and more in a few line of code.
This project is the continuation of allpoetryapi project from @jmbhughes. I simply clean up the code and updated some parts that were broken. I also changed the project structure, and added utilities functions.
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. See usage for notes on how to use the package.
allpoetry requires scraping libraries to work.
requests
beautiful soup 4
lxml
re
dateutil
To install the package, simply clone the repository and execute the setup.py
.
git clone https://github.com/arthurdjn/allpoetry
cd allpoetry
pip install .
Follow these notes to work with allpoetry package.
Connect to the python API to send your requests, and get the appropriate response.
from allpoetry import AllPoetry
# Optional, but recommended
USERNAME = "your_username"
PASSWORD = "your_password"
# Connect to the API
api = AllPoetry(USERNAME, PASSWORD)
Retrieve a poem from its url
.
url = "https://allpoetry.com/poem/8498499-Mirror-by-Sylvia-Plath"
poem = api.get_poem_from_url(url)
print(poem)
Mirror
¯¯¯¯¯¯
“I am silver and exact. I have no preconceptions.
Whatever I see I swallow immediately
Just as it is, unmisted by love or dislike.
I am not cruel, only truthful
The eye of a little god, four cornered.
Most of the time I meditate on the opposite wall.
It is pink, with speckles. I have looked at it so long
I think it is part of my heart. But it flickers.
Faces and darkness separate us over and over.
Now I am a lake. A woman bends over me,
Searching my reaches for what she really is.
Then she turns to those liars, the candles or the moon.
I see her back, and reflect it faithfully.
She rewards me with tears and an agitation of hands.
I am important to her. She comes and goes.
Each morning it is her face that replaces the darkness.
In me she has drowned a young girl, and in me an old woman
Rises toward her day after day, like a terrible fish.”
— Sylvia Plath
Likes: 229, Views: 355,300
You can retrieve the author with:
author = poem.author
print(author)
Sylvia-Plath
You can retrieve the title with:
title = poem.title
print(title)
Mirror
You can retrieve the text with:
text = poem.text
print(text)
I am silver and exact. I have no preconceptions.
Whatever I see I swallow immediately
Just as it is, unmisted by love or dislike.
# [...]
You can retrieve likes with:
likes = poem.likes
You can retrieve views with:
views = poem.views
You can retrieve tags with:
tags = poem.tags
You can of course retrieve all poems corresponding to an author.
The top_k
parameter is used to control the number of retrieved poems. If provided, you will get maximum top_k
poems otherwise all poems will be returned.
poems = api.get_poems(AUTHOR_NAME, top_k=100)
You can also retrieve famous authors, or users with:
authors = api.get_famous_authors(top_k=100)
author = api.get_author(AUTHOR_NAME)
To save your poems, you can simply use pickle
, a standard package for writing / loading binary files.
with open(f"{AUTHOR_NAME}.pkl", "wb") as file:
pickle.dump(poems, file)
- Add similar authors feature
- jsonify a poem
- retrieve a poem from its title and author (?)
Add famous authors feature✔️
- @arthurdjn - Updates and improvements
- Hat tip to @jmbhughes whose code was initially used