This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
/
web_dilingkari.py
150 lines (123 loc) · 4.93 KB
/
web_dilingkari.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
146
147
148
149
150
__author__ = 'oonarfiandwi'
from flask import Flask, request
from google.appengine.api import urlfetch, memcache
from os import path, environ
from model_profile import Profile
from google.appengine.ext import ndb
import jinja2
import urllib
import simplejson as json
import logging
app = Flask(__name__)
app.config['DEBUG'] = True
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(path.join(path.dirname(__file__), 'templates')),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
"""
you should consider to deploy this to other project (instance),
change this URL to your own project URL
DB_INSTANCE environment variable defined at app.yaml
beware that WEB_INSTANCE is multiple domain name
"""
DB_PROFILE = 'http://' + environ['DB_INSTANCE'] + '/profile'
DB_INDONESIA = 'http://' + environ['DB_INSTANCE'] + '/indonesia'
DB_VERIFIED_INDONESIA = 'http://' + environ['DB_INSTANCE'] + '/verified_indonesia'
UPDATE_DB_INDONESIA = 'http://' + environ['DB_INSTANCE'] + '/update_db_indonesia'
@app.route('/')
@app.route('/index')
def index():
nitems = '21'
page = request.args.get('p', '1')
output_str = 'yang aktif di Google+'
form_fields = {'nitems': nitems, 'page': page}
form_data = urllib.urlencode(form_fields)
users_data = {}
# try to load data from memcache
users_json = memcache.get('users_content:%s:%s' % (nitems, page))
if users_json is None:
result = urlfetch.fetch(url=DB_INDONESIA, payload=form_data, method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'}, deadline=60)
if result.status_code == 200:
users_data = json.loads(result.content)
if not memcache.add('users_content:%s:%s' % (nitems, page), result.content, 300): # 300 seconds
logging.error('Memcache set failed: users_content:%s:%s' % (nitems, page))
else:
users_data = json.loads(users_json)
count = 0
users = []
if users_data:
count_indonesia = long(users_data['total_data'])
if count_indonesia > long(nitems):
count = 1 + int(count_indonesia / long(nitems))
users = users_data['paging_data']
template_values = {
'body_text': output_str,
'users': users,
'page': int(page),
'last_page': count,
'base_url': request.base_url,
'url_root': request.url_root
}
template = JINJA_ENVIRONMENT.get_template('index.html')
return template.render(template_values)
@app.route('/profile/<profile_id>')
def profile(profile_id):
form_fields = {'id': profile_id}
form_data = urllib.urlencode(form_fields)
result = urlfetch.fetch(url=DB_PROFILE, payload=form_data, method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'}, deadline=60)
activity_data = ''
if result.status_code == 200:
activity_data = result.content
template_values = {
'id': profile_id,
'body_text': '',
'base_url': request.base_url
}
template = JINJA_ENVIRONMENT.get_template('profile.html')
return template.render(template_values)
@app.route('/verified')
def verified():
nitems = '21'
page = request.args.get('p', '1')
output_str = 'yang terverifikasi di Google+'
form_fields = {'nitems': nitems, 'page': page}
form_data = urllib.urlencode(form_fields)
users_data = {}
# try to load data from memcache
users_json = memcache.get('verified_content:%s:%s' % (nitems, page))
if users_json is None:
result = urlfetch.fetch(url=DB_VERIFIED_INDONESIA, payload=form_data, method=urlfetch.POST,
headers={'Content-Type': 'application/x-www-form-urlencoded'}, deadline=60)
if result.status_code == 200:
users_data = json.loads(result.content)
if not memcache.add('verified_content:%s:%s' % (nitems, page), result.content, 300): # 300 seconds
logging.error('Memcache set failed: verified_content:%s:%s' % (nitems, page))
else:
users_data = json.loads(users_json)
count = 0
users = []
if users_data:
count_verified = long(users_data['total_data'])
if count_verified > long(nitems):
count = 1 + int(count_verified / long(nitems))
users = users_data['paging_data']
template_values = {
'body_text': output_str,
'users': users,
'page': int(page),
'last_page': count,
'base_url': request.base_url,
'url_root': request.url_root
}
template = JINJA_ENVIRONMENT.get_template('index.html')
return template.render(template_values)
@app.errorhandler(404)
def page_not_found(e):
"""Return a custom 404 error."""
return 'Sorry, nothing at this URL. (' + str(e) + ')', 404
# Note: We don't need to call run() since our application is embedded within
# the App Engine WSGI application server.
#if __name__ == '__main__':
# app.run()