Пример #1
0
def populate_models(tumblr_user, user):
    """
    Takes a tumblr username (string), and a User model. Populates the tumblr models
    with data from 'tumblr_user'.tumblr.com, and associates the entries with 'user'.
    """

    tumbls = Api(tumblr_user + ".tumblr.com")
    for tumbl in tumbls.read():
        # Common to all models
        id = tumbl["id"]
        pub_date = datetime.datetime.strptime(tumbl["date-gmt"], "%Y-%m-%d %H:%M:%S %Z")

        # 'Regular' objects.
        if tumbl["type"] == "regular":
            if tumbl["regular-title"]:
                title = tumbl["regular-title"]
            else:
                title = ""
            body = tumbl["regular-body"]
            m = Regular(id=id, pub_date=pub_date, user=user, title=title, body=body)

        # 'Photo' objects.
        elif tumbl["type"] == "photo":
            source = tumbl["photo-url-250"]
            if tumbl["photo-caption"]:
                caption = tumbl["photo-caption"]
            else:
                caption = ""
            m = Photo(id=id, pub_date=pub_date, user=user, source=source, caption=caption)

        # 'Quote' objects.
        elif tumbl["type"] == "quote":
            quote = tumbl["quote-text"]
            if tumbl["quote-source"]:
                source = tumbl["quote-source"]
            else:
                source = ""
            m = Quote(id=id, pub_date=pub_date, user=user, quote=quote, source=source)

        # 'Link' objects.
        elif tumbl["type"] == "link":
            if tumbl["link-text"]:
                name = tumbl["link-text"]
            else:
                name = ""
            url = tumbl["link-url"]
            if tumbl["link-description"]:
                description = tumbl["link-description"]
            else:
                description = ""
            m = Link(id=id, pub_date=pub_date, user=user, name=name, url=url, description=description)

        # 'Conversation' objects.
        elif tumbl["type"] == "conversation":
            if tumbl["conversation-title"]:
                title = tumbl["conversation-title"]
            else:
                title = ""
            m = Conversation(
                id=id, pub_date=pub_date, user=user, title=title, conversation_text=tumbl["conversation-text"]
            )
            m.save()

        # 'Video' objects.
        elif tumbl["type"] == "video":
            embed = tumbl["video-player"]
            if tumbl["video-caption"]:
                caption = tumbl["video-caption"]
            else:
                caption = ""
            m = Video(id=id, pub_date=pub_date, user=user, embed=embed, caption=caption)

        # 'Audio' objects.
        elif tumbl["type"] == "audio":
            embed = tumbl["audio-player"]
            if tumbl["audio-caption"]:
                caption = tumbl["audio-caption"]
            else:
                caption = ""
            m = Audio(id=id, pub_date=pub_date, user=user, embed=embed, caption=caption)

        # TODO: Raise error.
        else:
            print "ERROR!", tumbl
            return ""

        m.save()
Пример #2
0
    def populate_models(self, tumblr_user, user):
        """
        Takes a tumblr username (string), and a User model. Populates the tumblr models
        with data from 'tumblr_user'.tumblr.com, and associates the entries with 'user'.
        """
        tumbls = Api(tumblr_user + ".tumblr.com")
        for tumbl in tumbls.read():
            # Common to all models
            tumblr_id = tumbl["id"]
            pub_date = datetime.datetime.strptime(tumbl["date-gmt"], "%Y-%m-%d %H:%M:%S %Z")
            format = tumbl["format"]
            tags = tumbl.get("tags", [])
            self.log.debug("%s (%s)" % (tumblr_id, tumbl["type"]))

            try:
                TumbleItem.objects.get(tumblr_id=tumblr_id)

            except TumbleItem.DoesNotExist:
                try:
                    # 'Regular' objects.
                    if tumbl["type"] == "regular":
                        title = tumbl.get("regular-title", "")
                        body = tumbl["regular-body"]
                        m = Regular(
                            tumblr_id=tumblr_id, pub_date=pub_date, user=user, format=format, title=title, body=body
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Photo' objects.
                    elif tumbl["type"] == "photo":
                        source = tumbl["photo-url-500"]
                        caption = tumbl.get("photo-caption", "")
                        m = Photo(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            source=source,
                            caption=caption,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Quote' objects.
                    elif tumbl["type"] == "quote":
                        quote_text = tumbl.get("quote-text", "")
                        source = tumbl.get("quote-source", "")
                        m = Quote(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            quote_text=quote_text,
                            source=source,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Link' objects.
                    elif tumbl["type"] == "link":
                        name = tumbl.get("link-text", "")
                        url = tumbl["link-url"]
                        description = tumbl.get("link-description", "")
                        m = Link(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            name=name,
                            url=url,
                            description=description,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Conversation' objects.
                    elif tumbl["type"] == "conversation":
                        title = tumbl.get("conversation-title", "")
                        conversation_text = tumbl["conversation-text"]
                        m = Conversation(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            title=title,
                            conversation_text=conversation_text,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Video' objects.
                    elif tumbl["type"] == "video":
                        embed = tumbl["video-player"]
                        caption = tumbl.get("video-caption", "")
                        m = Video(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            embed=embed,
                            caption=caption,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # 'Audio' objects.
                    elif tumbl["type"] == "audio":
                        embed = tumbl["audio-player"]
                        caption = tumbl.get("audio-caption", "")
                        m = Audio(
                            tumblr_id=tumblr_id,
                            pub_date=pub_date,
                            user=user,
                            format=format,
                            embed=embed,
                            caption=caption,
                        )
                        m.save()
                        m.tags.add(*tags)

                    # TODO: Raise error.
                    else:
                        self.log.error("Type does not exist: %s" % (tumbl["type"]))

                except Exception, e:
                    self.log.exception(e)
Пример #3
0
def populate_models(tumblr_user, user):
    '''
    Takes a tumblr username (string), and a User model. Populates the tumblr models
    with data from 'tumblr_user'.tumblr.com, and associates the entries with 'user'.
    '''

    tumbls = Api(tumblr_user + ".tumblr.com")
    for tumbl in tumbls.read():
        # Common to all models
        id = tumbl['id']
        pub_date = datetime.datetime.strptime(tumbl['date-gmt'],
                                              '%Y-%m-%d %H:%M:%S %Z')

        # 'Regular' objects.
        if tumbl['type'] == "regular":
            if tumbl['regular-title']:
                title = tumbl['regular-title']
            else:
                title = ""
            body = tumbl['regular-body']
            m = Regular(id=id,
                        pub_date=pub_date,
                        user=user,
                        title=title,
                        body=body)

        # 'Photo' objects.
        elif tumbl['type'] == "photo":
            source = tumbl['photo-url-250']
            if tumbl['photo-caption']:
                caption = tumbl['photo-caption']
            else:
                caption = ""
            m = Photo(id=id,
                      pub_date=pub_date,
                      user=user,
                      source=source,
                      caption=caption)

        # 'Quote' objects.
        elif tumbl['type'] == "quote":
            quote = tumbl['quote-text']
            if tumbl['quote-source']:
                source = tumbl['quote-source']
            else:
                source = ""
            m = Quote(id=id,
                      pub_date=pub_date,
                      user=user,
                      quote=quote,
                      source=source)

        # 'Link' objects.
        elif tumbl['type'] == "link":
            if tumbl['link-text']:
                name = tumbl['link-text']
            else:
                name = ""
            url = tumbl['link-url']
            if tumbl['link-description']:
                description = tumbl['link-description']
            else:
                description = ""
            m = Link(id=id,
                     pub_date=pub_date,
                     user=user,
                     name=name,
                     url=url,
                     description=description)

        # 'Conversation' objects.
        elif tumbl['type'] == "conversation":
            if tumbl['conversation-title']:
                title = tumbl['conversation-title']
            else:
                title = ""
            m = Conversation(id=id,
                             pub_date=pub_date,
                             user=user,
                             title=title,
                             conversation_text=tumbl['conversation-text'])
            m.save()

        # 'Video' objects.
        elif tumbl['type'] == "video":
            embed = tumbl['video-player']
            if tumbl['video-caption']:
                caption = tumbl['video-caption']
            else:
                caption = ""
            m = Video(id=id,
                      pub_date=pub_date,
                      user=user,
                      embed=embed,
                      caption=caption)

        # 'Audio' objects.
        elif tumbl['type'] == "audio":
            embed = tumbl['audio-player']
            if tumbl['audio-caption']:
                caption = tumbl['audio-caption']
            else:
                caption = ""
            m = Audio(id=id,
                      pub_date=pub_date,
                      user=user,
                      embed=embed,
                      caption=caption)

        # TODO: Raise error.
        else:
            print "ERROR!", tumbl
            return ''

        m.save()