Пример #1
0
        private void LoginToKoobeca(string user, string password)
        {
            if (string.IsNullOrEmpty(user))
            {
                throw new Exception("Koobeca user is missing");
            }
            if (string.IsNullOrEmpty(password))
            {
                throw new Exception("Koobeca password is missing");
            }

            Logger.Instance.Info("Login in to koobeca");
            //login
            _KoobecaClient  = new KoobecaClient();
            _KoobecaService = new KoobecaService(_KoobecaClient);
            var getAccountTask = _KoobecaService.GetAccountAsync(user, password);

            Task.WaitAll(getAccountTask);
            var account = getAccountTask.Result;

            if (string.IsNullOrEmpty(account.oauth_token))
            {
                throw new Exception("Failed to login");
            }
        }
Пример #2
0
        public void DoPosting(string user, string password)
        {
            //login
            var koobecaClient  = new KoobecaClient();
            var koobecaService = new KoobecaService(koobecaClient);
            var getAccountTask = koobecaService.GetAccountAsync(user, password);

            Task.WaitAll(getAccountTask);
            var account = getAccountTask.Result;

            if (string.IsNullOrEmpty(account.oauth_token))
            {
                Logger.Instance.Error("Failed to login to koobeca");
                return;
            }

            //load the cache
            PageSources.GetByType("fb_page").Select(p => _PageSourceCache[p.source_id] = p).ToArray();
            Logger.Instance.Debug($"page source cache loaded with {_PageSourceCache.Count} items.");

            //take from the queue
            while (IsActive)
            {
                if (_PostQueue.Count > 0)
                {
                    var item = _PostQueue.Dequeue();

                    if (item.Type == FBPostType.Picture)
                    {
                        try
                        {
                            //make the picture include the bigger text
                            StringBuilder bigMsg = new StringBuilder();
                            bigMsg.AppendLine(item.message);
                            bigMsg.AppendLine("------------------------");
                            bigMsg.AppendLine(item.name);
                            bigMsg.AppendLine(item.description);
                            item.message = bigMsg.ToString();

                            //get all puctures
                            List <string> picUrls = new List <string>();
                            picUrls.AddRange(item.attachments.data.Where(a => a.type == "photo").Select(a => a.media.image.src).ToList());
                            picUrls.AddRange(item.attachments.data.Where(a => a.type == "album").SelectMany(a => a.subattachments.data.Where(sa => sa.type == "photo").Select(sa => sa.media.image.src)).ToList());
                            var webClient = new WebClient();

                            item.Photos = new Stream[picUrls.Count];
                            for (int i = 0; i < picUrls.Count; i++)
                            {
                                var    url        = picUrls[i];
                                byte[] imageBytes = webClient.DownloadData(url);
                                item.Photos[i] = new MemoryStream(imageBytes);
                            }
                        }
                        catch (Exception e)
                        {
                            Logger.Instance.Error($"failed to get photo stream from {item.full_picture} , {e.Message}");
                        }
                    }
                    Logger.Instance.Info($"Got post from FB source:{item.source} of type {item.Type} , looking for target in Koobeca");
                    double timeSpent;
                    PublishPost(_KoobecaService, item, out timeSpent);
                    Published?.Invoke(item.source, timeSpent / 1000); // throw event to listeners
                }
                else
                {
                    Thread.Sleep(1000);
                }
            }

            Logger.Instance.Info($"Publishing ended.");
        }