Example #1
0
def get_url():
    APP_KEY = '2122800769'
    APP_SECRET = 'df544eed525171f054ac7a9be25bbdd6'
    REDIRECT_URL = 'http://www.csdn.net/greetings'
    client = sinaweibopy3.APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=REDIRECT_URL,)
    url = client.get_authorize_url()
    print(url)
    return client,url
 def __init__(self, weibo_id, weibo_name):
     self.weibo_id = weibo_id
     self.weibo_name = weibo_name
     self.client = sinaweibopy3.APIClient(
         app_key='1431227519',
         app_secret='02c1e46a8699199ea98fb47ddbacc92b',
         redirect_uri='https://api.weibo.com/oauth2/default.html')
     self.count = 0
     self.page = 0
     self.comments = []
     self.gender = []
def main():
    '''
    if you want to use this api,you should follow steps follows to operate.
    '''
    try:
        # step 1 : sign a app in weibo and then define const app key,app srcret,redirect_url
        APP_KEY = '3348254988'
        APP_SECRET = 'd15c60dc13f4a1a5b3b5c644e8a3bf3c'
        REDIRECT_URL = 'http://api.weibo.com/oauth2/default.html'
        # step 2 : get authorize url and code
        client = sinaweibopy3.APIClient(app_key=APP_KEY,
                                        app_secret=APP_SECRET,
                                        redirect_uri=REDIRECT_URL)
        url = client.get_authorize_url()
        # print(url)
        webbrowser.open_new(url)
        # step 3 : get Access Token
        # Copy the above address to the browser to run,
        #enter the account and password to authorize, the new URL contains code
        result = client.request_access_token(
            input("please input code : "
                  ))  # Enter the CODE obtained in the authorized address
        print(result)
        # At this point, the access_token and expires_in should be saved,
        # because there is a validity period.A
        # If you need to send the microblog multiple times in a short time,
        # you can use it repeatedly without having to acquire it every time.
        client.set_access_token(result.access_token, result.expires_in)

        # step 4 : using api by access_token
        print(client.public_timeline())  # get the latest public Weibo
        # =============================================================================
        #         statuses = client.public_timeline()['statuses']
        #         length = len(statuses)
        #         for i in range(0,length):
        #             print("昵称:"+statuses[i]['user']['screen_name'])
        #             print("简介:"+statuses[i]['user']['description'])
        #             print("位置:"+statuses[i]['user']['location'])
        # =============================================================================
        '''
        in this step,the api name have to turn '/' in to '__'
        for example,statuses/public_timeline(this is a standard api name) have to turn into statuses__public_timeline
        '''
        # Or use this method
        print(client.get.statuses__public_timeline())

        # Obtain the UID of the authorized user
        print(client.get.account__get_uid())

    except ValueError:
        print('pyOauth2Error')
def runWeibo(selectedname):
    client = sinaweibopy3.APIClient(app_key=APP_KEY,
                                    app_secret=APP_SECRET,
                                    redirect_uri=REDIRECT_URL)
    url = client.get_authorize_url()
    webbrowser.open_new(url)
    result = client.request_access_token(input("please input code : "))
    client.set_access_token(result.access_token, result.expires_in)

    result = client.fs_friends()
    raw_statuses = result['statuses']

    statuses = weiboFilter(raw_statuses, selectedname)
    length = len(statuses)
    usertext = []
    if length == 0:
        print("亲,你好像没关注" + selectedname)
        # This printout is to shown that user didn't subsribe the
        # channel that they searched
    else:
        alltext = []
        allgeo = []
        wfile = open('weibo.txt', 'w')
        jfile = open('weibo.json', 'w')
        gfile = open('geo.json', 'w')
        for i in statuses:
            geoloc = i['geo']
            if geoloc != None:
                latitude = geoloc['coordinates'][0]
                longitude = geoloc['coordinates'][1]
                print(type(latitude))
                geodict = {"lng": longitude, "lat": latitude, "count": 100}
                allgeo.append(geodict)
            text = i['text']
            text_clean = filter_text(text)
            alltext.append(text_clean)

            wfile.write(text_clean + "\n")
        json.dump(alltext, jfile)
        json.dump(allgeo, gfile)

        print('Done writing to the file')

        wfile.close()
        gfile.close()
        usertext = alltext
        usergeo = allgeo
    return usertext, usergeo
Example #5
0
def main():
    try:
        # step 1 : sign a app in weibo and then define const app key,app srcret,redirect_url
        APP_KEY = '127174040'
        APP_SECRET = '2b483df41e57480249a35660b69dcf19'
        REDIRECT_URL = 'https://api.weibo.com/oauth2/default.html'
        # step 2 : get authorize url and code
        client = sinaweibopy3.APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=REDIRECT_URL)
        url = client.get_authorize_url()
        print(url)
        webbrowser.open_new(url)
        # step 3 : get Access Token
        # Copy the above address to the browser to run,
        # enter the account and password to authorize, the new URL contains code
        result = client.request_access_token(
            input("please input code : "))  # Enter the CODE obtained in the authorized address
        print(result)

        client.set_access_token(result.access_token, result.expires_in)

    except ValueError:
        print('pyOauth2Error')

    ids = read_from_file("2.txt")
    sids = read_from_file("ids.txt")
    # screen_names = []
    # for uid in ids:
    #     screen_name = client.get_user_info(uid)
    #     print(screen_name)
    #     screen_names.append(screen_name)
    # write_to_csv("test.csv", screen_names)

    for sid in sids:
        relationship = []
        print("sid: " + sid)
        for tid in ids:
                flag = client.get_friendships(sid, tid)
                print("tid: " + tid + ", flag: " + str(flag))
                if flag:
                    relationship.append('1')
                else:
                    relationship.append('0')
        write_to_csv('test.csv', relationship)
keys = [
    '4278710059', '152557326', '646317177', '2507409448', '1006844052',
    '4052393024', '3777270742', '3181433703', '1744249797', '2606580009'
]
secrets = [
    '9065fb6f674b1d1203afb99f8b5c0156', '6945fe41a4796de948ab2174a403812c',
    '81aa30fe112ed9bebed31b5130b3e2d1', '5e9e0c40e9219b83442bedaaaa4d2a5c',
    'd58e7f4aa8aa8ccd5fa231d118cc94df', 'b138a3ec2d48ea1416da1e80c8e36614',
    '6ad2cee33ca07c5f74574d06f21ede2c', '906110cba1b9b0af5d1705dabc13e5b9',
    '59d41e7f9ceb96a944d75e17826f1053', '1d45d42c21f8e94777d506d83f22464c'
]
APP_KEY = keys[9]
APP_SECRET = secrets[9]
REDIRECT_URL = 'http://api.weibo.com/oauth2/default.html'
client = sinaweibopy3.APIClient(app_key=APP_KEY,
                                app_secret=APP_SECRET,
                                redirect_uri=REDIRECT_URL)
url = client.get_authorize_url()
webbrowser.open_new(url)
result = client.request_access_token(input("please input code : "))
print(result)
client.set_access_token(result.access_token, result.expires_in)

for i in range(165, 166):
    with open('weibo_data/{}.txt'.format(i), 'r', encoding='utf-8') as file:
        try:
            mid = json.loads(file.readline())['mid']
        except:
            continue
    detail_file = codecs.open('weibo_data/{}.txt'.format(i), 'a', 'utf-8')
    weibo_detail = []
Example #7
0
def main():
    """
    if you want to use this api,you should follow steps follows to operate.
    """

    try:
        # step 1 : sign a app in weibo and then define const app key,app secret,redirect_url
        APP_KEY = '4139690010'
        APP_SECRET = '4c3fb4a9b423c5cdeb0c0d9413e8b4a7'
        REDIRECT_URL = 'https://api.weibo.com/oauth2/default.html'

        # step 2 : get authorize url and code
        client = sinaweibopy3.APIClient(app_key=APP_KEY, app_secret=APP_SECRET, redirect_uri=REDIRECT_URL)
        url = client.get_authorize_url()
        # print(url)
        webbrowser.open_new(url)

        # step 3 : get Access Token
        # Copy the above address to the browser to run,
        # enter the account and password to authorize, the new URL contains code
        result = client.request_access_token(
            input("please input code : "))  # Enter the CODE obtained in the authorized address
        print(result)  # Save the access_token from here.

        # At this point, the access_token and expires_in should be saved,
        # because there is a validity period.
        # If you need to send the microblog multiple times in a short time,
        # you can use it repeatedly without having to acquire it every time.
        client.set_access_token(result.access_token, result.expires_in)

        # step 4 : using api by access_token
        '''
        in this step,the api name have to turn '/' in to '__'
        for example,statuses/public_timeline(this is a standard api name) have to turn into statuses__public_timeline
        '''

        # Obtain the UID of the authorized user
        # uid = client.get.account__get_uid()
        # print(uid)

        init_status = client.get.statuses__user_timeline()['statuses'][0]  # 获取用户最近微博

        while True:
            current_status = client.get.statuses__user_timeline()['statuses'][0]  # 获取用户最近微博
            current_text = current_status['text']
            current_id = current_status['id']
            # print(time.ctime(), current_text)

            if current_id != init_status['id'] and current_text:
                args = shlex.split(current_text)
                args.pop()
                # subprocess.check_output(args, shell=False)
				subprocess.getoutput(args)

                if '-s' in args:
                    client.post.comments__create(id=current_id, comment='正在关机......')
                elif '-a' in args:
                    client.post.comments__create(id=current_id, comment='已取消关机......')

                init_status = current_status

            time.sleep(10)  # ip限制1000次/小时

    except ValueError:
        print('pyOauth2Error')