示例#1
0
def main(user_name=None):
    blogs = callGetMeidaApi(user_name)
    if blogs is None:
        public_db.delNoName('tumblr', user_name)
    else:
        blogs = blogs['response']
        saveUserCheckNew(blogs)
示例#2
0
def getUser(user_name):
    '''
    modify by bigzhu at 15/07/31 16:41:16 api找用户时是按专注度来排序的,名字绝对匹配的未必是第一位, 从10个里面找
    '''
    users = list(pg.select('instagram_user', where="lower(username)=lower('%s')" % user_name))
    if users:
        return users[0]
    else:
        user = None
        for this_user in api.user_search(user_name, 10):
            if this_user.username.lower() == user_name.lower():
                user = this_user
                break
        #如果没有这个用户
        if user is None:
            public_db.delNoName('instagram', user_name)
        #user = api.user_search(user_name, 1)[0]
        user = api.user(user.id)

        db_user = storage()
        db_user.id = user.id
        db_user.username = user.username
        db_user.full_name = user.full_name
        db_user.profile_picture = user.profile_picture
        db_user.bio = user.bio
        db_user.website = user.website
        db_user.counts = json.dumps(user.counts)
        pg.insert('instagram_user', **db_user)
        #db_bz.insertIfNotExist(pg, 'instagram_user', db_user, "id=%s" % db_user.id)
        return getUser(user_name)
示例#3
0
def noMessageTooLong(m_type, name):
    sql = '''
    select * from message where m_type='%s' and name='%s' order by created_at desc limit 1
    ''' % (m_type, name)
    # last_message = public_db.getNewMessages(god_name=name, m_type=m_type, limit=1)
    last_message = pg.db.query(sql)
    if last_message:
        last_message_time = last_message[0].created_at
        if (datetime.datetime.now() - last_message_time).days >= 180:
            print('no message too long')
            public_db.delNoName(m_type, name)
示例#4
0
    def put(self):
        self.set_header("Content-Type", "application/json")
        data = json.loads(self.request.body)
        social_name = data['social_name']
        if social_name == '' or social_name is None:
            raise Exception('必须有god名字才能修改')
        type = data['type']
        if type == '' or type is None:
            raise Exception('必须有type才能修改')

        count = public_db.delNoName(type, social_name)
        # if count != 1:
        #     raise Exception("修改失败 type:%s social_name: %s count: %s" % (type, social_name, count))

        sql = ''' update apply_del set stat=1 where social_name='%s' and type='%s' and stat is null''' % (
            social_name, type)
        count = pg.query(sql)
        if count != 1:
            raise Exception("修改失败" + count)

        self.write(
            json.dumps({
                'error': '0',
                'count': count
            }, cls=json_bz.ExtEncoder))
示例#5
0
def getUser(user_name, always_check=False):
    '''
    modify by bigzhu at 15/07/31 16:41:16 api找用户时是按专注度来排序的,名字绝对匹配的未必是第一位, 从10个里面找
    '''
    users = list(
        pg.select('instagram_user',
                  where="lower(username)=lower('%s')" % user_name))
    if users and not always_check:
        return users[0]
    else:
        user = None
        try:
            for this_user in api.user_search(user_name, 10):
                if this_user.username.lower() == user_name.lower():
                    user = this_user
                    break
        except instagram.bind.InstagramClientError:
            print public_bz.getExpInfoAll()
            return
        # 如果没有这个用户
        if user is None:
            public_db.delNoName('instagram', user_name)
            return
        try:
            user = api.user(user.id)
        except (instagram.bind.InstagramAPIError,
                instagram.bind.InstagramClientError):
            # 通常是没有访问权限
            print public_bz.getExpInfoAll()
            public_db.delNoName('instagram', user_name)
            return

        db_user = storage()
        db_user.id_str = user.id
        db_user.username = user.username
        db_user.full_name = user.full_name
        db_user.profile_picture = user.profile_picture
        db_user.bio = user.bio
        db_user.website = user.website
        db_user.counts = json.dumps(user.counts)
        #pg.insert('instagram_user', **db_user)
        pg.insertOrUpdate(pg, 'instagram_user', db_user,
                          "id_str='%s'" % db_user.id_str)
        #db_bz.insertIfNotExist(pg, 'instagram_user', db_user, "id=%s" % db_user.id)
        return getUser(user_name)
示例#6
0
def getUser(user_name, always_check=False):
    '''
    modify by bigzhu at 15/07/31 16:41:16 api找用户时是按专注度来排序的,名字绝对匹配的未必是第一位, 从10个里面找
    '''
    users = list(pg.select('instagram_user', where="lower(username)=lower('%s')" % user_name))
    if users and not always_check:
        return users[0]
    else:
        user = None
        try:
            for this_user in api.user_search(user_name, 10):
                if this_user.username.lower() == user_name.lower():
                    user = this_user
                    break
        except instagram.bind.InstagramClientError:
            print public_bz.getExpInfoAll()
            return
        # 如果没有这个用户
        if user is None:
            public_db.delNoName('instagram', user_name)
            return
        try:
            user = api.user(user.id)
        except (instagram.bind.InstagramAPIError, instagram.bind.InstagramClientError):
            # 通常是没有访问权限
            print public_bz.getExpInfoAll()
            public_db.delNoName('instagram', user_name)
            return

        db_user = storage()
        db_user.id_str = user.id
        db_user.username = user.username
        db_user.full_name = user.full_name
        db_user.profile_picture = user.profile_picture
        db_user.bio = user.bio
        db_user.website = user.website
        db_user.counts = json.dumps(user.counts)
        #pg.insert('instagram_user', **db_user)
        pg.insertOrUpdate(pg, 'instagram_user', db_user, "id_str='%s'" % db_user.id_str)
        #db_bz.insertIfNotExist(pg, 'instagram_user', db_user, "id=%s" % db_user.id)
        return getUser(user_name)
示例#7
0
def main(user_name=None):
    user = getUser(user_name, always_check=True)
    if user is None:
        # 用户都没有,不用往下了
        return
    try:
        # https://api.instagram.com/v1/users/1337827037/media/recent/?access_token=1337827037.933ab14.2a607a5fc0534f9f9900e75196a2dfbb&min_id=1054034416535329463_1337827037
        # 即使设置了min_id,instagram还是会把当前这条min_id返回来,简直了
        #medias, next_ = api.user_recent_media(user_id=user.id, min_id=user.last_id)
        medias = callGetMeidaApi(user.id, user.last_id)
    except instagram.bind.InstagramClientError:
        print public_bz.getExpInfoAll()
        public_db.delNoName('instagram', user_name)
        return
    if medias is None:
        return

    saveLastId(user, medias)
    if len(medias['data']) != 0:
        print len(medias['data'])
    saveMedias(user, medias)
示例#8
0
def main(user_name=None):
    user = getUser(user_name, always_check=True)
    if user is None:
        # 用户都没有,不用往下了
        return
    try:
        # https://api.instagram.com/v1/users/1337827037/media/recent/?access_token=1337827037.933ab14.2a607a5fc0534f9f9900e75196a2dfbb&min_id=1054034416535329463_1337827037
        # 即使设置了min_id,instagram还是会把当前这条min_id返回来,简直了
        #medias, next_ = api.user_recent_media(user_id=user.id, min_id=user.last_id)
        medias = callGetMeidaApi(user.id, user.last_id)
    except instagram.bind.InstagramClientError:
        print public_bz.getExpInfoAll()
        public_db.delNoName('instagram', user_name)
        return
    if medias is None:
        return

    saveLastId(user, medias)
    if len(medias['data']) != 0:
        print len(medias['data'])
    saveMedias(user, medias)
示例#9
0
def getUserTimeline(screen_name):
    '''
    create by bigzhu at 15/07/04 22:49:04
        用 https://api.twitter.com/1.1/statuses/user_timeline.json 可以取到某个用户的信息
        参看 https://dev.twitter.com/rest/reference/get/statuses/user_timeline
    modify by bigzhu at 15/07/04 22:53:09
        考虑使用 http://www.tweepy.org/ 来调用twitter api
    modify by bigzhu at 15/08/02 21:35:46 避免批量微信通知
    '''
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth)
    try:
        public_tweets = api.user_timeline(screen_name=screen_name)
        for tweet in public_tweets:
            tweet.created_at += timedelta(hours=8)
            saveTwitter(copy.deepcopy(tweet))
    except tweepy.error.TweepError:
        print 'screen_name=', screen_name
        error_info = public_bz.getExpInfo()
        print error_info
        if 'Sorry, that page does not exist.' in error_info:
            public_db.delNoName('twitter', screen_name)
示例#10
0
def getUserTimeline(screen_name):
    '''
    create by bigzhu at 15/07/04 22:49:04
        用 https://api.twitter.com/1.1/statuses/user_timeline.json 可以取到某个用户的信息
        参看 https://dev.twitter.com/rest/reference/get/statuses/user_timeline
    modify by bigzhu at 15/07/04 22:53:09
        考虑使用 http://www.tweepy.org/ 来调用twitter api
    modify by bigzhu at 15/08/02 21:35:46 避免批量微信通知
    '''
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    api = tweepy.API(auth)
    try:
        public_tweets = api.user_timeline(screen_name=screen_name)
        for tweet in public_tweets:
            tweet.created_at += timedelta(hours=8)
            saveTwitter(copy.deepcopy(tweet))
    except tweepy.error.TweepError:
        print 'screen_name=', screen_name
        error_info = public_bz.getExpInfo()
        print error_info
        if 'Sorry, that page does not exist.' in error_info:
            public_db.delNoName('twitter', screen_name)
示例#11
0
def getMedia(user_name=None, with_next_url=None, user=None):
    if user_name:
        user = getUser(user_name)
        if user is None:
            return
        # min_id 会查出大于等于这个id的
        try:
            medias, next_ = api.user_recent_media(user_id=user.id, min_id=user.last_id)
        except instagram.bind.InstagramClientError:
            print public_bz.getExpInfoAll()
            public_db.delNoName('instagram', user_name)
            return
        if medias:
            last_id = medias[0].id
            pg.update('instagram_user', where="lower(username)=lower('%s')" % user_name, last_id=last_id)
    else:
        medias, next_ = api.user_recent_media(with_next_url=with_next_url)

    for media in medias:
        db_media = storage()
        if media.caption:
            caption = media.caption.__dict__
            caption['user_id'] = caption['user'].id
            del caption['user']
        else:
            caption = ''
        db_media.caption = json.dumps(caption, cls=public_bz.ExtEncoder)
        db_media.comment_count = media.comment_count

        if media.comments:
            media.comments = [d.__dict__ for d in media.comments]
            for comment in media.comments:
                comment['user'] = comment['user'].__dict__
        db_media.comments = json.dumps(media.comments, cls=public_bz.ExtEncoder)
        db_media.created_time = media.created_time
        # 8小时的问题
        db_media.created_time += timedelta(hours=8)
        db_media.filter = media.filter
        db_media.low_resolution = json.dumps(media.images['low_resolution'].__dict__)
        db_media.standard_resolution = json.dumps(media.images['standard_resolution'].__dict__)
        db_media.thumbnail = json.dumps(media.images['thumbnail'].__dict__)
        db_media.id_str = media.id
        db_media.like_count = media.like_count
        # likes里有User对象,暂时不存了
        #db_media.likes = json.dumps(media.likes)
        db_media.link = media.link
        db_media.type = media.type
        db_media.user_id = user.id
        id = db_bz.insertIfNotExist(pg, 'instagram_media', db_media, "id_str='%s'" % db_media.id_str)
        print 'new=', media.id, user.username
        if id is not None and len(medias) <= 2:  # 新增加消息,微信通知只通知2条以内
            openids = public_db.getOpenidsByName('instagram', user.username)
            for data in openids:
                if caption != '':
                    text = caption.get('text')
                else:
                    text = ''
                wechat_oper.sendInstagram(data.openid, text, media.images['standard_resolution'].url, user.username, id)
    # 递归查出
    if next_ != with_next_url:
        getMedia(with_next_url=next_, user=user)