本文整理汇总了Python中models.Feed.create方法的典型用法代码示例。如果您正苦于以下问题:Python Feed.create方法的具体用法?Python Feed.create怎么用?Python Feed.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Feed
的用法示例。
在下文中一共展示了Feed.create方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: add_feed
# 需要导入模块: from models import Feed [as 别名]
# 或者: from models.Feed import create [as 别名]
def add_feed(url=None):
# Get url submitted via AJAX
url = request.json['url']
FEED_TYPES = ('application/rss+xml',
'text/xml',
'application/atom+xml',
'application/x.atom+xml',
'application/x-atom+xml')
# Check if url already exists in feed DB
dupe = Feed.select().where(Feed.url == url).count()
if dupe > 0:
return jsonify(**DUPLICATE_FEED)
# Attempt to retrieve URL
try:
r = requests.get(url, timeout=5)
except requests.exceptions.Timeout:
return jsonify(**FEED_NOT_FOUND)
# check request status code
if r.status_code != requests.codes.ok:
return jsonify(**FEED_NOT_FOUND)
# Get Content-Type
contenttype = r.headers['content-type']
# If Content-type is RSS, add it directly
if contenttype in FEED_TYPES:
feed = Feed.create(url=url)
return jsonify(**STATUS_OK)
# If Content-type is HTML, pass to autodiscovery
if contenttype == 'text/html':
p = autodiscovery.Discover()
p.feed(r.text)
# check result in case of no feeds found
if len(p.feeds) == 0:
return jsonify(**FEED_NOT_FOUND)
else:
# TODO: Could loop over all available feeds found?
fulluri = p.feeds[0]['fulluri'] # just adds first feed found
feed = Feed.create(url=fulluri)
return jsonify(**STATUS_OK)
# dropped through to here, feed must be invalid
return jsonify(**FEED_INVALID)
示例2: add_feed
# 需要导入模块: from models import Feed [as 别名]
# 或者: from models.Feed import create [as 别名]
def add_feed(self, url, site_url = None, title = None, group = None):
"""Add a feed to the database"""
# existing feed?
try:
f = Feed.get(Feed.url == url)
except Feed.DoesNotExist:
f = Feed.create(url = url, title=title, site_url=site_url)
db.close()
return f
示例3: load_defaults
# 需要导入模块: from models import Feed [as 别名]
# 或者: from models.Feed import create [as 别名]
def load_defaults():
logging.info("Loading default feed entries into database %s..." % DB_FILE)
# Open defaults file
with open(DEFAULTS_FILE, 'r') as f:
rows = f.readlines()
f.close()
# Define database
db = SqliteDatabase(DB_FILE, threadlocals=True)
# Connect to database
db.connect()
# Iterate over default feeds list
# PSV format name|url|category
for row in rows:
(name, url, category) = row.split('|')
category = category.strip()
# Update Category table
c = Category.create(name=category, comment='Default category', order=1)
# Get Category insert id
cid = c.id
# Update Feeds table
f = Feed.create(name=name, version='', url=url, category=cid, favicon='', comment='Default feed', description='Default feed')
# Get Feed insert id
fid = f.id
# Get favicon for this Feed
# returns path to local favicon file, or None
# write to current feed record
logging.info("Getting favicon for %s" % f.url)
f.favicon = getFavicon(fid)
logging.info("Got favicon %s" % f.favicon)
# Save entry to feeds table
f.save()
logging.info("Default feeds loaded.")
示例4: opml_parse
# 需要导入模块: from models import Feed [as 别名]
# 或者: from models.Feed import create [as 别名]
def opml_parse():
UPLOAD_FOLDER = os.path.realpath('.') + '/static/uploads'
file = request.files['file']
if file and allowed_file(file.filename):
opml_filename = str(uuid.uuid4()) + '.xml' # use UUID as unique uploaded filename root
opml_path = os.path.join(UPLOAD_FOLDER, opml_filename)
file.save(opml_path)
print('OPML uploaded OK!')
# run Opml parser on uploaded file
o = Opml.OpmlReader(opml_path)
o.parseOpml()
print('OPML parsed OK!')
# Save categories to DB, skip invalid or duplicate feeds
for c in o.categories:
try:
cat = Category.create(name=c)
cat.save()
except IntegrityError:
pass
print('Categories added to DB!')
# Iterate over feeds found
for f in o.feeds:
print('------------')
print(f)
# Get corresponding Category id
cat_id = Category.get(Category.name == f['category']).id
if o.version == "1.0":
# Add feed from OPML version 1.0
# TODO: Exception handling
feed = Feed.create(name=f['text'], category=cat_id, version=f['type'], url=f['url'])
elif o.version == "1.1" or o.version == "2.0":
# Add feed from OPML version 1.1
# TODO: Exception handling
feed = Feed.create(name=f['title'], category=cat_id, version=f['type'], comment=f['text'],
description=f['description'], url=f['xmlUrl'])
else:
continue
# Add feed to DB, skip invalid or duplicate feeds
try:
feed.save()
except IntegrityError:
pass
print('Feeds added to DB!')
# return send_from_directory(UPLOAD_FOLDER, opml_filename) # Test returning uploaded OPML file
return redirect(url_for('index'), code=302)
return "<h1>Oops, something went wrong here...</h1>file extension is " + os.path.splitext(file.filename)[1]
示例5: print
# 需要导入模块: from models import Feed [as 别名]
# 或者: from models.Feed import create [as 别名]
cat = Category.create(name=c)
try:
cat.save()
except IntegrityError:
pass
# Iterate over feeds found
for f in o.feeds:
print('------------')
print(f)
# Get corresponding Category id
cat_id = Category.get(Category.name == f['category']).id
if o.version == "1.0":
# Add feed from OPML version 1.0
feed = Feed.create(name=f['text'], category=cat_id, version=f['type'], url=f['url'])
elif o.version == "1.1" or o.version == "2.0":
# Add feed from OPML version 1.1
feed = Feed.create(name=f['title'], category=cat_id, version=f['type'], comment=f['text'],
description=f['description'], url=f['xmlUrl'])
else:
continue
# Add feed to DB, skip invalid or duplicate feeds
try:
feed.save()
except IntegrityError:
pass