-
Notifications
You must be signed in to change notification settings - Fork 1
/
feeds.py
93 lines (77 loc) · 2.87 KB
/
feeds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
import operator
import urllib
import datetime
from time import mktime
from urllib.parse import urlparse
from flask import flash, url_for, redirect,session
from bs4 import BeautifulSoup
import feedparser
from _config import relevant_days_for_feed
from app.models import Feed
from db import db
def relevant_feeds(user_id):
return db.session.query(Feed).filter_by(
user_id=user_id)
def parse_feeds(feeds):
# parse and mix feeds
results = []
for feed in feeds:
feed_data = feedparser.parse(feed.url)
for entry in feed_data.entries:
try:
published_date = datetime.datetime.fromtimestamp(mktime(entry.updated_parsed)).date()
except:
published_date=datetime.strptime('01011980', "%d%m%Y").date()
#published_date = u"לא ידוע"
summary = BeautifulSoup(entry.summary).get_text()
if datetime.datetime.now().date() - published_date <= datetime.timedelta(relevant_days_for_feed):
results.append([feed.name, entry.title, summary, urllib.parse.quote(entry.link),published_date.strftime('%d/%m/%Y')])
return sorted(results, key=operator.itemgetter(4), reverse=True)
def set_title_by_feed(url,**kwargs):
# return tuple of porject name and specific title for new feeds
project_name = get_project_by_feed_url(url)
try:
title = feedparser.parse(url).feed.title
except:
title=""
return (project_name,title)
def relevant_feeds(user_id=None):
if user_id == None:
user_id = session['user_id']
# return relevant feeds for user
return db.session.query(Feed).filter_by(
user_id=user_id)
def relevant_feeds_urls():
# return relevant feeds for user
return [x.url for x in db.session.query(Feed.url).filter_by(
user_id=session['user_id']).distinct()]
def get_project_by_feed_url(url):
# return project name (and subclass,if relevant, by feed address)
domain_first_part = url.split(".")[0]
if "opentaba" in domain_first_part:
return u'תב"ע פתוחה'
elif domain_first_part == u"kikar":
return u'כיכר המדינה'
else:
return u"לא ידוע"
def save_feed_to_db(url, name, project,user_id,relevant_feeds):
if url not in relevant_feeds:
title = set_title_by_feed(url)
try:
title = title[1].split(" ")
except:
pass
a_new_feed = Feed(
url=url,
name=name,
user_id=user_id,
project=project
)
db.session.add(a_new_feed)
db.session.commit()
flash(u'ההזנה החדשה נוספה למאגר')
return redirect(url_for('notifier.feeds_editor'))
else:
flash(u'את/ה כבר עוקבים אחרי מקור מידע זה')
return redirect(url_for('notifier.feeds_editor'))