コード例 #1
0
ファイル: twitter.py プロジェクト: xdliuyong/follow_center
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 = tz.localize(tweet.created_at)
            tweet.created_at += timedelta(hours=8)
            # print tweet.created_at
            id = saveTwitter(copy.deepcopy(tweet))
            if id is not None and len(public_tweets) <= 3:  # 新增加消息
                openids = public_db.getOpenidsByName('twitter', screen_name)
                print 'new=', tweet.text
                for data in openids:
                    wechat_oper.sendTwitter(data.openid, tweet, screen_name, id)
    except tweepy.error.TweepError:
        print 'screen_name=', screen_name
        public_bz.getExpInfo()
コード例 #2
0
ファイル: github.py プロジェクト: xdliuyong/follow_center
def getUserEvent(user_name, etag):
    '''
    create by bigzhu at 15/07/15 17:54:08 取github
    modify by bigzhu at 15/07/22 16:20:42 时间同样要加入8小时,否则不正确
    '''
    headers = {'If-None-Match': etag}
    try:
        r = requests.get('https://api.github.com/users/%s/events' % user_name, headers=headers)
    except requests.exceptions.ConnectionError:
        print public_bz.getExpInfoAll()
        return
    if r.status_code == 200:
        messages = r.json()
        if not messages:
            delGithubUser(user_name)
            # 没有这个github用户,取消
            return
        actor = messages[0]['actor']
        #actor不定是作者名字,有可能org才是
        if actor['login'].lower() == user_name.lower():
            the_user = actor
        else:
            org = messages[0]['org']
            if org['login'].lower() == user_name.lower():
                the_user = org
                #如果是org,那么url不同
                the_user['url'] = "https://api.github.com/users/"+user_name
            else:
                raise("in this github can't find user_name=%s" % user_name)

        user_id = saveUser(the_user['id'], the_user['url'])

        # 更新etag
        etag = r.headers['etag']
        updateEtag(user_name, etag)

        for i in r.json():
            i['actor'] = user_id
            message = storage(i)
            message.created_at = time_bz.unicodeToDateTIme(message.created_at)
            message.created_at += timedelta(hours=8)
            id = saveMessage(copy.deepcopy(message))
            if id is not None:
                text = formatInfo(message)
                print text
                openids = public_db.getOpenidsByName('github', user_name)
                for data in openids:
                    wechat_oper.sendGithub(data.openid, text, user_name, id)
    else:
        print r.status_code
コード例 #3
0
def saveMedias(user, medias):
    '''
    create by bigzhu at 15/09/04 20:58:54 保存meedias

         "attribution":null,
         "tags":[  ],
         "type":"image",
         "location":{  },
         "comments":{  },
         "filter":"Normal",
         "created_time":"1441362020",
         "link":"https:\/\/instagram.com\/p\/7NIHiLJJs3\/",
         "likes":{  },
         "images":{  },
         "users_in_photo":[  ],
         "caption":{  },
         "user_has_liked":false,
         "id":"1066544388859271991_262341",
         "user":{  }
    '''

    for media_d in medias['data']:
        media = storage(media_d)
        db_media = storage()

        #db_media.attribution = media.attribution
        #db_media.tags = json.dumps(media.tags, cls=public_bz.ExtEncoder)
        db_media.type = media.type
        #db_media.location = json.dumps(media.location, cls=public_bz.ExtEncoder)
        db_media.comments = json.dumps(media.comments, cls=public_bz.ExtEncoder)
        db_media.filter = media.filter
        #db_media.created_time = time_bz.timestampToDateTime(media.created_time) + timedelta(hours=8)
        db_media.created_time = time_bz.timestampToDateTime(media.created_time)
        db_media.link = media.link
        #db_media.likes = json.dumps(media.likes, cls=public_bz.ExtEncoder)
        db_media.low_resolution = json.dumps(media.images['low_resolution'])
        db_media.standard_resolution = json.dumps(media.images['standard_resolution'])
        db_media.thumbnail = json.dumps(media.images['thumbnail'])
        #db_media.users_in_photo = json.dumps(media.users_in_photo, cls=public_bz.ExtEncoder)
        if media.caption:
            caption = media.caption
            caption['user_id'] = caption['from']['id']
            del caption['from']
        else:
            caption = ''
        db_media.caption = json.dumps(caption, cls=public_bz.ExtEncoder)

        db_media.id_str = media.id
        db_media.user_id = user.id

        id = pg.insertIfNotExist(pg, 'instagram_media', db_media, "id_str='%s'" % db_media.id_str)
        if id is None:
            raise Exception('重复记录 id=%s, name=%s' % (media.id, user.username))
        else:
            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['low_resolution']['url'], user.username, id)

    if medias['pagination']:
        next_url = medias['pagination']['next_url']
        medias = callGetMeidaApi(next_url=next_url)
        saveMedias(user, medias)
コード例 #4
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)