示例#1
0
文件: utils.py 项目: ygmpkk/kindleio
def create_or_update_user(user_id, attr):
    """
    Currently, attr must be one of ("douban", "twitter")
    """
    if not user_id or not attr:
        return

    if attr not in ("douban", "twitter"):
        return

    username = attr + "_" + user_id
    if User.objects.filter(username=username).exists():
        user = User.objects.get(username=username)
    else:
        user = User.objects.create_user(username=username)

    try:
        profile = user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile.objects.create(user=user)

    attr_name = attr + '_id'
    if getattr(profile, attr_name) != user_id:
        setattr(profile, attr_name, user_id)
        profile.save()
        if attr == 'twitter':
            try:
                api = get_twitter_private_api()
                api.CreateFriendship(user_id)
            except:
                pass
    user_logged_in.send(sender=user.__class__, user=user)
    return user
示例#2
0
文件: utils.py 项目: ygmpkk/kindleio
def set_user_twitter_token(user, screen_name, token_string):
    try:
        profile = user.get_profile()
    except UserProfile.DoesNotExist:
        profile = UserProfile.objects.create(user=user)
    profile.twitter_id = screen_name
    profile.twitter_token = token_string
    profile.save()
    try:
        api = get_twitter_private_api()
        api.CreateFriendship(screen_name)
    except:
        pass
示例#3
0
文件: views.py 项目: ygmpkk/kindleio
def check(request):
    api = get_twitter_private_api()
    try:
        messages = api.GetHomeTimeline(count=100)
    except:
        logger.error("Error when GetHomeTimeline when checking Notes")
        return HttpResponse("Error.")
    info = []
    for msg in messages:
        if "#Kindle" not in msg.text or 'http' not in msg.text:
            continue
        url = re.search(r'(http[^ ]+)', msg.text).group(1)
        if Note.objects.filter(url=url).exists() or Word.objects.filter(url=url).exists():
            continue
        user = get_user_from_twitter_id(msg.user.screen_name)
        if not user:
            continue
        added = datetime.datetime(*rfc822.parsedate(msg.created_at)[:6])
        info.append((user, url, added, msg.id))
    count = 0
    for user, url, added, tweet_id in info:
        save_note(user, url, added, tweet_id)
        count +=  1
    return HttpResponse("%s\n" % count)