コード例 #1
0
ファイル: spider.py プロジェクト: JInghfut/example
def parser_song(song_id, artist):
    tree = get_tree(SONG_URL.format(song_id))
    if tree==None:
        print "fetch song url get none,return !"
        return
    song = Song.objects.filter(id=song_id)
    r= post(COMMENTS_URL.format(song_id),song_id)
    data=r.json()
    print 'get comment ok '+str(song_id)
    print str(data['code']) + str(song_id)
    if data['code'] != 200:
        print "fetch comment get cheating " + str(data['code']) + " program stop"
        return
    if not song:  #如果是新歌
        for404 = tree.xpath('//div[@class="n-for404"]')
        if for404:
            print "404 found"
            return

        try:
            song_name = tree.xpath('//em[@class="f-ff2"]/text()')[0].strip()
        except IndexError:
            try:
                song_name = tree.xpath(
                    '//meta[@name="keywords"]/@content')[0].strip()
            except IndexError:
                print 'Fetch limit!'
                time.sleep(10)
                return parser_song(song_id, artist)
        song = Song(id=song_id, name=song_name, artist=artist,
                    comment_count=data['total'])
        song.save()
    else:
        song = song[0]  #如果歌曲已存在
    comments = []

    for comment_ in data['hotComments']:
        comment_id = comment_['commentId']
        content = comment_['content']
        like_count = comment_['likedCount']
        user = comment_['user']
        if not user:
            continue
        user = User.get_or_create(id=user['userId'], name=user['nickname'],
                                  picture=user['avatarUrl'])
        comment = Comment.get_or_create(id=comment_id, content=content,
                                        like_count=like_count, user=user,
                                        song=song)
        comment.save()
        comments.append(comment)
    song.comments = comments
    song.save()
    print "song comments finish"
    time.sleep(0.5)
    return song
コード例 #2
0
ファイル: parser.py プロジェクト: cooljovi/commentbox
def parser_song(song_id, artist):
    tree = get_tree(SONG_URL.format(song_id))
    song = Song.objects.filter(id=song_id)
    r = post(COMMENTS_URL.format(song_id))  # 必须post一些东西才能获取评论信息
    if r.status_code != 200:
        print('API Error: Song {}'.format(song_id))
        return
    data = r.json()
    if not song:
        for404 = tree.xpath('//div[@class="n-for404"]')
        if for404:
            return

        try:
            song_name = tree.xpath('//em[@class="f-ff2"]/text()')[0].strip()
        except IndexError:
            try:
                song_name = tree.xpath(
                    '//meta[@name="keywords"]/@content')[0].strip()
            except IndexError:
                print('Fetch limit!')
                time.sleep(10)
                return parser_song(song_id, artist)
        song = Song(id=song_id,
                    name=song_name,
                    artist=artist,
                    comment_count=data['total'])
        song.save()
    else:
        song = song[0]
    comments = []
    for comment_ in data['hotComments']:
        comment_id = comment_['commentId']
        content = comment_['content']
        like_count = comment_['likedCount']
        user = comment_['user']
        if not user:
            continue
        user = User.get_or_create(id=user['userId'],
                                  name=user['nickname'],
                                  picture=user['avatarUrl'])
        comment = Comment.get_or_create(id=comment_id,
                                        content=content,
                                        like_count=like_count,
                                        user=user,
                                        song=song)
        comment.save()
        comments.append(comment)
    song.comments = comments
    song.save()
    time.sleep(1)
    return song
コード例 #3
0
ファイル: parser.py プロジェクト: cxiaodian/commentbox
def parser_song(song_id, artist):
    tree = get_tree(SONG_URL.format(song_id))
    song = Song.objects.filter(id=song_id)
    r = post(COMMENTS_URL.format(song_id))
    if r.status_code != 200:
        print 'API Error: Song {}'.format(song_id)
        return
    data = r.json()
    if not song:
        for404 = tree.xpath('//div[@class="n-for404"]')
        if for404:
            return

        try:
            song_name = tree.xpath('//em[@class="f-ff2"]/text()')[0].strip()
        except IndexError:
            try:
                song_name = tree.xpath(
                    '//meta[@name="keywords"]/@content')[0].strip()
            except IndexError:
                print 'Fetch limit!'
                time.sleep(10)
                return parser_song(song_id, artist)
        song = Song(id=song_id, name=song_name, artist=artist,
                    comment_count=data['total'])
        song.save()
    else:
        song = song[0]
    comments = []
    for comment_ in data['hotComments']:
        comment_id = comment_['commentId']
        content = comment_['content']
        like_count = comment_['likedCount']
        user = comment_['user']
        if not user:
            continue
        user = User.get_or_create(id=user['userId'], name=user['nickname'],
                                  picture=user['avatarUrl'])
        comment = Comment.get_or_create(id=comment_id, content=content,
                                        like_count=like_count, user=user,
                                        song=song)
        comment.save()
        comments.append(comment)
    song.comments = comments
    song.save()
    time.sleep(1)
    return song