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)
def new_feed(): feedurl = request.form['feed_url'] f = feedparser.parse(feedurl) if f.bozo == 1: return jsonify(status='FAIL') if 'title' not in f.feed: return jsonify(status='FAIL') title = f.feed.title try: description = f.feed.description except AttributeError: description = "" Feed.create( user=current_user.id, title=title, url=feedurl, description=description) return jsonify(status='OK')
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
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) except: log.error(tb()) return f
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.")
def new_feed(): if request.method == 'GET': return render_template('newfeed.html') # Si es POST es porque se ha enviado un formulario al servidor y espera # respuesta. else: # Obtiene del formulario la URL del feed ingresado. url = request.form.get('feed_url') # Ingreso al objeto User para agregarle los feeds asociados a su # cuenta. user = User.get(User.id == current_user.get_id()) # Verifico si la URL es correcta y caso contrario muestro mensaje # de error. Permanezco en newfeed. try: d = feedparser.parse(url) except: return render_template('newfeed.html', error_message='URL invalida') # Verifico que no exista ya el feed en los feeds del usuario para # que no hayan duplicados. Muestro el index. for feeds in user.feeds: if feeds.url == url: return render_template('index.html', message='El Feed ya esta agregado') # Es excluyente que tenga titulo pero no necesariamente descripcion. try: a = d.feed.title try: f = d.feed.description except AttributeError: f = '' # Almaceno el feed para el usuario en la db. feed = Feed.create(user=user, title=a, url=url, description=f) except: return render_template('newfeed.html', error_message='URL invalida') return redirect(url_for('index'))
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]
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()
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