Beispiel #1
0
 def post(self, text):
     account = self.read_file()
     ff = fanfou.XAuth({
         'key': account['key'],
         'secret': account['secret']
     }, account['username'], account['password'])
     ff.request('/statuses/update', 'POST', {'status': text})
Beispiel #2
0
def xauth():
    form = AuthForm()
    if request.method == 'POST':
        if form.validate_on_submit():
            username = form.username.data
            password = form.password.data

            try:
                consumer = {
                    'key': const.CONSUMER_KEY,
                    'secret': const.CONSUMER_SECRET
                }
                client = fanfou.XAuth(consumer, username, password)
                user_info = client.request('/users/show', 'POST')
                user_json = json.loads(user_info.read().decode('utf8'))
                nickname = user_json['screen_name']
                unique_id = user_json['unique_id']
                try:
                    ff_auth = FFAuth.query.equal_to('uniqueID',
                                                    unique_id).first()
                except LeanCloudError as err:
                    if err.code == 101:
                        ff_auth = FFAuth()
                    else:
                        raise err
                token = client.oauth_token['key'].decode('utf-8')
                secret = client.oauth_token['secret'].decode('utf-8')
                ff_auth.set('uniqueID', unique_id)
                ff_auth.set('username', username)
                ff_auth.set('nickname', nickname)
                ff_auth.set('token', token)
                ff_auth.set('secret', secret)
                ff_auth.save()
                login_user(ff_auth, True)
                return redirect(url_for('main.index'))
            except LeanCloudError as _:
                error = '写入数据库失败'
            except urllib.error.HTTPError as _:
                error = '认证失败, 请输入正确的用户名和密码'
        else:
            error = '表单非法'
    else:
        error = session.get('error_msg', None)
        session['error_msg'] = None
    return render_template('auth.html', form=form, error=error)
Beispiel #3
0
    def __init__(self):
        # 通过api抓取数据
        self.public_timeline_url = 'http://api.fanfou.com/statuses/public_timeline.json'
        self.followers_url = 'http://api.fanfou.com/users/followers.json'
        self.direct_message_url = 'http://api.fanfou.com/direct_messages/conversation_list.json'
        self.send_private_msg_url = 'http://api.fanfou.com/private_messages/new.json'
        self.local_followers_file = 'followers.txt'
        self.local_conversation_file = 'conversations.txt'
        self.local_timeline_file = 'timelines.txt'
        self.username = config.client_key
        self.password = config.client_passwd
        self.client = fanfou.XAuth(config.consumer, config.client_key, config.client_passwd)

        black_list, black_uname, word_list, seq_list, contain_list = self.init_dict()
        self.black_list = black_list
        self.black_uname = black_uname
        self.word_list = word_list
        self.seq_list = seq_list
        self.contain_list = contain_list
        self.my_unique_id = config.my_unique_id
        self.message_user = set()
Beispiel #4
0
def do_auth(consumer):
    client = fanfou.XAuth(consumer, consumer['username'], consumer['password'])
    fanfou.bound(client)
    return client
Beispiel #5
0
def get_client():
    import fanfou
    consumer = {'key': key, 'secret': secret}
    client = fanfou.XAuth(consumer, username, password)
    fanfou.bound(client)
    return client
Beispiel #6
0
#!/home/namyzal/.virtualenvs/fanfou6/bin/python
# -*- coding: utf-8 -*-
import time
import fanfou

consumer = {'key': 'db4736b0c26cd5699350e6b76df6dc87', 'secret': 'a801cf0d7437a5027423d6c080ba3daf'}
client = fanfou.XAuth(consumer, '*****@*****.**', '000aaa')
fanfou.bound(client)  

while True:
	curTime=time.localtime()
	curHr=curTime.tm_hour+8
	if curHr>=24:
		curHr=curHr-24
	curMin=curTime.tm_min
	curSec=curTime.tm_sec
	print str(curHr)+':'+str(curMin)+':'+str(curSec)
	if curMin==0 and curSec==0:
		nMW=curHr/10
		nM=curHr%10
		str1=''
		for i in range(0,nMW):
			str1=str1+'ß÷~'
		for j in range(0,nM):
			str1=str1+'ß÷ÎØ'			
		str1=str1+' '+str(curHr)+'µãÀ²'
		if curHr>=0 and curHr<=7:
			str1=str1+' zzZ zzZ'
		if nMW==0 and nM==0:
			body = {'status': 'ºÇÇ·~~~'}
		else:
Beispiel #7
0
import time
import fanfou

#987654321
consumer = {'key': '', 'secret': ''}

# client = fanfou.XAuth(consumer, 'username', 'password')

client = fanfou.XAuth(consumer, '@sina.com', '')
fanfou.bound(client)
resp = client.users.show()
resp = resp.json()


def getFilemsg(fromMsg):
    outMsg = ''
    try:
        with open('story.txt', encoding='utf8') as fi:
            contents = fi.readlines()
        fi.close()
        fileLen = len(contents)
        content = fromMsg
    except Exception as e:
        print(e)

    pos = 0
    try:
        for line in contents:
            if content in line:
                break
            else:
Beispiel #8
0
    def detect_join(log_filename):
        is_first_traver = 1  # 第一次迭代设立时间戳的标记
        is_first_traver_msg = 1
        date_flag = 0
        date_flag_msg = 0
        block_filename = 'block_list.txt'
        while 1:
            psw_json = json.load(open("psw.json", encoding='utf-8'))
            consumer = {
                'key': psw_json['robot']['key'],
                'secret': psw_json['robot']['secret']
            }
            try:
                client = fanfou.XAuth(consumer, psw_json['robot']['username'],
                                      psw_json['robot']['password'])
            except TypeError:
                print('connect out of time.')
                continue
            except urllib.error.URLError:
                print('connect out of time.')
                continue
            except TimeoutError:
                print('connect out of time.')
                continue
            fanfou.bound(client)
            write_to_file(log_filename, 'start check -quit:')
            resp_mentions = create_resp(client, 'mentions')
            resp_messages = create_resp(client, 'messages')
            if resp_mentions == 0 or resp_messages == 0:
                continue
            for item in resp_mentions.json()[::-1]:  # 以此获得时间线上的20条消息
                # 设立时间flag,防止重复检索消息
                current_date = item['created_at']
                current_date = datetime.datetime.fromtimestamp(
                    datetime.datetime.timestamp(parse(current_date)))
                if is_first_traver:  # 全局第一次执行,设立时间flag
                    is_first_traver = 0
                    date_flag = current_date
                    print('data_flag:', date_flag)
                    if (current_date - date_flag
                        ).total_seconds() < 0:  # 消息的时间早于当前的flag的时候,则跳过。
                        continue
                # print(current_date)
                elif (current_date - date_flag
                      ).total_seconds() <= 0:  # 消息的时间早于或者等于当前的flag的时候,则跳过。
                    continue
                date_flag = current_date
                print('data_flag update:', date_flag)
                print(convert_code(item['text']))
                result = process_join_quit(item, block_filename, log_filename,
                                           current_date, client)
                if result == 0:
                    continue
            for item in resp_messages.json()[::-1]:  # 以此获得时间线上的20条私信
                # 设立时间flag,防止重复检索消息
                current_date = item['created_at']
                current_date = datetime.datetime.fromtimestamp(
                    datetime.datetime.timestamp(parse(current_date)))
                if is_first_traver_msg:  # 全局第一次执行,设立时间flag
                    is_first_traver_msg = 0
                    date_flag_msg = current_date
                    print('data_flag:', date_flag_msg)
                    if (current_date - date_flag_msg
                        ).total_seconds() < 0:  # 消息的时间早于当前的flag的时候,则跳过。
                        continue
                # print(current_date)
                elif (current_date - date_flag_msg
                      ).total_seconds() <= 0:  # 消息的时间早于或者等于当前的flag的时候,则跳过。
                    continue
                date_flag_msg = current_date
                print('data_flag update:', date_flag_msg, current_date,
                      date_flag_msg)
                print(convert_code(item['text']))
                result = process_join_quit_msg(item, block_filename,
                                               log_filename, current_date,
                                               client)
                if result == 0:
                    continue

            time.sleep(60)
Beispiel #9
0
import fanfou
import pandas as pd
import json

consumer = {'key': 'your_key', 'secret': 'your_secret'}
client = fanfou.XAuth(consumer, 'your_id', 'your_password')
fanfou.bound(client)

#  get all the statuses
page = 1
last = False

total_df = pd.DataFrame(columns=['created_at', 'text'])

while last == False:
    body = {'page': page}
    resp = client.statuses.user_timeline(body)
    data = resp.json()

    # control the loop
    if len(data) == 0:
        last = True
        break

    datas = pd.DataFrame(data)
    current_df = pd.DataFrame(datas, columns=['created_at', 'text'])

    # put every current_df into csv
    if page == 1:
        current_df.to_csv('fanfou_exp.csv', index=False)
    else:
Beispiel #10
0
import fanfou
import urllib
import time
from urllib import error
import requests
from PIL import Image
from io import BytesIO
import json

psw_json = json.load(open('psw.json', encoding='utf-8'))
consumer = {'key': psw_json['download']['key'], 'secret': psw_json['download']['secret']}
client = fanfou.XAuth(consumer, psw_json['download']['username'], psw_json['download']['password'])
fanfou.bound(client)

dict_array = {}
count = 0
while 1:
    # resp = client.statuses.home_timeline()
    try:
        resp = client.statuses.public_timeline()
    except error.URLError:  # 处理返回异常
        print('return error')
    except requests.exceptions.ConnectionError:
        print('connect error')
    # ------------------------------------
    # 打印一条消息
    '''data = resp.json()
    print(type(data))
    print(type(data[0]))
    print(data[0]['text'])
    print(data[1]['text'])'''
Beispiel #11
0
 def get_client(self):
     client = fanfou.XAuth(self.consumer, self.username, self.password, fake_https=True)
     return client
Beispiel #12
0
def robot_start(traget, log_filename):
    print('start at:', log_filename)
    count_cycle = 30  # 总共缓存消息数量
    psw_json = json.load(open("psw.json", encoding='utf-8'))
    consumer = {
        'key': psw_json['robot']['key'],
        'secret': psw_json['robot']['secret']
    }
    client = fanfou.XAuth(consumer, psw_json['robot']['username'],
                          psw_json['robot']['password'])
    fanfou.bound(client)
    dict_array = {}  # 最近发布过的消息ID列表
    count = 0  # 最近发布过的消息ID索引
    while 1:
        # 回收内存
        gc.collect()
        # print('test-->0.1')
        # resp = client.statuses.home_timeline()
        try:
            resp = client.statuses.public_timeline()
        except error.URLError:  # 处理返回异常
            print('return error resp')
            time.sleep(10)
            continue
        except requests.exceptions.ConnectionError:
            print('connect error')
            continue
        except TimeoutError:
            print('time out error!')
            continue
        except http.client.RemoteDisconnected:
            print('RemoteDisconnected')
            continue
        # ------------------------------------
        # 打印一条消息
        '''data = resp.json()
        print(type(data))
        print(type(data[0]))
        print(data[0]['text'])
        print(data[1]['text'])'''
        # ------------------------------------
        # 发送一条饭否消息
        #body = {'status': 'hello,fanfou'}
        #resp = client.statuses.update(body)
        #print(resp.code)  # 如果成功,返回200
        # ------------------------------------

        print('-list:',
              time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
        for item in resp.json():  # 获得时间线上的20条ID并逐条转发处理
            if 'photo' in item.keys():  # 该消息中存在photo
                if 'repost_user_id' not in item.keys():  # 非转发消息
                    # print('test-->0.5')
                    # print(item.keys())
                    try:

                        # ※先判断当前message id 是否存在于已检测过的消息中。若存在,则不发布。
                        list_id = item['id']  # type str
                        if list_id in dict_array.values():
                            continue

                        # 提取图片的url,如果url是gif图片,则跳过,并加入count计数器
                        image_url = item['photo']['largeurl']
                        if image_url[-3:] == 'gif':
                            dict_array[count] = item['id']
                            count = int(count % count_cycle) + 1
                            write_to_file(log_filename,
                                          'this is gif id:count++')
                            continue

                        # 从block list中加载屏蔽用户,当存在屏蔽列表,则continue 跳过本次。
                        is_block = 0
                        block_list = open('block_list.txt')
                        # print('block_list:')
                        for block_user_id in block_list:
                            # print(block_user_id[:-1])
                            if item['user']['id'] == block_user_id[:-1]:
                                print('this id has exist in block list')
                                is_block = 1
                                break
                        if is_block == 1:
                            print('block id:', item['user']['id'])
                            dict_array[count] = item['id']
                            count = int(count % count_cycle) + 1
                            write_to_file(log_filename,
                                          'this is block id:count++')
                            continue

                        # 提取图片 ndarray格式
                        image = url_to_image(image_url)
                        if type(image) == int:
                            image = url_to_image(image_url)
                            if type(image) == int:
                                print('get url timeout')
                                continue
                        # 如果图片过长,或者过高。对图片进行裁剪,采取上中下,左中右,以及原图四张图片预测。有一张符合则符合。
                        if len(image.shape) == 2:
                            image = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)
                        img_hig = image.shape[0]
                        img_wide = image.shape[1]
                        # 如果图片的高度,宽度小于一定值,不预测。
                        if img_hig < 201 or img_wide < 201:
                            dict_array[count] = item['id']
                            count = int(count % count_cycle) + 1
                            write_to_file(log_filename,
                                          'this is little image:count++')
                            continue
                        write_to_file(log_filename, '-count:' + str(count))
                        # 将信息写入log文件
                        message_text = convert_code(item['text'])
                        try:
                            write_to_file(
                                log_filename, '\n' +
                                time.strftime('%Y-%m-%d %H:%M:%S',
                                              time.localtime(time.time())) +
                                ' ' + list_id + ' ' + item['user']['name'] +
                                ' ' + item['user']['id'] + ' ' +
                                str(message_text) + '\n' + image_url)
                        except UnicodeEncodeError as err:
                            write_to_file(
                                log_filename,
                                'UnicodeEncodeError:' + str(err) + '\n')
                        # 发布转载消息
                        body = {
                            'status':
                            '转@' + item['user']['name'] + ' ' + item['text'],
                            'repost_status_id':
                            item['id']
                        }
                        # print('test-->1')
                        # 发送消息
                        # resp = client.statuses.update(body)
                        # 保存图片到文件夹
                        # response = requests.get(image_url)
                        # path = r'D:/ProjectPython/fanfou/images/' + image_url[-22:-12]
                        # path = r'D:/ProjectPython/fanfou/images/' + item['user']['id'] + '+' + item['id'] + '.jpg'
                        # image = Image.open(BytesIO(response.content))
                        # image.save(path)

                        # 显示图片
                        # image = io.imread(image_url)
                        # io.imshow(image)
                        # io.show()
                        # print('test-->1.5')

                        if img_wide >= img_hig * 1.4 and img_hig > 240:  # 长图片
                            img_start = int((img_wide - img_hig) / 2)
                            image_mid = image[:,
                                              img_start:img_start + img_hig, :]
                            image_left = image[:, 0:img_hig, :]
                            image_right = image[:,
                                                img_wide - img_hig:img_wide, :]
                            test_result = {}
                            test_result[0] = Predict.predict(image)
                            test_result[1] = Predict.predict(image_left)
                            test_result[2] = Predict.predict(image_mid)
                            test_result[3] = Predict.predict(image_right)
                            write_to_file(
                                log_filename, 'long image: ' +
                                str(test_result[0]) + str(test_result[1]) +
                                str(test_result[2]) + str(test_result[3]))
                            # 这里是四个分类的数量,0-人,1-文字,2-食物,3-动物,4-风景
                            count_classify = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
                            is_human = 1
                            for i in range(4):  # 计算每个可能情况的数量(一共的数量是4)
                                key = test_result[i]
                                count_classify[key] = count_classify[key] + 1
                            for i in range(1, 5):  # 如果 1-4 中有一个出现三次可能,则不是0。
                                if count_classify[i] == 3:
                                    is_human = 0
                            if is_human and count_classify[0]:
                                resp = client.statuses.update(body)
                                # print('relay state:', resp)
                                write_to_file(log_filename, 'success post!')

                        elif img_wide * 1.4 <= img_hig and img_wide > 240:  # 高图片
                            img_start = int((img_hig - img_wide) / 2)
                            image_mid = image[img_start:img_start +
                                              img_wide, :, :]
                            image_up = image[0:img_wide, :, :]
                            image_down = image[img_hig -
                                               img_wide:img_hig, :, :]
                            test_result = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
                            test_result[0] = Predict.predict(image)
                            test_result[1] = Predict.predict(image_up)
                            test_result[2] = Predict.predict(image_mid)
                            test_result[3] = Predict.predict(image_down)
                            write_to_file(
                                log_filename, 'long image: ' +
                                str(test_result[0]) + str(test_result[1]) +
                                str(test_result[2]) + str(test_result[3]))
                            # 这里是四个分类的数量,0-人,1-文字,2-食物,3-动物,4-风景
                            count_classify = {0: 0, 1: 0, 2: 0, 3: 0, 4: 0}
                            is_human = 1
                            for i in range(4):  # 计算每个可能情况的数量(一共的数量是4)
                                key = test_result[i]
                                count_classify[key] = count_classify[key] + 1
                            for i in range(1, 5):  # 如果 1-4中有一个出现三次可能,则不是0。
                                if count_classify[i] == 3:
                                    is_human = 0
                            if is_human and count_classify[0]:
                                resp = client.statuses.update(body)
                                # print('relay state:', resp)
                                write_to_file(log_filename, 'success post!')
                        else:  # 方形图片,只预测一次
                            test_result = Predict.predict(image)
                            write_to_file(log_filename,
                                          'square image: ' + str(test_result))
                            if test_result == 0:
                                resp = client.statuses.update(body)
                                # print('relay state:', resp)
                                write_to_file(log_filename, 'success post!')

                        # urllib.urlretrieve(image_url, path)
                        dict_array[count] = list_id
                        count = int(count % count_cycle) + 1
                        write_to_file(log_filename, 'success predict:count++')
                    except error.URLError:  # 快速发布多条相同消息异常处理
                        print('400 error')
                    except requests.exceptions.ConnectionError:
                        print('connect error')
                    except ConnectionResetError:
                        print('connect reset error')
                    except MemoryError:
                        print('memory error')
                    except tensorflow.python.framework.errors_impl.ResourceExhaustedError:
                        print('resource exhausted error')
                    except tensorflow.python.framework.errors_impl.InternalError:
                        print('InternalError')
                    except ValueError:
                        print('value error')
                    except TypeError:
                        print('type error')
                    except TimeoutError:
                        print('timeout error')
                    except timeout:
                        print('socket timeout')
                    except Exception as err:
                        print(err)
                    except http.client.IncompleteRead:
                        # print('http.client.IncompleteRead error')
                        write_to_file(log_filename,
                                      'http.client.IncompleteRead error')
                    # 没有错误,顺利执行之后,count++。不论是否发布成功
        time.sleep(time_sleep)
Beispiel #13
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
import time
import datetime
import fanfou
import re
import shelve

consumer = {
    'key': '3812d23acfd579969428db1c223141c4',
    'secret': '4c6557a19a8b44a1c787937d2a8b657a'
}
client = fanfou.XAuth(consumer, 'GetFlag', 'xxxxxx')
fanfou.bound(client)

FlagFile = shelve.open('FlagFile')
FlagFile['ids'] = []
FlagFile['Datas'] = []
#FlagFile['Datas']=[{date:3/21,text:xxxxx,user_id:xxx,name:xx},]


def regex(text):
    fi = re.compile(r'(\d\d|\d)(月|.|-|/)(\d\d|\d)(日|)(.*)', re.DOTALL)
    mo = fi.search(text)
    if not mo:
        return None
    else:
        flag_text = mo.group(5)
        flag_month = mo.group(1)
        flag_day = mo.group(3)
Beispiel #14
0
#!/usr/bin/python
# -*- coding: utf-8 -*-
import datetime
import fanfou

# 请修改为你的 Consumer
consumer = {'key': 'consumer key', 'secret': 'consumer secret'}
# 请修改为你的 ID 和密码
client = fanfou.XAuth(consumer, 'username', 'password')
fanfou.bound(client)

now = datetime.datetime.now()
bong = 'Bong ' * ((now.hour % 12) or 12)
client.statuses.update({'status': bong[:-1] + '!'})
Beispiel #15
0
# -*- coding: utf-8 -*-

import fanfou
import config as config

# login in on fanfou
consumer = {'key': config.key, 'secret': config.secret}
client = fanfou.XAuth(consumer, config.client_name, config.client_pwd)
fanfou.bound(client)

user = {'id': config.target_id, 'page': '0'}

page_max = config.page_max

file_name = user['id'] + '.txt'
try:
    out_file = open(file_name, 'a')
except:
    print "Output file open failed!"

for i in range(0, page_max):
    page = i + 1
    print('Page:' + str(page) + '\n')
    user['page'] = str(page)
    tweets = client.statuses.user_timeline(user).json()
    length = len(tweets)

    for j in range(0, length):
        foo = tweets[j]['text']  #type: unicode
        if foo.find('@') >= 0:  #if reply/mention: skip
            continue
Beispiel #16
0
		if f not in followers:

			client.blocks.create({'id': f})
			num += 1
	# if num > 0: print "[" + str(num) + " user(s) blocked.]"
	print "[" + str(num) + "/" + str(len(past_followers)) + " user(s) blocked.]"

if __name__ == "__main__":
	accounts = {'账号ID-1': '账号密码-1', '账号ID-2': '账号密码-2'}
	consumer_key = 'b7d7411a97bce56df29043c7de521395'
	consumer_secret = '576f9b944ac787983be03c54831adbeb'
	consumer = {'key': consumer_key, 'secret': consumer_secret}

	for userID in accounts:
		# print "[Processing for " + userID + "...]"
		print userID + " "
		filename = userID + "_followers"
		client = fanfou.XAuth(consumer, userID, accounts[userID])
		fanfou.bound(client)


		past_followers = read_past_followers(filename)
		followers = get_followers(client)
		check_and_block(past_followers, followers, client)

		file = open(filename, 'w')
		for f in followers:
			file.write(f + "\n")
		file.close()

Beispiel #17
0
import fanfou
import time
import random
import datetime
import pandas as pd

# API 协议 
consumer = {'key': '****** your  key ************', 
            'secret': '****** your  secret ************'} 
# 这里要替换成“需要发言”的 ID 和密码
client = fanfou.XAuth(consumer, '***your username***', '***your password***')

# 必要的一步绑定
fanfou.bound(client)

# 读取评论文件
df_ne = pd.read_excel('./data/netease_2.xlsx')
df_ne = df_ne.reset_index(drop=True)

df_db = pd.read_excel('./data/douban_book_1.xlsx').reset_index(drop=True)
df_db = df_db.drop(df_db[df_db.commnet.duplicated(keep='last')].index.values)

df_db_movie = pd.read_excel('./data/douban_movie_1.xlsx').reset_index(drop=True)
df_db_movie = df_db_movie.drop(df_db_movie[df_db_movie.commnet.duplicated(keep='last')].index.values)

# 网易云音乐的评论
def get_text_ne(df_ne):
    num = random.choice(df_ne.index)
    comment = df_ne.loc[num, 'content']
    song = df_ne.loc[num, 'song']
    nick = df_ne.loc[num, 'nickname']