__author__ = 'YangZongyun'
# -*- coding: utf-8 -*-


from weibo import APIClient
import webbrowser
import MySQLdb
import numpy as np


APP_KEY = YourAppKey  # need init
APP_SECRET = YourAppSecret  # need init
CALLBACK_URL = 'https://api.weibo.com/oauth2/default.html'  # callback url
CLIENT = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
URL = CLIENT.get_authorize_url()


def db_connect(poiid, title, address, lat, lon, category, checkin_user_num, city):
    if city != u'0025': # 0025 is the code of Nanjing you can change this by you own need
        return
    else:
        conn = MySQLdb.connect(
            host='localhost',
            port=3306,
            user=DbUser,
            passwd=DbPass,
            db=DBName,
            charset='utf8'
        )
        cur = conn.cursor()
        cur.execute('set names utf8')
        try:
            sql = "insert into weibopoi(poiid, title, address, lon, lat, category,checkin_user_num,city)\
         values('%s','%s','%s','%s','%s', '%s', '%s', '%s');"\
              % (poiid, title, address, lon, lat, category, checkin_user_num,city)
            n = cur.execute(sql)
        except:
            conn.rollback()
        cur.close()
        conn.commit()
        conn.close()


def weibo_api(client, deflat, deflong):

    r = client.place.nearby.pois.get(lat=deflat, long=deflong, range=10000)
    return r

if __name__ == '__main__':
    '''db_connect()'''
    webbrowser.open_new(URL)
    code = raw_input('pls input \n')
    CLIENT = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
    R = CLIENT.request_access_token(code)
    access_token = R.access_token
    expires_in = R.expires_in
    CLIENT.set_access_token(access_token, expires_in)
    for i in np.linspace(0, 1.384, 1500, endpoint=False):
        LAT = 31.23 + i
        for j in np.linspace(0, 0.88, 1500, endpoint=False):
            LONG = 118.36 + j
            LAT = float('%0.5f' % LAT)
            LONG = float('%0.5f' % LONG)
            try:  # consider the situation of weibo api times limits
                Json_content = weibo_api(client=CLIENT, deflat=LAT, deflong=LONG)
                print str(LAT)+'\t'+str(LONG)+'\n'
                try:  # keep the programm going on when Json_comtent is empty, keep the code not crushing
                    for poi in Json_content['pois']:
                        _poiid = poi['poiid']
                        _title = poi['title']
                        _address = poi['address']
                        _lat = poi['lat']
                        _lon = poi['lon']
                        _category = poi['category_name']
                        _checkin_user_num = poi['checkin_user_num']
                        _city = poi['city']
                        db_connect(poiid=_poiid, title=_title, address=_address, lat=_lat, lon=_lon, category=_category,  checkin_user_num=_checkin_user_num, city=_city)
                except:
                    pass
            except:
                webbrowser.open_new(URL)
                code = raw_input('pls input \n')
                CLIENT = APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=CALLBACK_URL)
                R = CLIENT.request_access_token(code)
                access_token = R.access_token
                expires_in = R.expires_in
                CLIENT.set_access_token(access_token, expires_in)

@@ -1303,16 +1303,23 @@
  deflong
+, pages
 ):%0A%0A    
@@ -1388,16 +1388,38 @@
 ge=10000
+, count=50, page=pages
 )%0A    re
@@ -2114,16 +2114,108 @@
  limits%0A
+                for _page in range(1,1000):%0A                    if not _page == 1: %0A        
         
@@ -2282,26 +2282,309 @@
 deflong=LONG
+,pages=_page
 )%0A
+                        max_page = Json_conenten%5B'total_number'%5D%0A                    elif _page %3E= max_page:%0A                        Json_content = weibo_api(client=CLIENT, deflat=LAT, deflong=LONG,pages=_page)%0A                    else:%0A                        break%0A    
             
@@ -2622,16 +2622,20 @@
 G)+'%5Cn'%0A
+    
         
@@ -2748,24 +2748,28 @@
             
+    
 for poi in J
@@ -2813,16 +2813,20 @@
         
+    
 _poiid =
@@ -2863,16 +2863,20 @@
         
+ 
+   
 _title =
@@ -2885,24 +2885,28 @@
 oi%5B'title'%5D%0A
+    
             
@@ -2967,16 +2967,20 @@
         
+    
 _lat = p
@@ -3013,16 +3013,20 @@
         
+   
+ 
 _lon = p
@@ -3035,16 +3035,20 @@
 %5B'lon'%5D%0A
+    
         
@@ -3120,16 +3120,20 @@
         
+   
+ 
 _checkin
@@ -3192,16 +3192,20 @@
         
+    
 _city = 
@@ -3240,16 +3240,20 @@
         
+  
+  
 db_conne
@@ -3398,32 +3398,36 @@
                 
+    
 except:%0A        
@@ -3442,13 +3442,21 @@
     
+    
 pass%0A
+    
     
@@ -3483,24 +3483,28 @@
             
+    
 webbrowser.o
@@ -3512,24 +3512,28 @@
 en_new(URL)%0A
+    
             
@@ -3565,24 +3565,28 @@
  input %5Cn')%0A
+    
             
@@ -3687,24 +3687,28 @@
             
+  
+  
 R = CLIENT.r
@@ -3745,24 +3745,28 @@
             
+    
 access_token
@@ -3795,24 +3795,28 @@
             
+ 
+   
 expires_in =
@@ -3825,24 +3825,28 @@
 .expires_in%0A
+    
             

update the page an count parm in weiboapi, to get more poi information