public void DoFetching(int checkingIntervalInMinutes = 1)
        {
            //load sourcelist
            //PageSources.Add(390, "1154233056", "Tal Gilad", "fb_user");

            while (IsActive)
            {
                //go through sources
                foreach (var source in PageSources.GetByType("fb_page").Select(s => s.source_id).ToList())
                {
                    FetchPosts(source, "fb_page");
                }
                foreach (var source in PageSources.GetByType("fb_user").Select(s => s.source_id).ToList())
                {
                    FetchPosts(source, "fb_user");
                }

                //update last fetch time
                _LastFetchTime = DateTime.Now;
                if (!IsActive)
                {
                    return;
                }
                //go to sleep
                int sleepSeconds = checkingIntervalInMinutes * 60;
                while (sleepSeconds-- > 0 && IsActive)
                {
                    Thread.Sleep(1000);
                }
            }

            Logger.Instance.Info("Fetching process ended !");
        }
Beispiel #2
0
 private void AddPublicFigures()
 {
     PageSources.GetByType("fb_user").Select(s => AddSource(SourcesMap.Instance.GetSource(s.page_id, "user"))).ToArray();
 }
Beispiel #3
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.");
        }