mongona

mongona
-- --
正在获取天气

网易云我的喜欢音乐下载

import json
import hashlib
import time

import requests
import re
import threading
from pymongo import MongoClient
from qiniu import Auth, put_data

import multiprocessing
import sys

sys.path.insert(0, '/data/MyBlog')
from MyBlog.settings import MEDIA_URL
from MyBlog.utils import redis_client
from constants import MONGO_URI, MONGODB_NAME, QINIU_ACCESS_KEY, QINIU_SECRET_KEY, QINIU_BUCKET_NAME


class QiNiu(object):
    def __init__(self):
        self.user = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY)
        self.bucket_name = QINIU_BUCKET_NAME

    def up_stream(self, stream, key):
        token = self.user.upload_token(self.bucket_name, key, 3600)
        return put_data(token, key, stream, progress_handler=True)

def get_mongo():
    return MongoClient(MONGO_URI, connect=False)[MONGODB_NAME]


mongodb = get_mongo()
qiniu_client = QiNiu()



def get_lrc(song_id):
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:58.0) Gecko/20100101 Firefox/58.0'}
    url = 'http://music.163.com/api/song/media?id={}'.format(song_id)
    res = requests.get(url=url, headers=headers)
    if res.status_code not in (200, 201):
        data = {'lrc': '[00:20.010] 获取歌词失败!'}
    else:
        json_data = json.loads(res.text)
        data = {'lrc': json_data.get('lyric', "[00:20.010] 暂无歌词~")}
        if '纯音乐' in data['lrc'] or not data['lrc']:
            data['lrc'] = "[00:20.010] 暂无歌词~"
        data['lrc'] = data['lrc'].replace('\r\n', '\n')
        data['lrc'] = data['lrc'].replace('\n\n', '\n')
        data['lrc'] = data['lrc'].replace('\r', '\n')

        lrc = data['lrc'].split('\n')
        data['lrc'] = '\n'.join(l for l in lrc if all(l.split(']')))
    format_lrc = [d for d in data.get('lrc').split('\n') if d and not d.isspace()]
    # print(format_lrc)
    pretty_lrc = []
    try:
        pretty_lrc = [lrc[lrc.index(']') + 1:] for lrc in format_lrc if lrc and not lrc.isspace()]
    except:
        
        print(format_lrc)
    return format_lrc, pretty_lrc


def get_my_like(username, password):
    # username 手机号
    base_url = 'http://music.mongona.com/song/url?id={}'

    with requests.session() as se:
        login_res = se.get(url='http://music.mongona.com/login/cellphone?phone={}&password={}'.format(username, password))
        # 拿身份信息 id
        login_id = login_res.json().get('account').get('id')
        # 拿我的歌单
        my_list_res = se.get(url='http://music.mongona.com/user/playlist?uid={}'.format(login_id))
        # 我的喜欢列表
        my_like_list_id = my_list_res.json()['playlist'][0]['id']
        # my_like_detail
        my_like_songs_ids = \
        se.get(url='http://music.mongona.com/playlist/detail?id={}'.format(my_like_list_id)).json()['playlist'][
            'trackIds']
        my_like_songs_ids = [like['id'] for like in my_like_songs_ids]
        for i in my_like_songs_ids:
            temp = dict()
            song_detail = se.get(url='http://music.mongona.com/song/detail?ids={}'.format(i)).json()['songs'][0]
            temp['title'] = song_detail.get('name')
            temp['author'] = ','.join([n['name'] for n in song_detail.get('ar')])
            temp['images'] = song_detail.get('al').get('picUrl')
            temp['album'] = song_detail.get('al').get('name')
            temp['pub_date'] = time.strftime('%Y-%m-%d %H:%M:%S',
                                             time.localtime(song_detail.get('publishTime') // 1000))
            # 拿热评
            comment_data = se.get(url='http://music.mongona.com/comment/hot?id={}&type=0'.format(i)).json()
            temp['comments_count'] = comment_data['total']
            temp['comment_id'] = ''
            temp['comment_user_id'] = comment_data['hotComments'][0]['user']['userId']
            temp['comment_nickname'] = comment_data['hotComments'][0]['user']['nickname']
            temp['comment_avatar_url'] = comment_data['hotComments'][0]['user']['avatarUrl']
            temp['comment_liked_count'] = ','.join(
                [str(f) for f in str(comment_data['hotComments'][0]['likedCount']).split('\n')])
            temp['comment_content'] = comment_data['hotComments'][0]['content']
            temp['comment_pub_date'] = time.strftime('%Y-%m-%d %H:%M:%S',
                                                     time.localtime(comment_data['hotComments'][0]['time'] // 1000))
            temp['format_lrc'], temp['pretty_lrc'] = get_lrc(song_id=i)
            song_info = se.get(url=base_url.format(i))
            if song_info.status_code not in (200, 201):
                return
            data = song_info.json().get('data')[0]
            music_url = data.get('url')
            if not music_url:
                print(data)
                continue
            music_type = data.get('type')
            res = se.get(music_url)
            m = hashlib.md5()
            m.update(res.content)
            uid = m.hexdigest()
            if mongodb['music'].find_one({'uid': uid}):
                continue
            r, info = qiniu_client.up_stream(res.content, 'music/' + uid + '.' + music_type)
            temp['song_id'] = i
            temp.update({'mp3_url': MEDIA_URL + r.get('key')})
            temp['id'] = redis_client.incrby('music_id', 1)
            temp['uid'] = uid
            # new_data.append(temp)
            print(temp)
            mongodb['music'].insert_one(temp)


get_my_like(username, password)

通过接口,拿网易云我的喜欢 所有音乐

links: https://www.mongona.com/?c=5

这上边的所有音乐都是我喜欢列表里的 .居然看到了我自己的评论!!

6
0
Tags
富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善。
打赏二维码