Пример #1
0
        public static async Task <IList <BilibiliMessage> > FetchLatestComments(int count)
        {
            var fi = new FileInfo(ChatLogDatabasePath);

            fi.Directory.Create();
            fi = new FileInfo(UserDatabasePath);
            fi.Directory.Create();

            using (var db = new LiteDatabase(ChatLogDatabasePath))
            {
                var chats = db.GetCollection <BilibiliMessage>();
                var query = Query.And(
                    Query.All("ReceiveTime", Query.Descending),
                    Query.Or(
                        Query.EQ("MsgType", "Danmaku"),
                        Query.EQ("MsgType", "Gift")
                        )
                    );
                var results = chats.Find(query, 0, count);

                foreach (var i in results)
                {
                    var user = PickUserInformation(i.SenderId);
                    if (user != null)
                    {
                        var facedata = await HttpRequests.DownloadBytes(user.Face);

                        i.AvatarBase64 = ImageHelper.ConvertToJpegBase64(facedata);
                    }
                }

                return(results.Reverse().ToList());
            }
        }
Пример #2
0
        private async Task <User> CacheUser(string uid, Action <User> Callback = null)
        {
            // Pick user if exists and skip caching.
            var inDB = Database.PickUserInformation(Convert.ToInt32(uid));

            if (inDB != null && inDB.LastUpdateTime.Subtract(DateTime.UtcNow).TotalDays < 1)
            {
                return(inDB);
            }

            //Call api to get user information.
            var headers = new Dictionary <string, string> {
                { "User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36" },
                { "Referer", "http://m.bilibili.com" },
                { "Origin", "http://m.bilibili.com" },
            };

            // Post and get data from API.
            var result = await HttpRequests.DownloadString(
                url : "https://api.bilibili.com/x/space/acc/info?mid=" + uid,
                headers : headers,
                encoding : Encoding.UTF8
                );

            var json   = JToken.Parse(result);
            var face64 = "";

            // Download avatar.
            if (json["data"]?["face"] != null)
            {
                var facedata = await HttpRequests.DownloadBytes(json["data"]["face"].ToString());

                face64 = ImageHelper.ConvertToJpegBase64(facedata);
            }

            // Save the data.
            var user = new User
            {
                BirthDay   = json["data"]["birthday"]?.ToString() ?? "保密",
                Face       = json["data"]["face"]?.ToString() ?? "",
                FaceBase64 = face64 ?? "",
                Level      = json["data"]["level_info"]?["current_level"]?.ToObject <int>() ?? -1,
                Id         = json["data"]["mid"]?.ToObject <int>() ?? 0,
                Name       = json["data"]["name"]?.ToString() ?? "",
                Sex        = json["data"]["sex"]?.ToString() ?? "保密"
            };

            if (user.Id == 0)
            {
                return(null);
            }
            Database.SaveUserInformation(user);
            Callback(user);
            return(user);
        }