Skip to content
This repository was archived by the owner on May 5, 2023. It is now read-only.

Commit d67a924

Browse files
committed
Merge branch 'develop'
2 parents ea4243a + 5bd8f40 commit d67a924

36 files changed

+398
-170
lines changed

MANIFEST.in

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
include LICENSE
2+
include README.md
3+
include requirements.txt
4+
5+
recursive-include django_covid19/spider *
6+
recursive-include docs *
7+
recursive-include demo *
8+
recursive-include demo_proj manage.py
9+
recursive-include demo_proj requirements.txt
10+
recursive-include demo_proj/ncov *
11+
12+
recursive-exclude demo_proj db.sqlite3
13+
recursive-exclude django_covid19/migrations *

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
<img src="https://img.shields.io/github/languages/top/leafcoder/django-covid19" data-origin="https://img.shields.io/github/languages/top/leafcoder/django-covid19" alt="GitHub top language">
2121
<img src="https://img.shields.io/github/languages/code-size/leafcoder/django-covid19" data-origin="https://img.shields.io/github/languages/code-size/leafcoder/django-covid19" alt="GitHub code size in bytes">
2222
<img src="https://img.shields.io/github/commit-activity/w/leafcoder/django-covid19" data-origin="https://img.shields.io/github/commit-activity/w/leafcoder/django-covid19" alt="GitHub commit activity">
23-
<img src="https://img.shields.io/github/downloads/leafcoder/django-covid19/total" data-origin="https://img.shields.io/github/downloads/leafcoder/django-covid19/total" alt="GitHub All Releases">
23+
<img src="https://img.shields.io/pypi/dm/django_covid19" data-origin="https://img.shields.io/pypi/dm/django_covid19" alt="PyPI - Downloads">
2424
</p>
2525

2626
</div>
@@ -36,14 +36,14 @@
3636
如果开发者本身没有个人的云服务器用来部署本项目,也可以直接调用本项目已部署好
3737
的实时接口,可用于科研、娱乐、教学等各方面。
3838

39-
[![在线文档](docs/images/docs.png)](http://111.231.75.86:8000/docs/)
39+
[![在线文档](https://raw.githubusercontent.com/leafcoder/django-covid19/master/docs/images/docs.png)](http://111.231.75.86:8000/docs/)
4040

4141
# 在线大屏
4242

4343
根据已部署的疫情在线接口,并结合使用开源数据大屏项目中的示例代码,本项目提
4444
供了一个使用本项目接口的数据大屏示例。
4545

46-
[![在线数据大屏](docs/images/dashboard.png)](http://111.231.75.86/dashboard)
46+
[![在线数据大屏](https://raw.githubusercontent.com/leafcoder/django-covid19/master/docs/images/dashboard.png)](http://111.231.75.86/dashboard)
4747

4848
# 问题相关
4949

manage.py renamed to demo_proj/manage.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
def main():
8-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'covid19.settings')
8+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ncov.settings')
99
try:
1010
from django.core.management import execute_from_command_line
1111
except ImportError as exc:
File renamed without changes.

covid19/settings.py renamed to demo_proj/ncov/settings.py

+10-20
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Django settings for covid19 project.
2+
Django settings for ncov project.
33
44
Generated by 'django-admin startproject' using Django 2.2.10.
55
@@ -37,26 +37,26 @@
3737
'django.contrib.sessions',
3838
'django.contrib.messages',
3939
'django.contrib.staticfiles',
40-
40+
# set by user
4141
'corsheaders',
4242
'django_crontab',
4343
'rest_framework',
4444
'django_filters',
45-
'ncovapi.apps.NcovapiConfig'
45+
'django_covid19'
4646
]
4747

4848
MIDDLEWARE = [
4949
'django.middleware.security.SecurityMiddleware',
5050
'django.contrib.sessions.middleware.SessionMiddleware',
51-
'corsheaders.middleware.CorsMiddleware',
51+
'corsheaders.middleware.CorsMiddleware', # 新增跨域部分
5252
'django.middleware.common.CommonMiddleware',
5353
'django.middleware.csrf.CsrfViewMiddleware',
5454
'django.contrib.auth.middleware.AuthenticationMiddleware',
5555
'django.contrib.messages.middleware.MessageMiddleware',
5656
'django.middleware.clickjacking.XFrameOptionsMiddleware',
5757
]
5858

59-
ROOT_URLCONF = 'covid19.urls'
59+
ROOT_URLCONF = 'ncov.urls'
6060

6161
TEMPLATES = [
6262
{
@@ -74,7 +74,7 @@
7474
},
7575
]
7676

77-
WSGI_APPLICATION = 'covid19.wsgi.application'
77+
WSGI_APPLICATION = 'ncov.wsgi.application'
7878

7979

8080
# Database
@@ -139,15 +139,15 @@
139139
CACHES = {
140140
'default': {
141141
'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
142-
'LOCATION': '/var/tmp/covid19_cache',
142+
'LOCATION': '/var/tmp/ncov_cache',
143143
'TIMEOUT': 3600,
144144
'OPTIONS': {
145145
'MAX_ENTRIES': 20000
146146
}
147147
}
148148
}
149149

150-
#跨域增加忽略
150+
# 跨域增加忽略
151151
CORS_ALLOW_CREDENTIALS = True
152152
CORS_ORIGIN_ALLOW_ALL = True
153153

@@ -175,22 +175,12 @@
175175
'Pragma',
176176
)
177177

178-
CRONTAB_LOCK_JOBS = True
179-
180178
# 静态文件目录
181179
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
182180

183-
# 日志文件目录
184-
LOGS_DIR = os.path.join(BASE_DIR, 'var', 'logs')
185-
if not os.path.exists(LOGS_DIR):
186-
os.makedirs(LOGS_DIR)
187-
188-
# 配置 Scrapy 命令完整路径
189-
SCRAPY_CMD = '~/.virtualenvs/django-covid19/bin/scrapy'
190-
191-
# Setting of Crontab
181+
CRONTAB_LOCK_JOBS = True
192182
CRONJOBS = (
193183
# 每分钟抓取一次
194-
('*/1 * * * *', 'ncovapi.cron.crawl_dxy', [], {}, '>> %s/crontab.log' % LOGS_DIR),
184+
('*/1 * * * *', 'django.core.management.call_command', ['crawl']),
195185
)
196186

covid19/urls.py renamed to demo_proj/ncov/urls.py

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
"""covid19 URL Configuration
1+
"""ncov URL Configuration
22
33
The `urlpatterns` list routes URLs to views. For more information please see:
44
https://docs.djangoproject.com/en/2.2/topics/http/urls/
@@ -19,17 +19,9 @@
1919
from django.conf import settings
2020
from django.conf.urls import url
2121

22-
import os
2322

2423
urlpatterns = [
2524
path('admin/', admin.site.urls),
26-
path('api/', include('ncovapi.urls', namespace='ncovapi')),
27-
url(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT}),
28-
url(r'^docs/$', serve, {
29-
'document_root': os.path.join(settings.BASE_DIR, 'docs'),
30-
'path': 'index.html'
31-
}),
32-
url(r'^docs/(?P<path>.*)$', serve, {
33-
'document_root': os.path.join(settings.BASE_DIR, 'docs')
34-
})
25+
path('api/', include('django_covid19.urls', namespace='django_covid19')),
26+
url(r'^static/(?P<path>.*)$', serve, {'document_root': settings.STATIC_ROOT})
3527
]

covid19/wsgi.py renamed to demo_proj/ncov/wsgi.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
WSGI config for covid19 project.
2+
WSGI config for ncov project.
33
44
It exposes the WSGI callable as a module-level variable named ``application``.
55
@@ -11,6 +11,6 @@
1111

1212
from django.core.wsgi import get_wsgi_application
1313

14-
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'covid19.settings')
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'ncov.settings')
1515

1616
application = get_wsgi_application()

demo_proj/requirements.txt

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
attrs==19.3.0
2+
Automat==20.2.0
3+
certifi==2020.4.5.1
4+
cffi==1.14.0
5+
chardet==3.0.4
6+
constantly==15.1.0
7+
cryptography==2.9
8+
cssselect==1.1.0
9+
Django==2.2.12
10+
django-cors-headers==3.2.1
11+
django-covid19 @ file:///home/zhanglei3/Desktop/django-covid19/dist/django_covid19-0.3.tar.gz
12+
django-crontab==0.7.1
13+
django-filter==2.2.0
14+
django-mysql==3.4.0
15+
djangorestframework==3.11.0
16+
drf-extensions==0.6.0
17+
hyperlink==19.0.0
18+
idna==2.9
19+
incremental==17.5.0
20+
lxml==4.5.0
21+
parsel==1.5.2
22+
Protego==0.1.16
23+
pyasn1==0.4.8
24+
pyasn1-modules==0.2.8
25+
pycparser==2.20
26+
PyDispatcher==2.0.5
27+
PyHamcrest==2.0.2
28+
pyOpenSSL==19.1.0
29+
pytz==2019.3
30+
queuelib==1.5.0
31+
schedule==0.6.0
32+
Scrapy==2.0.1
33+
scrapy-djangoitem==1.1.1
34+
service-identity==18.1.0
35+
six==1.14.0
36+
sqlparse==0.3.1
37+
Twisted==20.3.0
38+
urllib3==1.25.9
39+
w3lib==1.21.0
40+
zope.interface==5.1.0

django_covid19/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
default_app_config = 'django_covid19.apps.DjangoCovid19Config'

ncovapi/admin.py renamed to django_covid19/admin.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
from django.contrib import admin
2-
from django.conf import settings
32
from django.urls import reverse
43
from django.utils.html import format_html
4+
from django.utils.safestring import mark_safe
55

66
from . import models
7+
import json
78
# Register your models here.
89

910

@@ -22,11 +23,37 @@ class BaseAdmin(admin.ModelAdmin):
2223
class StatisticsAdmin(BaseAdmin):
2324

2425
list_display = (
25-
'globalStatistics', 'domesticStatistics', 'internationalStatistics',
26-
'modifyTime', 'createTime', 'crawlTime'
26+
'id', 'jsonGlobalStatistics', 'jsonDomesticStatistics',
27+
'jsonInternationalStatistics', 'modifyTime', 'crawlTime'
2728
)
2829
search_fields = ('crawlTime', 'modifyTime')
2930

31+
def jsonGlobalStatistics(self, obj):
32+
return self.to_json(obj.globalStatistics)
33+
jsonGlobalStatistics.short_description = '全球疫情'
34+
jsonGlobalStatistics.admin_order_field = 'globalStatistics'
35+
36+
def jsonDomesticStatistics(self, obj):
37+
return self.to_json(obj.domesticStatistics)
38+
jsonDomesticStatistics.short_description = '国内疫情'
39+
jsonDomesticStatistics.admin_order_field = 'domesticStatistics'
40+
41+
def jsonInternationalStatistics(self, obj):
42+
return self.to_json(obj.internationalStatistics)
43+
jsonInternationalStatistics.short_description = '国际疫情'
44+
jsonInternationalStatistics.admin_order_field = 'internationalStatistics'
45+
46+
def to_json(self, data):
47+
try:
48+
data = json.loads(data)
49+
except:
50+
return
51+
result = []
52+
for k, v in sorted(data.items()):
53+
result.append(format_html('{}: {}', k, v))
54+
return mark_safe(format_html(
55+
'<pre>{}</pre>', format_html('<br>'.join(result))))
56+
3057
@admin.register(models.City)
3158
class CityAdmin(BaseAdmin):
3259

django_covid19/apps.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from django.apps import AppConfig
2+
3+
4+
class DjangoCovid19Config(AppConfig):
5+
name = 'django_covid19'
6+
verbose_name = '新冠肺炎疫情'
7+
8+
def ready(self):
9+
import django_covid19.signals
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import sys
3+
import django_covid19
4+
5+
app_dir = os.path.dirname(django_covid19.__file__)
6+
sys.path.insert(0, os.path.join(app_dir, 'spider'))
7+
8+
from nCoV.spiders.dxy import DXYSpider
9+
from scrapy.crawler import CrawlerProcess
10+
from scrapy.utils.project import get_project_settings
11+
from django.core.management.base import BaseCommand
12+
13+
class Scraper:
14+
def __init__(self):
15+
settings_file_path = 'nCoV.settings'
16+
os.environ.setdefault('SCRAPY_SETTINGS_MODULE', settings_file_path)
17+
self.process = CrawlerProcess(get_project_settings())
18+
self.spider = DXYSpider
19+
20+
def run_spiders(self):
21+
self.process.crawl(self.spider)
22+
self.process.start()
23+
24+
class Command(BaseCommand):
25+
26+
help = 'Crawl data from DingXiangYuan.'
27+
28+
def handle(self, *args, **options):
29+
scraper = Scraper()
30+
scraper.run_spiders()
File renamed without changes.
File renamed without changes.
File renamed without changes.

django_covid19/settings.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.conf import settings
2+
3+
CACHE_PAGE_TIMEOUT = getattr(settings, 'CACHE_PAGE_TIMEOUT', 24*60*60)
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from django.dispatch import receiver
22
from django.core.cache import cache
33
from django.core.signals import request_finished
4-
4+
55
@receiver(request_finished)
6-
def my_callback(sender, **kwargs):
6+
def spider_callback(sender, **kwargs):
77
if cache.get('crawled'):
88
cache.clear()

django_covid19/spider/nCoV/__init__.py

Whitespace-only changes.

spider/nCoV/items.py renamed to django_covid19/spider/nCoV/items.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import scrapy
99

1010
from scrapy_djangoitem import DjangoItem
11-
from ncovapi import models
11+
from covid19 import models
1212

1313

1414
class StatisticsItem(DjangoItem):

spider/nCoV/pipelines.py renamed to django_covid19/spider/nCoV/pipelines.py

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import os
99
import json
1010
import sqlite3
11+
from uuid import uuid4
1112

1213
from django.core.cache import cache
1314

@@ -17,6 +18,8 @@
1718
class NcovPipeline(object):
1819

1920
def open_spider(self, spider):
21+
spider.object_id = uuid4().hex
22+
cache.set('running_spider_id', spider.object_id)
2023
spider.crawled = 0
2124

2225
def process_item(self, item, spider):
@@ -46,3 +49,4 @@ def process_item(self, item, spider):
4649

4750
def close_spider(self, spider):
4851
cache.set('crawled', spider.crawled)
52+
cache.delete('running_spider_id')

spider/nCoV/settings.py renamed to django_covid19/spider/nCoV/settings.py

-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
import os
4-
import sys
5-
6-
sys.path.insert(0, os.path.dirname(os.path.abspath('.')))
7-
8-
print(sys.path)
9-
os.environ['DJANGO_SETTINGS_MODULE'] = 'covid19.settings'
10-
11-
import django
12-
django.setup()
13-
143
# Scrapy settings for nCoV project
154
#
165
# For simplicity, this file contains only settings considered important or

0 commit comments

Comments
 (0)