soonmyeong2

Merge branch 'recommanded_module_test'

#-*- coding: utf-8 -*-
from datetime import datetime, timedelta
import re
class D_day:
def __init__(self, date_str):
self.date = date_str
def dday_calculate(self, date):
y_m_d = date.split("-")
return (datetime.now() - datetime(int(y_m_d[0]), int(y_m_d[1]), int(y_m_d[2]))).days
def date_calculate(self):
dates = re.findall(r'[0-9]{2,4}[.|-][0-9]{1,2}[.|-][0-9]{1,2}|~', self.date)
for i in range(len(dates)):
dates[i] = dates[i].replace(".", "-")
# 날짜가 없는 경우
if len(dates) == 0:
return "상시"
# XXX ~
if dates[-1] == "~":
dday = self.dday_calculate(dates[0])
if dday > 0:
return "진행중"
else:
return "준비중"
# XXX
if len(dates) == 1:
return str(dates[0])
# ~ XXX and XXX ~ XXX
if dates[-1] != "~":
dday = self.dday_calculate(dates[-1])
if dday < 0:
return "D"+str(dday)
else:
return "종료"
# except
return "상 시"
def update_date(self, date):
self.date = date
#### example
'''
a=D_day("2019.11.20")
print(a.date_calculate())
a.update_date("2019.11.21")
print(a.date_calculate())
a.update_date("~2019.11.30")
print(a.date_calculate())
a.update_date("2019.10.11~2019.11.27")
print(a.date_calculate())
a.update_date("2019.10.11~")
print(a.date_calculate())
'''
check_policy :
* hash_policy.csv를 읽어와서 RSA256해싱 결과 값으로 디비랑 달라진 값 찾음
* csv 갱신하려면 policy_hash_writer(policies) 메소드 쓰면 된다.
D_dayCalculator:
* 날자 계산하는 클래스 파일
db_updater:
* DB의 날자를 갱신해주는 파일, input 없이 실행만 시키면 된다.
push_messaging:
* push message를 보내는 파일
* 실행시켜서 input으로 카테고리 명을 띄어쓰기 없이 입력하면 해당 구독자에게 메세지 발생
** ex) c1_2c3_4c11_1
raw_preprocessing:
* raw data 전처리 파일
* D-day를 계산해서 새로운 컬럼으로 추가해주고, 카테고리 명을 str로 변환시킨다.
* 주소는 정규식과 그리디로 최초 url 발견시 리턴 없으면 None
* input 값은 raw를 json으로 변환 시킨 것
view_counter
* view_count_raw.json를 갱신 시킨다
* 날자별로 정책별 조회수를 전부 긁어와서 json으로 저장
* 날자별로 실행하면 그날의 조회수 조회 가능
raw_calculate_Dday ->
\ No newline at end of file
import hashlib
from firebase_admin import db
from firebase_admin import credentials
import firebase_admin
import csv
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://capstone-vip.firebaseio.com/'})
ref = db.reference()
policies = ref.get()
## compare dict
policy_dict = dict()
def policy_hash_writer(policies):
with open('./hash_policy.csv', 'w', encoding='utf-8', newline='') as csvfile:
wr = csv.writer(csvfile)
for policy in policies:
sha = hashlib.sha256()
sha.update(str(policy).encode('utf-8'))
wr.writerow([policy["Value"], sha.hexdigest()])
def policy_db_reader():
for policy in policies:
if policy["Value"] in policy_dict.keys():
sha = hashlib.sha256()
sha.update(str(policy).encode('utf-8'))
if policy_dict[policy["Value"]] != sha.hexdigest():
print("change contents : ", policy["Value"], ".", policy["Policy"])
else:
print("new policy : ", policy["Value"], ". ", policy["Policy"])
def policy_hash_reader():
with open('./hash_policy.csv', 'r', encoding='utf-8') as csvfile:
rdr = csv.reader(csvfile)
for r in rdr:
policy_dict[r[0]] = r[1]
#policy_hash_writer(policies)
policy_hash_reader()
policy_db_reader()
from firebase_admin import db
from firebase_admin import credentials
import firebase_admin
from D_dayCalculator import D_day
import json
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://capstone-vip.firebaseio.com/'})
ref = db.reference()
policies = ref.get()
date = D_day("1970.01.01")
#with open('view_count_raw.json', 'rt', encoding='utf-8') as json_file:
# view_count = json.load(json_file)
for policy in policies:
# 날짜 갱신
date.update_date(policy["Date"])
print(date.date_calculate())
ref.child(policy["Value"]).child("D_day").set(date.date_calculate())
import re
'''
test cast:
lines1 = "['mailto:psalms1273@merryyear.org', 'http://www.merryyear.org/abbs/?act=bbs&amp;subAct=view&amp;bid=Notice&amp;page=1&amp;order_index=no&amp;order_type=desc&amp;seq=1571']"
lines = "['https://www.shinhanhope.com/web/main.jsp']"
ll ="['https://www.childfund.or.kr/news/noticeView.do?bmTemplate=/inc/jsp/board/template/default&amp;bdId=20019410&amp;bmIds=10000023,10000097']"
l="http://bokjiro.go.kr/gowf/wel/welsvc/svcsearch/WelGvmtSvcSearchView.do?servId=WII00000124"
t = "https://welfare.gangdong.go.kr/site/contents/bokji/html00/html00/index3.html"
'''
def find_url_in_str(url) :
# regex = re.compile(r'(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?[^\'\]](\/|\/([\w#!:.?+=&%@!\-\/]))?', re.IGNORECASE)
regex = re.compile(r'https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?[^\'\]])?', re.IGNORECASE)
m = regex.search(url)
if m != None:
return m.group()
return None
find_url_in_str()
0,d24548533c984425a9a96cbd23e0085baed58a16d26d2764ee3e08f4ec9f2713
1,4f2e40e552527f48278fa2cdfadfa34e61c5a1672d82dc2ff42fcb4bff484cc2
2,7f41b893ec6ca567b928f9ba975c87e31db98a14fa6bc1c28431ab3a244cca43
3,fb6094dc6ba8c21846557664fda37a808f9877f35f301c444194b484e6cf38ae
4,121b23de7cf0d313077a208739b7a202ddfe6442c3f62dd2ed0cb475411c3bb8
5,26e05536f3fc6a844af4a746ecd070157180e4787fb122cf18b5dc8b91d2ff63
6,2809644d986b0226b080fdffbe7d3d1453095a12984101edbaefbf61821a8848
7,c7e3ac97575b1006424d0f2a391df327dc249067f0361d604355700bbe780102
8,3d7f2a439aa277c88101b08d2af865d6610f9791b614d9bbcce5a5f424736bb5
9,fba861e8ecc5f5411dec793409f8a98a96cbbb738fba74be397294e288dc7a4b
from firebase_admin import messaging
from firebase_admin import credentials
from firebase_admin import datetime
from firebase_admin import db
import firebase_admin
import json
import re
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://wello-topic.firebaseio.com/'})
ref = db.reference()
topics = list(ref.get())
pre_topic = list()
def send_to_topic(policy, topic):
message = messaging.Message(
android=messaging.AndroidConfig(
ttl=datetime.timedelta(seconds=3600),
priority='normal',
notification=messaging.AndroidNotification(
title='새 정책 알람',
body=policy,
icon='@drawable/mini',
color='#29ABE2',
sound='default'
),
),
data={
'score': '850',
'time': '2:45',
},
webpush=messaging.WebpushConfig(
notification=messaging.WebpushNotification(
title='웹 알림',
body='TEST',
icon='',
),
),
topic=topic,
)
response = messaging.send(message)
print('Successfully sent message:', response)
for topic in topics:
categorys = re.findall('c[0-9]+[_][0-9]+', topic)
for i in range(len(categorys) - 1, -1, -1):
if '1' == categorys[i].split('_')[-1]:
categorys.pop(i)
pre_topic.append(categorys)
print('new policy category : ', end = '')
policy = input()
policy_category = re.findall('c[0-9]+[_][0-9]+', policy)
policy_category_list = re.findall('c[0-9]+', policy)
push_index = list()
flag = True
for i, categorys in enumerate(pre_topic):
for category in categorys:
if category.split('_')[0] in policy_category_list:
if category not in policy_category:
flag = False
break
if flag: push_index.append(i)
flag = True
for push in push_index:
send_to_topic('[가정복지부] 기성이를 보유하신 가정에 지원금을 드립니다.', topics[push])
#-*- coding: utf-8 -*-
from datetime import datetime, timedelta
import json
import re
def find_url_in_str(url) :
regex = re.compile(r'https?://(\w*:\w*@)?[-\w.]+(:\d+)?(/([\w/_.]*(\?\S+)?)?[^\'\]])?', re.IGNORECASE)
m = regex.search(url)
if m != None:
return m.group()
return None
def dday_calculate(date):
y_m_d = date.split("-")
return (datetime.now() - datetime(int(y_m_d[0]), int(y_m_d[1]), int(y_m_d[2]))).days
def date_calculate(url):
dates = re.findall(r'[0-9]{2,4}[.|-][0-9]{1,2}[.|-][0-9]{1,2}|~', url)
for i in range(len(dates)):
dates[i] = dates[i].replace(".", "-")
# 날짜가 없는 경우
if len(dates) == 0:
return "상시"
# XXX ~
if dates[-1] == "~":
dday = dday_calculate(dates[0])
if dday > 0:
return "진행중"
else:
return "준비중"
# XXX
if len(dates) == 1:
return "상시"
# ~ XXX and XXX ~ XXX
if dates[-1] != "~":
dday = dday_calculate(dates[-1])
if dday < 0:
return "D"+str(dday)
else:
return "종료"
# except
return "상 시"
with open('db (2).json', 'rt', encoding='utf-8') as json_file:
json_data = json.load(json_file)
for js in json_data:
for key, value in js.items():
if type(value) != str and key != 'View':
js[key] = str(value)
if key == "Link":
js[key] = find_url_in_str(value)
# critical point
if key == "Keyword":
js[key] = "[" + value.replace("[", "").replace("]", "") + "]"
if key == "View":
js["D_day"] = date_calculate(js["Date"])
break
###############
with open('result1.json', 'w', encoding='utf-8') as make_file:
make_file.write('[')
for i, js in enumerate(json_data):
json.dump(js, make_file, indent="\t", ensure_ascii=False)
if i != len(json_data) - 1:
make_file.write(',')
make_file.write(']')
#-*- coding: utf-8 -*-
from datetime import datetime, timedelta
import json
import re
def dday_calculate(date):
y_m_d = date.split("-")
return (datetime.now() - datetime(int(y_m_d[0]), int(y_m_d[1]), int(y_m_d[2]))).days
def date_calculate(url):
dates = re.findall(r'[0-9]{2,4}[.|-][0-9]{1,2}[.|-][0-9]{1,2}|~', url)
for i in range(len(dates)):
dates[i] = dates[i].replace(".", "-")
# 날짜가 없는 경우
if len(dates) == 0:
return "상시"
# XXX ~
if dates[-1] == "~":
dday = dday_calculate(dates[0])
if dday > 0:
return "진행중"
else:
return "준비중"
# ~ XXX and XXX ~ XXX
if dates[-1] != "~":
dday = dday_calculate(dates[-1])
if dday < 0:
return "D"+str(dday)
else:
return "종료"
# except
return "상 시"
with open('d.json', 'rt', encoding='utf-8') as json_file:
json_data = json.load(json_file)
for js in json_data:
for key, value in js.items():
if type(value) != str and key != 'View':
js[key] = str(value)
if key == "Link" and value[0] == '[':
js[key] = value[1:-1]
# critical point
if key == "View":
js["D_day"] = date_calculate(js["Date"])
break
###############
with open('result.json', 'w', encoding='utf-8') as make_file:
make_file.write('[')
for i, js in enumerate(json_data):
json.dump(js, make_file, indent="\t", ensure_ascii=False)
if i != len(json_data) - 1:
make_file.write(',')
make_file.write(']')
This diff could not be displayed because it is too large.
{
"0": {
"2019-11-20": 27
},
"1": {
"2019-11-20": 3
},
"2": {
"2019-11-20": 26
},
"3": {
"2019-11-20": 4
},
"4": {
"2019-11-20": 3
},
"5": {
"2019-11-20": 4
},
"6": {
"2019-11-20": 7
},
"7": {
"2019-11-20": 3
},
"8": {
"2019-11-20": 4
},
"9": {
"2019-11-20": 22
},
"10": {
"2019-11-20": 0
},
"11": {
"2019-11-20": 0
},
"12": {
"2019-11-20": 0
},
"13": {
"2019-11-20": 0
},
"14": {
"2019-11-20": 0
},
"15": {
"2019-11-20": 0
},
"16": {
"2019-11-20": 0
},
"17": {
"2019-11-20": 0
},
"18": {
"2019-11-20": 0
},
"19": {
"2019-11-20": 0
},
"20": {
"2019-11-20": 0
},
"21": {
"2019-11-20": 0
},
"22": {
"2019-11-20": 0
},
"23": {
"2019-11-20": 0
},
"24": {
"2019-11-20": 0
},
"25": {
"2019-11-20": 0
},
"26": {
"2019-11-20": 0
},
"27": {
"2019-11-20": 0
},
"28": {
"2019-11-20": 0
},
"29": {
"2019-11-20": 0
},
"30": {
"2019-11-20": 0
},
"31": {
"2019-11-20": 0
},
"32": {
"2019-11-20": 0
},
"33": {
"2019-11-20": 0
},
"34": {
"2019-11-20": 0
},
"35": {
"2019-11-20": 0
},
"36": {
"2019-11-20": 0
},
"37": {
"2019-11-20": 0
},
"38": {
"2019-11-20": 0
},
"39": {
"2019-11-20": 0
},
"40": {
"2019-11-20": 0
},
"41": {
"2019-11-20": 0
},
"42": {
"2019-11-20": 0
},
"43": {
"2019-11-20": 0
},
"44": {
"2019-11-20": 0
},
"45": {
"2019-11-20": 0
},
"46": {
"2019-11-20": 0
},
"47": {
"2019-11-20": 0
},
"48": {
"2019-11-20": 0
},
"49": {
"2019-11-20": 0
},
"50": {
"2019-11-20": 0
},
"51": {
"2019-11-20": 0
},
"52": {
"2019-11-20": 0
},
"53": {
"2019-11-20": 0
},
"54": {
"2019-11-20": 0
},
"55": {
"2019-11-20": 0
},
"56": {
"2019-11-20": 0
},
"57": {
"2019-11-20": 0
},
"58": {
"2019-11-20": 0
},
"59": {
"2019-11-20": 0
},
"60": {
"2019-11-20": 0
},
"61": {
"2019-11-20": 0
},
"62": {
"2019-11-20": 0
},
"63": {
"2019-11-20": 0
},
"64": {
"2019-11-20": 0
},
"65": {
"2019-11-20": 0
},
"66": {
"2019-11-20": 0
},
"67": {
"2019-11-20": 0
},
"68": {
"2019-11-20": 0
},
"69": {
"2019-11-20": 0
},
"70": {
"2019-11-20": 0
},
"71": {
"2019-11-20": 0
},
"72": {
"2019-11-20": 0
},
"73": {
"2019-11-20": 0
},
"74": {
"2019-11-20": 0
},
"75": {
"2019-11-20": 0
},
"76": {
"2019-11-20": 0
},
"77": {
"2019-11-20": 0
},
"78": {
"2019-11-20": 0
},
"79": {
"2019-11-20": 0
},
"80": {
"2019-11-20": 0
},
"81": {
"2019-11-20": 0
},
"82": {
"2019-11-20": 0
},
"83": {
"2019-11-20": 0
},
"84": {
"2019-11-20": 0
},
"85": {
"2019-11-20": 0
},
"86": {
"2019-11-20": 0
},
"87": {
"2019-11-20": 0
},
"88": {
"2019-11-20": 0
},
"89": {
"2019-11-20": 0
},
"90": {
"2019-11-20": 1
},
"91": {
"2019-11-20": 0
},
"92": {
"2019-11-20": 0
},
"93": {
"2019-11-20": 0
},
"94": {
"2019-11-20": 0
},
"95": {
"2019-11-20": 0
},
"96": {
"2019-11-20": 0
},
"97": {
"2019-11-20": 0
},
"98": {
"2019-11-20": 0
},
"99": {
"2019-11-20": 0
},
"100": {
"2019-11-20": 0
},
"101": {
"2019-11-20": 0
},
"102": {
"2019-11-20": 0
},
"103": {
"2019-11-20": 0
},
"104": {
"2019-11-20": 0
},
"105": {
"2019-11-20": 0
},
"106": {
"2019-11-20": 0
},
"107": {
"2019-11-20": 0
},
"108": {
"2019-11-20": 0
},
"109": {
"2019-11-20": 0
},
"110": {
"2019-11-20": 0
},
"111": {
"2019-11-20": 0
},
"112": {
"2019-11-20": 0
},
"113": {
"2019-11-20": 0
},
"114": {
"2019-11-20": 0
},
"115": {
"2019-11-20": 0
},
"116": {
"2019-11-20": 0
},
"117": {
"2019-11-20": 0
},
"118": {
"2019-11-20": 0
},
"119": {
"2019-11-20": 0
},
"120": {
"2019-11-20": 0
},
"121": {
"2019-11-20": 0
},
"122": {
"2019-11-20": 0
},
"123": {
"2019-11-20": 0
},
"124": {
"2019-11-20": 0
},
"125": {
"2019-11-20": 0
},
"126": {
"2019-11-20": 0
},
"127": {
"2019-11-20": 0
},
"128": {
"2019-11-20": 0
},
"129": {
"2019-11-20": 0
},
"130": {
"2019-11-20": 0
},
"131": {
"2019-11-20": 0
},
"132": {
"2019-11-20": 0
},
"133": {
"2019-11-20": 0
},
"134": {
"2019-11-20": 0
},
"135": {
"2019-11-20": 0
},
"136": {
"2019-11-20": 0
},
"137": {
"2019-11-20": 0
},
"138": {
"2019-11-20": 0
},
"139": {
"2019-11-20": 0
},
"140": {
"2019-11-20": 0
},
"141": {
"2019-11-20": 1
},
"142": {
"2019-11-20": 0
},
"143": {
"2019-11-20": 0
}
}
\ No newline at end of file
from firebase_admin import db
from firebase_admin import credentials
import firebase_admin
import json
from datetime import datetime
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://capstone-vip.firebaseio.com/'})
ref = db.reference()
policies = ref.get()
## compare dict
json_data = dict()
today = str(datetime.now().year) + "-" + str(datetime.now().month) + "-" + str(datetime.now().day)
def view_count_raw_writer():
with open('./view_count_raw.json', 'w', encoding='utf-8') as make_file:
json.dump(json_data, make_file, indent="\t")
def count_update():
for policy in policies:
if policy["Value"] in json_data.keys():
if today in json_data[policy["Value"]].keys():
json_data[policy["Value"]][today] = abs(policy["View"])
else:
json_data[policy["Value"]][today] = dict()
json_data[policy["Value"]][today] = abs(policy["View"])
else:
json_data[policy["Value"]] = dict()
json_data[policy["Value"]][today] = abs(policy["View"])
def view_count_raw_reader():
with open('./view_count_raw.json', 'r', encoding='utf-8') as json_file:
global json_data
json_data = json.load(json_file)
view_count_raw_reader()
count_update()
print(json.dumps(json_data, indent="\t"))
view_count_raw_writer()