-
Notifications
You must be signed in to change notification settings - Fork 4
/
kuaci.py
145 lines (104 loc) · 3.82 KB
/
kuaci.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import re
import pandas as pd
import requests
from datetime import datetime
from furl import furl
df_kodewilayah = pd.read_csv('kodewilayah.csv', index_col='kodewilayah')
def facebook_search(name, key, em=True):
f = furl('https://www.googleapis.com/customsearch/v1')
f.args['key'] = key
f.args['cx'] = '013743239855164640998:wdyn0oqb1ia'
f.args['q'] = name
if em:
f.args['exactTerms'] = name
f.args['siteSearch'] = 'www.facebook.com'
ret = requests.get(f.url)
result = ret.json()
return ret.json()['queries']['request'][0]['totalResults']
def instagram_search(name, key, em=True):
f = furl('https://www.googleapis.com/customsearch/v1')
f.args['key'] = key
f.args['cx'] = '013743239855164640998:wdyn0oqb1ia'
f.args['q'] = name
if em:
f.args['exactTerms'] = name
f.args['siteSearch'] = 'www.instagram.com'
ret = requests.get(f.url)
result = ret.json()
return ret.json()['queries']['request'][0]['totalResults']
def google_search(name, key, em=True):
f = furl('https://www.googleapis.com/customsearch/v1')
f.args['key'] = key
f.args['cx'] = '013743239855164640998:wdyn0oqb1ia'
f.args['q'] = name
if em:
f.args['exactTerms'] = name
ret = requests.get(f.url)
result = ret.json()
return ret.json()['queries']['request'][0]['totalResults']
def validator(ktp):
if len(str(ktp)) != 16:
return False
else:
return True
def locator(kodewilayah):
kodewilayah = int(kodewilayah)
try:
df_tmp = df_kodewilayah.loc[kodewilayah]
valid = True
provinsi = df_tmp['provinsi']
kabupatenkota = df_tmp['kabupatenkota']
kecamatan = df_tmp['kecamatan']
except KeyError as error:
valid = False
provinsi = ''
kabupatenkota = ''
kecamatan = ''
finally:
return pd.Series([valid, provinsi, kabupatenkota, kecamatan])
def gender_checker(gender_num):
gender_num = int(gender_num)
if gender_num in [0, 1, 2, 3]:
return 'Pria'
elif gender_num in [4, 5, 6]:
return 'Wanita'
else:
return 'Not Valid'
def dob_checker(dob):
dob = int(dob)
if dob > 400000:
dob = dob - 400000
dob = str(dob)
try:
dateformat_dob = datetime.strptime(dob, '%d%m%y')
except:
dateformat_dob = 'Not Valid'
return dateformat_dob
def kuaci_ktp_check(df_ktp):
if not isinstance(df_ktp, pd.DataFrame):
raise Exception('Input is not a pandas dataframe')
if len(df_ktp.columns > 1):
raise Exception('Input columns more than 1')
df_ktp['ktp'] = df_ktp[df_ktp.columns[0]]
if df_ktp.columns[0] != 'ktp':
del df_ktp[df_ktp.columns[0]]
df_ktp['valid'] = df_ktp['ktp'].apply(validator)
df_ktp['kodewilayah'] = df_ktp['ktp'].astype(str).str[:6]
df_ktp[['location_valid', 'provinsi', 'kabupatenkota', 'kecamatan']] = df_ktp['kodewilayah'].apply(locator)
df_ktp['gender'] = df_ktp['ktp'].astype(str).str[6:7].apply(gender_checker)
df_ktp['dob'] = df_ktp['ktp'].astype(str).str[6:12].apply(dob_checker)
return df_ktp
def kuaci_ktp_check_single(ktp_number):
if not isinstance(ktp_number, int):
raise Exception('Input is not an integer')
data = [ktp_number]
df_ktp = pd.DataFrame(data, columns=['KTP'])
df_ktp['ktp'] = df_ktp[df_ktp.columns[0]]
if df_ktp.columns[0] != 'ktp':
del df_ktp[df_ktp.columns[0]]
df_ktp['valid'] = df_ktp['ktp'].apply(validator)
df_ktp['kodewilayah'] = df_ktp['ktp'].astype(str).str[:6]
df_ktp[['location_valid', 'provinsi', 'kabupatenkota', 'kecamatan']] = df_ktp['kodewilayah'].apply(locator)
df_ktp['gender'] = df_ktp['ktp'].astype(str).str[6:7].apply(gender_checker)
df_ktp['dob'] = df_ktp['ktp'].astype(str).str[6:12].apply(dob_checker)
return df_ktp