Example #1
0
def main():
    client = get_client()
    fanfou.bound(client)
    status = get_status()
    # print(status)
    resp = client.statuses.update({'status': status})
    # print(resp.code)
    assert resp.code == 200
Example #2
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()

Example #3
0
import math
import json
import time
import random
import conf
import models
import fanfou
from datetime import datetime

sent = models.Whale_Sent()
user = models.Whale_User()
talk = models.Whale_Talk()

client1 = fanfou.OAuth(*conf.token['whale1'])
client2 = fanfou.OAuth(*conf.token['whale2'])
fanfou.bound(client1)
fanfou.bound(client2)

my_unique_id = '~YVRvMhrSZx0'

with open(os.path.join(conf.curdir, 'static', 'whale.json'),
          encoding='utf8') as f:
    whale_json = json.loads(f.read())


def htmlec(s):
    el = {'&lt;': '<', '&gt;': '>', '&quot;': '"', '&amp;': '&'}
    for k, v in el.items():
        s = s.replace(k, v)
    return s.strip()
Example #4
0
def do_auth(consumer):
    client = fanfou.XAuth(consumer, consumer['username'], consumer['password'])
    fanfou.bound(client)
    return client
Example #5
0
def get_client():
    import fanfou
    consumer = {'key': key, 'secret': secret}
    client = fanfou.XAuth(consumer, username, password)
    fanfou.bound(client)
    return client
Example #6
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)
Example #7
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)