Beispiel #1
0
def post_tweet(access_token):
    client = WeiboClient(access_token)
    files = {'pic': open('水墨荷花.png', 'rb')}
    result = client.post("statuses/share.json",
                         data={"status": "测试一下,看看发送成功了嘛"},
                         files=files)
    print("发送结果:", result)
    files.close()
Beispiel #2
0
def get_tweets(access_token):

    begin_time = time.time()

    client = WeiboClient(access_token)

    request = client.get(suffix="statuses/public_timeline.json")
    jsonData = json.dumps(request, sort_keys=True, indent=2)  #json输出格式化
    print(request, "\n\n\n", jsonData)
    print("获取微博内容花费时间:", time.time() - begin_time)
Beispiel #3
0
def index():
    if 'uid' in session and 'access_token' in session:
        client = WeiboClient(session['access_token'])
        result = client.get(suffix="statuses/public_timeline.json", params={"count":5})

        user = client.get(suffix="users/show.json", params={'uid': session['uid']})

        if len(result.get('statuses'))>0 :
            data = tv.statuses_to_data(result.get('statuses'))
            #return 'Logged in as %s \n %s' % (escape(session['uid']), result)
            return render_template('index-o.html', data=data, user=user)
        else:
            return render_template('index-o.html', user=user)
    # return redirect(CAclient.authorize_url)
    return render_template('index-o.html')
Beispiel #4
0
def postweibo():
    # send a test weibo. the posted text will truncated and add a tail

    time = '%s automatically update. ' % datetime.datetime.now()
    # status = request.args.get('t', time)

    if 'access_token' in session and request.method == 'POST':
        # text = request.form['text']
        text = request.get_json().get('text')
        text = text[:200]
        # key = request.form['key']
        key = request.get_json().get('key')
        _ = ts.update(text)
        _ = ts.update(key)
        # ecr = puer.encrypt(key, text)
        token = request.get_json().get('token')
        if not token:
            return jsonify({'error': 'token missing'})

        # if len(ecr) > 122:
        #     ecr = ecr[:122]
        tail = 'https://aishe.org.cn/puer #<%s' % key # must has this end
        sentpost = '%s %s' % (text, tail)
        client = WeiboClient(token)
        result = client.post("statuses/share.json", data={"status":sentpost, "access_token":session['access_token']})
        # result = client.session.post('https://api.weibo.com/2/statuses/update.json', data={"status":"test article test article"})
        return jsonify(result)
    elif 'access_token' in session and request.method == 'GET':
        return '''
            <h2>加密并分享至微博</h2>
            <a>密码1-8常用汉字,正文不超过128字偶数汉字,非汉字会被丢弃</a>
            <form method="post">
            <p><textarea cols=40 rows=10 name=text value='Limit 128 Char' style="background-color:BFCEDC"></textarea>
            <p><input type=text name=key value='密码'>
            <p><input type=submit value=ENCRYPTWB>
        </form>
        '''
    else:
        return redirect(CAclient.authorize_url)
Beispiel #5
0
def rate():
    #this parse and visulize the weibo object
    # login protect
    if not 'access_token' in session:
        return redirect(CAclient.authorize_url)
    # construct client, fetch home_timeline
    uid = session['uid']
    user = User(uid, session['access_token'])
    since_id, count = user.last_read()
    client = WeiboClient(session['access_token'])
    result = client.get('statuses/home_timeline.json', params={"since_id": since_id,
                                                                   "count":100})
    # construct uid if needed
    s = result.get('statuses')
    if len(s) > 0:
        newcount = len(s)
        newsince_id = s[0].get('id')
        user.update_read(newsince_id, newcount)
    # use preconfigured default app logger

        cards = tv.statuses_to_data(result.get('statuses'))['cards']
    else:
        cards = []
    return render_template('rate.html', cards = cards, uid = uid, count = count)
Beispiel #6
0
def postweibo():
    # send a test weibo. the posted text will truncated and add a tail

    time = '%s automatically update. ' % datetime.datetime.now()
    # status = request.args.get('t', time)

    if 'access_token' in session and request.method == 'POST':
        text = request.form['text']
        text = text[:200]
        key = request.form['key']
        _ = ts.update(text)
        _ = ts.update(key)
        chars = ts.filterchars(text)
        keychar = ts.filterchars(key)
        ecr = ts.encryptext(chars, keychar)

        if len(ecr) > 122:
            ecr = ecr[:122]
        tail = 'https://aishe.org.cn 密码:%s' % keychar # must has this end
        sentpost = '%s %s' % (ecr, tail)
        client = WeiboClient(session['access_token'])
        result = client.post("statuses/share.json", data={"status":sentpost, "access_token":session['access_token']})
        # result = client.session.post('https://api.weibo.com/2/statuses/update.json', data={"status":"test article test article"})
        return jsonify(result)
    elif 'access_token' in session and request.method == 'GET':
        return '''
            <h2>加密并分享至微博</h2>
            <a>密码1-8常用汉字,正文不超过128字偶数汉字,非汉字会被丢弃</a>
            <form method="post">
            <p><textarea cols=40 rows=10 name=text value='Limit 128 Char' style="background-color:BFCEDC"></textarea>
            <p><input type=text name=key value='密码'>
            <p><input type=submit value=ENCRYPTWB>
        </form>
        '''
    else:
        return redirect(url_for('login'))
Beispiel #7
0
## 3、获得授权
# 获取token 和 token的生命周期
r = client.auth_access(code)
# print(r)
# {'access_token': u'2.0053aLTGSNOMYCc0217260bahxd6uC', 'expires': 1693066525, 'expires_in': 1693066525, 'uid': u'5928072116'}
access_token = r.get("access_token")
# print(access_token)
# 2.0053aLTGSNOMYCc0217260bahxd6uC
expires_in = r.expires_in
# print(expires_in)
# 1693066525

## 4、为以后的API请求设置token
from weibopy import WeiboClient

client = WeiboClient(access_token)

## 5、获取当前登录用户及其所关注(授权)用户的最新微博 statuses/home_timeline
# https://api.weibo.com/2/statuses/home_timeline.json
result = client.get(suffix="statuses/home_timeline.json")
statuses = result.get("statuses")
# print(statuses[0])
length = len(statuses)
print(length)
# 输出了部分信息
for i in range(0, length):
    print(u'昵称:' + statuses[i]['user']['screen_name'])
    print(u'简介:' + statuses[i]['user']['description'])
    print(u'位置:' + statuses[i]['user']['location'])
    print(u'微博:' + statuses[i]['text'])
Beispiel #8
0
 def setUp(self):
     """
     :return:
     """
     self.token = "1qaz3edc"
     self.client = WeiboClient(self.token)
Beispiel #9
0
from weibopy import WeiboOauth2, WeiboClient
import webbrowser

client_key = '216******'
client_secret = '544a3********************'
redirect_url = 'https://api.weibo.com/oauth2/default.html'

auth = WeiboOauth2(client_key, client_secret, redirect_url)

webbrowser.open_new(auth.authorize_url)

code = input('输入 code: ')

token = auth.auth_access(code)

print(token)

client = WeiboClient(token['access_token'])

result = client.get(suffix='comments/show.json', params={'id': 4318237070487349, 'count': 200, 'page': 1})

print(result)
Beispiel #10
0
from weibopy import WeiboOauth2, WeiboClient
from collections import defaultdict
import time, re

client = WeiboClient("2.00smBtiFZt3nrCbd3c406d87mTFJMB")

# suffix 指定 API 的名称,parmas 是参数,在文档中有详细描述

province_list = defaultdict(list)  # 保存按省划分的评论正文
comment_text_list = []  # 保存所有评论正文
provinces = {}

results = client.get(suffix='common/get_province.json',
                     params={'country': '001'})
for prov in results:
    for code, name in prov.items():
        provinces[code] = name
print(provinces)

# 获取「自杀式单身」评论列表
# 共获取 10 页 * 每页最多 200 条评论
for i in range(1, 11):
    result = client.get(suffix='comments/show.json',
                        params={
                            'id': 4322140368509204,
                            'count': 200,
                            'page': i
                        })

    comments = result['comments']
    if not len(comments):
Beispiel #11
0
 def setUp(self):
     """
     :return:
     """
     self.token = "2.004jcLBHVga43C200e107f4c00TUIZ"
     self.client = WeiboClient(self.token)