Skip to content

Commit

Permalink
add an rss example
Browse files Browse the repository at this point in the history
  • Loading branch information
kneufeld committed Feb 27, 2019
1 parent 9806772 commit f6cfb69
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/pythonbytes/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# pythonbytes

Load an rss feed and then query it.

This example is read-only.

```
python examples/pythonbytes/rssfeed.py
```
2 changes: 2 additions & 0 deletions examples/pythonbytes/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alkali
feedparser
51 changes: 51 additions & 0 deletions examples/pythonbytes/rssfeed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import feedparser
import alkali
from alkali import fields

class RssLoader(alkali.Storage):

def __init__(self, url):
self.filename = url

def read(self, model_class):
feed = feedparser.parse(self.filename)
for item in feed['items']:
yield item


class Episode(alkali.Model):

class Meta:
storage = RssLoader('https://pythonbytes.fm/episodes/rss')

guid = fields.UUIDField(primary_key=True)
title = fields.StringField()
published = fields.DateTimeField()
itunes_episode = fields.IntField()
link = fields.StringField()
description = fields.StringField()

def __str__(self):
return f"<Epsiode {self.itunes_episode} - {self.title}>"

@property
def num(self):
return self.itunes_episode


db = alkali.Database(models=[Episode])
db.load()

# or directly but non-traditionally
# Episode.objects.load(Episode.Meta.storage)

print("last 10 episodes with 'python' in the title")
for ep in Episode.objects.filter(title__rei=r"\bpython\b").order_by('-published').limit(10):
print(' ', ep)

print("total episode count:", Episode.objects.count)

e = Episode.objects.get(itunes_episode=100)
print(e.title, e.published.date())

print("episode featuring alkali:", Episode.objects.get(title__rei='alkali').link)

0 comments on commit f6cfb69

Please sign in to comment.