Example #1
0
 def __init__(self,
              token=None,
              keyWord=None,
              weiboRef=None,
              weiboCookie=None,
              weiboSCF=None,
              weixinToken=None):
     self.useDingTalk = False
     self.useWeibo = False
     self.useSCF = False
     self.useWeixin = False
     if token and keyWord:
         self.useDingTalk = True
         self.d = DingtalkChatbot(
             'https://oapi.dingtalk.com/robot/send?access_token=%s' % token)
         self.keyWord = keyWord
     if weiboRef and weiboCookie:
         self.useWeibo = True
         self.weibo = Weibo(weiboRef, weiboCookie)
     if weiboSCF:
         self.useSCF = True
         self.weiboSCF = weiboSCF
     if weixinToken:
         self.useWeixin = True
         self.wxurl = 'https://sc.ftqq.com/%s.send' % weixinToken
Example #2
0
def getFilePathByUid(uid):
    # 初始化weibo
    weibo = Weibo(uid)
    weibo.get_user_info()
    # 获取用户信息
    filePath = weibo.get_filepath("csv")
    return filePath
Example #3
0
def main_handler(event, context):
    logger.info("start main handler")
    if "requestContext" not in event.keys():
        return {"code": 410, "errorMsg": "event is not come from api gateway"}
    weibo = Weibo(WeiboRef, WeiboCookie)
    weibo.send(event['body'])
    return ("o")
Example #4
0
 def run(self):
     while 1:
         ret = self.db_2.find()
         for i in ret:
             uid = int(i['star_id'])
             filter = 1  # 值为0表示爬取全部微博(原创微博+转发微博),值为1表示只爬取原创微博
             pic_download = 1  # 值为0代表不下载微博原始图片,1代表下载微博原始图片
             wb = Weibo(uid, filter, pic_download)
             pn = 1
             while 1:
                 wb.get_one_page(pn)
                 time.sleep(random.randint(6, 10))
                 pn += 1
                 if pn == 3:
                     break
                 try:
                     for obj in wb.weibo:
                         obj['_id'] = md5(obj['screen_name'] + obj['text'] +
                                          obj['created_at'])
                         self.db_1.insert_one(obj)
                         print(obj)
                 except Exception:
                     break
                 wb.weibo = []
         time.sleep(60)
Example #5
0
def main():
    try:
        # 爬取关注列表的user_id
        user_id = int('Your id')
        cookie = {'Cookie': 'Your cookie'}
        # 将your cookie替换成自己的cookie
        fw = Follow(user_id, cookie)  # 调用Weibo类,创建微博实例wb
        fw.get_follow_list()  # 获取关注列表
        print(fw.follow_list)  # 输出关注列表的uid
        print(fw.follow_name_list)  # 输出关注列表的昵称

        filter = 1  # 值为0表示爬取全部微博(原创微博+转发微博),值为1表示只爬取原创微博
        since_date = '2018-01-01'  # 起始时间,即爬取发布日期从该值到现在的微博,形式为yyyy-mm-dd
        """mongodb_write值为0代表不将结果写入MongoDB数据库,1代表写入;若要写入MongoDB数据库,
        请先安装MongoDB数据库和pymongo,pymongo安装方法为命令行运行:pip install pymongo"""
        mongodb_write = 0
        """mysql_write值为0代表不将结果写入MySQL数据库,1代表写入;若要写入MySQL数据库,
        请先安装MySQL数据库和pymysql,pymysql安装方法为命令行运行:pip install pymysql"""
        mysql_write = 0
        pic_download = 1  # 值为0代表不下载微博原始图片,1代表下载微博原始图片
        video_download = 0  # 值为0代表不下载微博视频,1代表下载微博视频
        for user in fw.follow_list:
            # 爬每个人的微博
            new_list = [user]
            wb = Weibo(filter, since_date, mongodb_write, mysql_write,
                       pic_download, video_download)
            wb.start(new_list)

    except Exception as e:
        print('Error: ', e)
        traceback.print_exc()
Example #6
0
def main():
    ins = Ins()
    weibo = Weibo()
    ins.user = "******"

    while True:
        ins.checkUpdate()
        time.sleep(5)
        weibo.checkNewDirs()
        time.sleep(1200)
Example #7
0
 def load_session(self):
     self.weibo = None
     try:
         with open(SESSION_PATH, 'rb') as f:
             self.weibo = pickle.load(f)
     except OSError:  # 打开文件错误
         pass
     except pickle.PickleError:
         logger.exception('反序列化Weibo时出错:')
     if self.weibo is None:
         self.weibo = Weibo()
Example #8
0
 def postSingleFilm(self, url=None):
     status = self.parse_film_info(url) if url else self.pick_a_film(
         self.get_bangdan())
     status += "\n#电影# #电视剧#"
     print(status)
     if status:
         w = Weibo()
         w.login()
         w.postStatus(status, [self.cover])
         w.safeWaterFeeds()
         w.tearDown()
Example #9
0
 def reducer_get_user_info(self, key, _):
     # 避免代理失效
     for _ in range(3):
         try:
             wb = Weibo([key])
             wb.start()
             logger.info(wb.user.json())
             yield key, wb.user.json()
             break
         except:
             logger.error(f"{key} eroor")
     else:
         yield key, "fail"
Example #10
0
def run():
    weibo = Weibo()
    # 热搜
    searches, resp = weibo.get_hot_search()
    if resp:
        save_raw_content(resp.text, 'hot-search')
    # 话题榜
    topics, resp = weibo.get_hot_topic()
    if resp:
        save_raw_content(resp.text, 'hot-topic')

    # 最新数据
    readme = generate_readme(searches, topics)
    save_readme(readme)
    # 归档
    archiveMd = generate_archive_md(searches, topics)
    save_archive_md(archiveMd)
Example #11
0
    def new_weibo(self, weibo: Weibo) -> bool:
        '''
        sent a new Weibo
        :param weibo: a Weibo object
        :return: whether the Weibo sent successfully
        '''
        value = weibo.value()
        weibo_id = value['owner'] + '_' + str(value['sent_time'])
        weibo_owner = value['owner']
        weibo_time = value['sent_time']
        weibo_context = value['context']
        if value['comments']:
            weibo_comments = str(value['comments'])
        else:
            weibo_comments = ''

        sparql_check = '''
        select ?id 
        where
        {
        ?id <username> "%s".
        }
        ''' % weibo_owner
        self.Gconn.load(user_db, self.admin, self.password)
        result_check = self.Gconn.query(self.admin, self.password, user_db,
                                        sparql_check)
        json_check = json.loads(result_check)
        self.Gconn.unload(user_db, self.admin, self.password)
        if json_check["StatusCode"] == 0:
            if json_check["results"]["bindings"]:
                owner_id = json_check["results"]["bindings"][0]["id"]["value"]
            else:
                raise UserError("there is no such username")
        else:
            raise UserError(json_check["StatusMsg"])

        sparql_insert = '''
        insert data
        {
        <%s> <owner> <%s>.
        <%s> <context> "%s".
        <%s> <comments> "%s".
        <%s> <time> "%d".
        }''' % (weibo_id, owner_id, weibo_id, weibo_context, weibo_id,
                weibo_comments, weibo_id, weibo_time)
        print(sparql_insert)
        self.Gconn.load(user_db, self.admin, self.password)
        result_insert = self.Gconn.query(self.admin, self.password, user_db,
                                         sparql_insert)
        self.Gconn.unload(user_db, self.admin, self.password)
        self.Gconn.checkpoint(user_db, self.admin, self.password)
        json_insert = json.loads(result_insert)
        if json_insert["StatusCode"] == 402:
            return True
        else:
            raise UserError(json_insert["StatusMsg"])
Example #12
0
def login(username, passwd):
    '''
    登录
    '''
    weibo = Weibo(username, passwd)
    login = weibo.login()
    if not login[0]:
        try:
            print login[1]
        except:
            print login[1].encode('utf-8')

        sys.exit(1)
    try:
        print 'success login\nuid= %s,' % login[1], 'nick=', login[2]
    except:
        print 'success login\nuid= %s,' % login[1], 'nick=', login[2].encode(
            'utf-8')
    return weibo
Example #13
0
    def _on_cookie_added(self, cookie):
        if cookie.name() == b'SUB':
            try:
                cookie_sub = cookie.value().data().decode()
                cookies = {'SUB': cookie_sub}
                if Weibo(cookies).is_login():
                    self._weibo_cookies = cookies
                    self.accept()
                else:
                    logger.debug('无效的Cookie:%s', cookie_sub)

            except:
                logger.exception('获取登录状态时出错:')
Example #14
0
class Push(object):
    def __init__(self,
                 token=None,
                 keyWord=None,
                 weiboRef=None,
                 weiboCookie=None,
                 weiboSCF=None,
                 weixinToken=None):
        self.useDingTalk = False
        self.useWeibo = False
        self.useSCF = False
        self.useWeixin = False
        if token and keyWord:
            self.useDingTalk = True
            self.d = DingtalkChatbot(
                'https://oapi.dingtalk.com/robot/send?access_token=%s' % token)
            self.keyWord = keyWord
        if weiboRef and weiboCookie:
            self.useWeibo = True
            self.weibo = Weibo(weiboRef, weiboCookie)
        if weiboSCF:
            self.useSCF = True
            self.weiboSCF = weiboSCF
        if weixinToken:
            self.useWeixin = True
            self.wxurl = 'https://sc.ftqq.com/%s.send' % weixinToken

    def sendMsg(self, title, msg, is_at_all=False):
        if self.useDingTalk:
            self.d.send_markdown(title=self.keyWord + title,
                                 text=msg,
                                 is_at_all=is_at_all)
        if self.useWeibo:
            self.weibo.send(msg)
        if self.useSCF:
            requests.post(self.weiboSCF, data=msg.encode("utf-8"))
        if self.useWeixin:
            data = {'text': title, 'desp': msg}
            requests.post(self.wxurl, data=data)
Example #15
0
def main():
    queue = Message(config.Redis, config.RedisKey)
    weibo = Weibo(config.ChromeDriver, callback)
    while True:
        try:
            msg = queue.getMessage()
            if msg is not None:
                msg = msg.decode()
                if msg == 'debug':
                    weibo.debug("debug")
                    continue
                log.info("检测到消息,准备发送")
                weibo.postWeibo(msg)
        except Exception:
            queue.reAddMessage(msg)
            weibo.debug("exception")
            log.error("error: %s", traceback.format_exc())
            weibo.browser.refresh()
        time.sleep(10)
Example #16
0
def update_data():
    # old_tweet = OwlTweet()
    nga = Nga()
    weibo = Weibo()
    object_data = {
        'Transfer': {
            'data': [] + weibo.new_items + nga.new_items,
            'id_key': 'id'
        },
    }

    for name, info in object_data.items():
        data_objects = []
        LEANCLOUD_OBJECT_DATA = load_json(os.path.join('leancloud_data', name))
        data_dict = {}
        for item in info['data']:
            if data_changed(
                    LEANCLOUD_OBJECT_DATA.get(
                        object_id_key(name, item.get(info['id_key'])), {}),
                    item):
                if info['id_key'] not in item:
                    continue
                data_objects.append(
                    leancloud_object(name, item, info['id_key']))
            data_dict[item.get(info['id_key'])] = item
        print(name + " Total Count:" + str(len(info['data'])))
        print(name + " Changed Count:" + str(len(data_objects)))
        i = 0
        batch_size = 20
        while True:
            if len(data_objects[i:i + batch_size]) > 0:
                leancloud.Object.save_all(data_objects[i:i + batch_size])
                i += batch_size
            else:
                break
        for data_object in data_objects:
            OBJECT_ID_MAP[object_id_key(name, data_object.get(
                info['id_key']))] = data_object.id
            LEANCLOUD_OBJECT_DATA[object_id_key(
                name,
                data_object.get(info['id_key']))] = data_dict[data_object.get(
                    info['id_key'])]
        write_json('local_config/object_id_map.json', OBJECT_ID_MAP)
        write_json(os.path.join('leancloud_data', name), LEANCLOUD_OBJECT_DATA)
Example #17
0
with open(home + '/.send_wave/config.cfg', 'r') as cfgfile:
    config.readfp(cfgfile)
    try:
        access_token = config.get('weibo', 'access_token')
    except ConfigParser.NoOptionError, err:
        print "未配置weibo"
        exit(1)
    if not access_token:
        print "未配置weibo"
        exit(1)


if __name__ == "__main__":
    msg = ''
    for line in fileinput.input():
        msg += line

    weibo = Weibo(access_token=access_token)
    msglen = len(unicode(msg, 'utf-8'))
    if  msglen < 140:
        weibo.post_msg(msg)
    else:
        msgs = [msg.decode('utf8')[i:i+132].encode('utf8')
                for i in range(0, msglen, 132)]
        for m in msgs:
            if m is msgs[0]:
                m = m + '(未完)'
            else:
                m = '接上条: ' + m
            weibo.post_msg(m)
Example #18
0
def main(filepath):

    w = Weibo(00000, filter=0)
    w.get_weibo_from_file(filepath)
    w.write_csv(filepath.replace("txt", "csv"))
Example #19
0
from config import EMAIL, PASSWD, COOKIE_FILE, UID
from weibo import Weibo
import db
import os
import random
from time import sleep as _sleep

import requests_cache
requests_cache.configure('cache')

weibo = Weibo(EMAIL, PASSWD, COOKIE_FILE)

weibo.load_cookies()

def login():
    weibo.login()
    weibo.dump_cookies()

def get_friends(fans, follow):
    friends = []
    for fan in fans:
        for fo in follow:
            if fan == fo:
                friends.append(fan)
                break
    return friends

def get_myrelation():
    db.add_queue(UID)
    fans = []
    for fan in weibo.get_myfans(UID):
Example #20
0
if __name__ == "__main__":
    help = "Usage: %s [option]" % sys.argv[0]
    help += """\noption:
    -h | --help                                 display this information
    -s | --desc "photo description"             discription the photo
    """
    short_opts = "hs:"
    opts = ["help", "desc="]
    try:
        opts, args = getopt.getopt(sys.argv[1:], short_opts, opts)
    except getopt.GetoptError as err:
        print (err)
        print (help)
        sys.exit(1)

    desc = "pic"
    for opt, arg in opts:
        if opt in ("-h", "--help"):
            print (help)
            sys.exit()
        elif opt in ("-s", "--desc"):
            desc = arg
        else:
            print (help)
            sys.exit(1)

    photo_data = sys.stdin

    weibo = Weibo(access_token=access_token)
    weibo.post_photo(photo_data, desc=desc)
Example #21
0
#!/usr/bin/env python3
# dependencies
import sys
from datetime import datetime
from weibo import Weibo

# configuration parameters
from config import cookie
from config import filter
from config import connection_timeout
from config import pause_interval
from config import pause_time

# 更新微博
w = Weibo(5461287018, filter=0)
w.set_cookie(cookie)
w.connection_timeout = connection_timeout
w.pause_interval = pause_interval
w.pause_time = pause_time
w.start()
Example #22
0
from google.appengine.api import memcache
from google.appengine.ext import db
import sys, os
from zhan  import Zhan
from weibo import Weibo
from config import *

class MyData(db.Model):
    last_id = db.StringProperty(required=True)

if __name__ == "__main__":
    data = MyData.get_or_insert(key_name='mydata', last_id="feed_3674946092032508824")
    last_post_id = data.last_id

    weibo = Weibo()
    zhan = Zhan("ishoothust")
    new_posts = zhan.get_new_posts(last_post_id)

    for post in new_posts:
        image_url = post["image_url"].encode("utf-8")
        msg = "#我们爱拍华科#%s " % post["title"].encode("utf-8")
        count = post["photo_count"] - 1
        msg += "还有%d张精彩照片呦:" % count if count else " "
        msg += post["link"].encode("utf-8")
        weibo.send(msg, image_url)
        last_post_id = post["id"]

    data.last_id = last_post_id
    data.put()
Example #23
0
#!/usr/bin/env python3
# dependencies
import sys
from datetime import datetime
from utilities import stream_tee
from weibo import Weibo

# configuration parameters
from config import cookie
from config import filter
from config import user_id
from config import connection_timeout
from config import pause_interval
from config import pause_time

# 日志
logname = datetime.now().strftime('%Y-%m-%d-%H-%M') + '.log'
logfile = open(logname, "w+")
sys.stdout = stream_tee(sys.stdout, logfile)

# 读取微博
w = Weibo(user_id, filter=0)
w.set_cookie(cookie)
w.connection_timeout = connection_timeout
w.pause_interval = pause_interval
w.pause_time = pause_time
w.update()

logfile.close()
Example #24
0
from config import EMAIL, PASSWD, COOKIE_FILE, UID
from weibo import Weibo
import db
import os
import random
from time import sleep as _sleep

import requests_cache
requests_cache.configure('cache')

weibo = Weibo(EMAIL, PASSWD, COOKIE_FILE)

weibo.load_cookies()


def login():
    weibo.login()
    weibo.dump_cookies()


def get_friends(fans, follow):
    friends = []
    for fan in fans:
        for fo in follow:
            if fan == fo:
                friends.append(fan)
                break
    return friends


def get_myrelation():
Example #25
0
class ReadTheWeibo(QObject):
    def __init__(self, *args):
        super().__init__(*args)

        # 微博session、API
        self.weibo = None
        # 登录
        self.load_session()
        if not self.weibo.is_login():
            dlg = LoginDlg()
            # 如果还是没登录则退出
            if not dlg.exec():
                sys.exit(0)
            self.weibo.cookies = dlg.weibo_cookies

        # 显示微博弹窗
        self.show_post = True
        # 微博弹窗
        self._popup_post = PopupPost(self)
        # 读出微博
        self.speak_post = True
        # TTS引擎
        self._tts = pyttsx3.init()
        self._tts.connect('finished-utterance', self._on_finish_speaking)

        # 微博队列
        self._post_queue = Queue()
        # 定时器、线程
        self._update_timer = QTimer(self)
        self._update_timer.timeout.connect(self._update_posts)
        self._tts_loop_thread = None

    def load_session(self):
        self.weibo = None
        try:
            with open(SESSION_PATH, 'rb') as f:
                self.weibo = pickle.load(f)
        except OSError:  # 打开文件错误
            pass
        except pickle.PickleError:
            logger.exception('反序列化Weibo时出错:')
        if self.weibo is None:
            self.weibo = Weibo()

    def save_session(self):
        try:
            with open(SESSION_PATH, 'wb') as f:
                self.weibo = pickle.dump(self.weibo, f)
        except (OSError, pickle.PickleError):
            logger.exception('序列化Weibo时出错:')

    def start(self):
        if self._tts_loop_thread is None:
            self._tts_loop_thread = Thread(target=self._tts.startLoop)
            self._tts_loop_thread.daemon = True
            self._tts_loop_thread.start()
        if not self._update_timer.isActive():
            self._update_timer.start(25 * 1000)
            self._update_posts()

    def stop(self):
        if self._update_timer.isActive():
            self._update_timer.stop()
        if self._tts_loop_thread is not None:
            self._tts.endLoop()
            self._tts_loop_thread.join()
            self._tts_loop_thread = None

    def _update_posts(self):
        """
        获取未读微博
        """

        try:
            n_unread = min(self.weibo.get_n_unread(), 20)
            # n_unread = 5  # 测试用
            if n_unread > 0:
                posts = self.weibo.get_friend_feed()[n_unread - 1::-1]
                for post in posts:
                    self._post_queue.put(post)
        except ConnectionResetError:
            pass
        except:
            logger.exception('获取新微博时出错:')

        self._process_new_post()

    def _process_new_post(self):
        """
        处理队列中的新微博,如果正在显示微博或队列为空则什么也不做
        """

        if (self._popup_post.isVisible() or self._tts.isBusy()
                or self._post_queue.empty()):
            return
        post = self._post_queue.get_nowait()
        logger.debug('处理微博:%s:%s\n剩余%d条', post.user_name, post.content,
                     self._post_queue.qsize())

        if self.show_post:
            self._popup_post.show_post(post)
        if self.speak_post:
            self._tts.say(self._filter_tts_content(post))

    @staticmethod
    def _filter_tts_content(post):
        """
        处理微博,准备TTS
        :param post: 微博
        :return: 处理后的微博内容
        """

        res = post.content
        for reg, replace in FILTER_PARAMS:
            res = reg.sub(replace, res)
        if post.is_repost:
            res = '{}转发微博,说:{}。原微博,{}'.format(
                post.user_name, res,
                ReadTheWeibo._filter_tts_content(post.original_post))
        else:
            res = '{}说:{}'.format(post.user_name, res)
        return res

    def on_popup_post_close(self):
        """
        微博弹窗被关闭
        """

        logger.debug('on_popup_post_close')
        # 如果是手动关闭,停止发声,如果是发声结束关闭,什么也不做
        self._tts.stop()

    def on_popup_post_hide(self):
        """
        微博弹窗被隐藏
        """

        logger.debug('on_popup_post_hide')
        self._process_new_post()

    def _on_finish_speaking(self, name, completed):
        """
        结束发声
        :param name: say的参数,未使用
        :param completed: 发声正常结束
        """

        logger.debug('_on_finish_speaking, completed = %s', completed)
        # 如果是手动关闭,什么也不做,如果是发声结束关闭,关闭窗口
        if completed:
            self._popup_post.close()
Example #26
0
def getWeibo(i):
    contrainerID = i["contrainerID"]
    weibo = Weibo(contrainerID)
    weiboID = i["weiboID"]
    print(f"正在扫描 {weiboID}")
    try:
        # 初次启动记录前十条微博id
        if first_check_dict[contrainerID] is True:
            weibo_id_array = weibo.IdArray
            weibo_id_dict[contrainerID] = weibo_id_array
            first_check_dict[contrainerID] = False

        if first_check_dict[contrainerID] is False:
            # 取最新的前三条微博
            for idcount in range(0, 3):
                # 广告位微博id为0,忽略
                if int(weibo.IdArray[idcount]) == 0:
                    continue
                # 微博id不在记录的id列表里,判断为新微博
                if weibo.IdArray[idcount] not in weibo_id_dict[contrainerID]:
                    # 将id计入id列表
                    weibo_id_dict[contrainerID].append(weibo.IdArray[idcount])

                    # 检查新微博是否是转发
                    if weibo.checkRetweet(idcount):
                        print("[INFO] IGNORE REPOST TEXT")
                    else:
                        text = weibo.getWeibo(idcount)
                        url = weibo.getScheme(idcount)

                        tag = True

                        for word in i['keyword']:
                            if word not in text:
                                tag = False

                        if tag is True:
                            print("FIND NEW MESSAGE")
                            if i['shieldingWords'] == "":
                                title = f"{weiboID} 微博更新提醒"
                                mail_msg = text + "\n" + url
                                send(mail_msg, title)

                            if i['shieldingWords'] != "" and i[
                                    'shieldingWords'] not in text:
                                title = f"{weiboID} 微博更新提醒"
                                mail_msg = text + "\n" + url
                                send(mail_msg, title)
                            else:
                                print("[INFO] FIND SHIELDINGWORDS,")

                        else:
                            print("[INFO] TEXT DON'T MATCH")

                    time.sleep(0.5)

                else:
                    pass

    except Exception as e:
        print('[ERROE] GET WEIBO FAILED', e)
    finally:
        pass
Example #27
0
import sys, os
from zhan import Zhan
from weibo import Weibo
from config import *


class MyData(db.Model):
    last_id = db.StringProperty(required=True)


if __name__ == "__main__":
    data = MyData.get_or_insert(key_name='mydata',
                                last_id="feed_3674946092032508824")
    last_post_id = data.last_id

    weibo = Weibo()
    zhan = Zhan("ishoothust")
    new_posts = zhan.get_new_posts(last_post_id)

    for post in new_posts:
        image_url = post["image_url"].encode("utf-8")
        msg = "#我们爱拍华科#%s " % post["title"].encode("utf-8")
        count = post["photo_count"] - 1
        msg += "还有%d张精彩照片呦:" % count if count else " "
        msg += post["link"].encode("utf-8")
        weibo.send(msg, image_url)
        last_post_id = post["id"]

    data.last_id = last_post_id
    data.put()
Example #28
0
def fix_images_over_nine():
    try:
        wb = Weibo(1, '2010-01-01', 0, 0, 0, 0)

        weibo_update_list = []

        # count = 1
        # tmp_count = 0
        # random_pages = random.randint(1, 5)
        """建立数据库连接"""
        conn = pymysql.connect(host=dbinfo_host,
                               user=dbinfo_user,
                               passwd=dbinfo_password,
                               db=dbinfo_db)
        cursor = conn.cursor()
        n = cursor.execute(
            "SELECT WEIBO_ID FROM weibo_info w JOIN weibo_user_info u ON w.USER_ID = u.USER_ID WHERE LENGTH(PICS) - LENGTH(REPLACE(PICS, ',', '')) = 8 AND w.CREATE_TIME >= '2019-10-01 00:00:00' AND w.CREATE_TIME < '2019-12-01 00:00:00' AND u.FLAG = '1'  AND u.STATUS = '1' AND u.BAN <> '1' ORDER BY WEIBO_ID"
        )
        conn.close()
        if n:
            # for row in cursor.fetchall():
            for row in tqdm(cursor.fetchall(), desc='progress'):
                weibo_id = row[0]
                print '爬取微博id:' + weibo_id
                weibo = wb.get_long_weibo(weibo_id)
                print '微博内容:' + str(weibo)
                if weibo:
                    pics = weibo['pics']
                    if pics:
                        if pics.find(",") >= 0:
                            pics_arr = pics.split(",")
                            if pics_arr.__len__() > 9:
                                print "ok"
                                weibo_update = {
                                    'weibo_id': row[0],
                                    'pics': pics
                                }
                                weibo_update_list.append(weibo_update)
                sleep(random.randint(1, 5))
                # if count - tmp_count == random_pages and count < n:
                #     sleep(random.randint(1, 3))
                #     tmp_count = count
                #     random_pages = random.randint(7, 20)
                # count = count + 1

        print "需要更新微博数:", len(weibo_update_list)
        conn = pymysql.connect(host=dbinfo_host,
                               user=dbinfo_user,
                               passwd=dbinfo_password,
                               db=dbinfo_db)
        cursor = conn.cursor()
        for weibo_update in weibo_update_list:
            weibo_id = weibo_update['weibo_id']
            pics = weibo_update['pics']
            cursor.execute(
                "UPDATE weibo_info SET PICS = %s WHERE WEIBO_ID = %s",
                (pics, weibo_id))
        conn.commit()
        conn.close()
    except Exception as e:
        print('Error: ', e)
        traceback.print_exc()
Example #29
0
def getWeibo():
    weibo = Weibo()
    try:
        INFO('check weibo')
        global weibo_id_array
        global firstcheck_weibo
        # 初次启动记录前十条微博id
        if firstcheck_weibo is True:
            INFO('first check weibo')
            weibo_id_array = weibo.IdArray
            firstcheck_weibo = False
        if firstcheck_weibo is False:
            # 取最新的前三条微博
            for idcount in range(0, 3):
                # 广告位微博id为0,忽略
                if int(weibo.IdArray[idcount]) == 0:
                    continue
                # 微博id不在记录的id列表里,判断为新微博
                if weibo.IdArray[idcount] not in weibo_id_array:
                    msg = []
                    # 将id计入id列表
                    weibo_id_array.append(weibo.IdArray[idcount])
                    # 检查新微博是否是转发
                    if weibo.checkRetweet(idcount):
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '小偶像刚刚转发了一条微博:\n'}})
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '%s\n' % weibo.getRetweetWeibo(idcount)}})
                    # 原创微博
                    else:
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '小偶像刚刚发了一条新微博:\n'}})
                        msg.append(
                            {
                                'type': 'text',
                                'data': {'text': '%s\n' % weibo.getWeibo(idcount)}})
                        # 检查原创微博是否带图
                        if weibo.checkPic(idcount):
                            # 只取第一张图,pro可以直接发图,air则无
                            msg.append(
                                {
                                    'type': 'image',
                                    'data': {'file': '%s' % weibo.getPic(idcount)[0]}})
                            # 播报图的总数
                            if len(weibo.getPic(idcount)) > 1:
                                msg.append(
                                    {
                                        'type': 'text',
                                        'data': {'text': '\n(一共有%d张图喔)\n' % len(weibo.getPic(idcount))}})
                    msg.append(
                        {
                            'type': 'text',
                            'data': {'text': '传送门:%s' % weibo.getScheme(idcount)}})
                    for grpid in groupid():
                        bot.send_group_msg_async(
                            group_id=grpid, message=msg, auto_escape=False)
                        time.sleep(0.5)
                    # print(msg)
    except Exception as e:
        WARN('error when getWeibo', e)
    finally:
        INFO('weibo check completed')
Example #30
0
# coding:utf-8
# 入口文件
from weibo import Weibo
from budejie import Budejie
import time
import random
from tmall import Tmall

cookie = "SUHB=0z1QSD6NPHGsYt; _T_WM=a506522edde4d5289f7200fe4ed0b4e5; SUB=_2A256EAuhDeTxGeNG6FoU8y3LzDWIHXVZ-pXprDV6PUJbrdBeLXKlkW1LHeuCotsx-20jafpfu6wnfUM7_oWA8A..; _TTT_USER_CONFIG_H5=%7B%22ShowMblogPic%22%3A1%2C%22ShowUserInfo%22%3A1%2C%22MBlogPageSize%22%3A10%2C%22ShowPortrait%22%3A1%2C%22CssType%22%3A0%2C%22Lang%22%3A1%7D; gsid_CTandWM=4uMFCpOz5wPOZPrZ3COuVouRTcz; M_WEIBOCN_PARAMS=uicode%3D20000061%26featurecode%3D20000180%26fid%3D3965617230628097%26oid%3D3965617230628097; H5_INDEX=0_all; H5_INDEX_TITLE=%E7%94%9F%E6%B4%BB%E5%BC%80%E5%BF%83%E5%B0%B1%E5%A5%BD%E5%93%88%E5%93%88"
weibo = Weibo(cookie)
budejie = Budejie()
tmall = Tmall()

#8-24点,每4个小时抓取一次
for i in range(4):
    list_duanzi = budejie.get_duanzi()
    list_goods = tmall.get_goods()
    #抓取的数据在4个小时内发布
    for j in range(4):
        #这是一个小时内的事情
        for k in range(6):
            #一个小时发布6条, 轮流发布商品和段子
            if (k%2==1):
                index = random.randint(0, len(list_duanzi) - 1)
                send_content = list_duanzi[index]
                list_duanzi.remove(send_content)
            else:
                index = random.randint(0, len(list_goods) - 1)
                g = list_goods[index]
                l = len(g["title"]+g["buy"])
                send_content = g['title']+">>"+g["summary"][0:140-l-10]+"..."+g["buy"]
Example #31
0
#encoding:utf-8
from weibo import Weibo

wb = Weibo()
wb.start()
wb.login()
wb.search(u"\u90ed\u5bcc\u57ce", page_count=50)