├── .gitignore
├── README.md
├── static
└── style.css
├── templates
└── home.html
└── headlines.py
/.gitignore:
--------------------------------------------------------------------------------
1 | *.pyc
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 作者的挫作,《Python Linux系统管理与自动化运维》,敬请品鉴。
2 |
3 | 完整的目录在[这里](https://github.com/lalor/python_for_linux_system_administration)。
4 |
5 | 
6 |
--------------------------------------------------------------------------------
/static/style.css:
--------------------------------------------------------------------------------
1 | html {
2 | font-family: "Helvetica";
3 | background: white;
4 | }
5 | body {
6 | background: lightgrey;
7 | max-width: 900px;
8 | margin: 100 auto;
9 | }
10 | #header {
11 | padding-top: 5;
12 | background: lightsteelblue;
13 | }
14 | #inner-header {
15 | padding-left: 10;
16 | }
17 | #main{
18 | padding-left: 10;
19 | }
20 | input[type="text"], select {
21 | color: grey;
22 | border: 1px solid lightsteelblue;
23 | height: 30px;
24 | line-height:15px;
25 | margin: 2px 6px 16px 0px;
26 | }
27 | input[type="submit"] {
28 | padding: 5px 10px 5px 10px;
29 | color: black;
30 | background: lightsteelblue;
31 | border: none;
32 | box-shadow: 1px 1px 1px #4C6E91;
33 | }
34 | input[type="submit"]:hover{
35 | background: steelblue;
36 | }
37 |
--------------------------------------------------------------------------------
/templates/home.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
17 |
Current weather
18 |
22 |
23 |
城市: {{weather.city}}
24 |
{{weather.description}} |{{weather.temperature}}℃
25 |
26 |
30 |
31 | {% for article in articles %}
32 |
{{article.title}}
33 |
{{article.published}}
34 |
{{article.summary}}
35 |
36 | {% endfor %}
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/headlines.py:
--------------------------------------------------------------------------------
1 | #!/usr/bin/python
2 | # -*- coding: UTF-8 -*-
3 | from __future__ import unicode_literals
4 |
5 | import datetime
6 |
7 | import requests
8 | import feedparser
9 | from flask import Flask, render_template, request, make_response
10 |
11 |
12 | app = Flask(__name__)
13 |
14 | RSS_FEED = {"zhihu": "https://www.zhihu.com/rss",
15 | "netease": "http://news.163.com/special/00011K6L/rss_newsattitude.xml",
16 | "songshuhui": "http://songshuhui.net/feed",
17 | "ifeng": "http://news.ifeng.com/rss/index.xml"}
18 |
19 | DEFAULTS = {'city': '北京',
20 | 'publication': 'songshuhui'}
21 |
22 | WEATHERS = {"北京": 101010100,
23 | "上海": 101020100,
24 | "广州": 101280101,
25 | "深圳": 101280601}
26 |
27 |
28 | def get_value_with_fallback(key):
29 | if request.args.get(key):
30 | return request.args.get(key)
31 | if request.cookies.get(key):
32 | return request.cookies.get(key)
33 | return DEFAULTS[key]
34 |
35 |
36 | @app.route('/')
37 | def home():
38 | publication = get_value_with_fallback('publication')
39 | city = get_value_with_fallback('city')
40 |
41 | weather = get_weather(city)
42 | articles = get_news(publication)
43 |
44 | response = make_response(render_template('home.html', articles=articles,
45 | weather=weather))
46 |
47 | expires = datetime.datetime.now() + datetime.timedelta(days=365)
48 | response.set_cookie('publication', publication, expires=expires)
49 | response.set_cookie('city', city, expires=expires)
50 |
51 | return response
52 |
53 |
54 | def get_news(publication):
55 | feed = feedparser.parse(RSS_FEED[publication])
56 | return feed['entries']
57 |
58 |
59 | def get_weather(city):
60 | code = WEATHERS.get(city, 101010100)
61 | url = "http://www.weather.com.cn/data/sk/{0}.html".format(code)
62 |
63 | r = requests.get(url)
64 | r.encoding = 'utf-8'
65 |
66 | data = r.json()['weatherinfo']
67 | return dict(city=data['city'], temperature=data['temp'],
68 | description=data['WD'])
69 |
70 |
71 | if __name__ == '__main__':
72 | app.run(host='0.0.0.0', port=5000, debug=True)
73 |
--------------------------------------------------------------------------------