Example #1
0
        public ActionResult Index()
        {
            using (var db = new ArticleDBContext())
            {
                var articles = from article in db.Articles
                               orderby article.Title
                               select article;

                return(View(articles.OrderByDescending(art => art.Date).ToList()));
            }
            return(View());
        }
Example #2
0
        public static Article[] GetRelatedArticles(ArticleDBContext db, int aid, int n)
        {
            var q = from order in db.Order
                    where order.OrderDetails.Any(od => od.Article.ArticleId == aid)
                    select order.OrderDetails;

            var q3 = q.ToArray();

            Dictionary <int, int> dic = new Dictionary <int, int>();

            foreach (var odl in q3)
            {
                foreach (var od in odl)
                {
                    int artId = od.Article.ArticleId;

                    if (artId == aid)
                    {
                        continue;
                    }

                    if (!dic.ContainsKey(artId))
                    {
                        dic[artId] = 0;
                    }

                    dic[artId] = dic[artId] + 1;
                }
            }



            var q2 = from e in dic
                     orderby e.Value descending
                     select e.Key;

            q2 = q2.ToArray();

            List <Article> arts = new List <Article>();

            foreach (var id in q2)
            {
                arts.Add(db.Article.Where(a => a.ArticleId == id).Single());
            }

            return(arts.ToArray());
        }
Example #3
0
        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ArticleDBContext dBContext)
        {
            #region 站台檢核

            app.UseHealthChecks("/chk", new HealthCheckOptions
            {
                ResponseWriter = async(context, report) =>
                {
                    var result = JsonConvert.SerializeObject(
                        new
                    {
                        status      = report.Status.ToString(),
                        environment = env.EnvironmentName,
                        name        = "Article"
                    });
                    context.Response.ContentType = MediaTypeNames.Application.Json;
                    await context.Response.WriteAsync(result);
                }
            });
            #endregion
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });

            dBContext.Database.EnsureCreated();
        }
Example #4
0
        public JsonResult GetTrends(string date_from, string date_to,  string word)
        {
            string[] cols = word.Split(',');

            DateTime start = Convert.ToDateTime(date_from);
            DateTime end = Convert.ToDateTime(date_to);
            ArticleDBContext context = new ArticleDBContext();

            var words = from wf in context.WordFrequenies
                        join w in context.Words on wf.WordId equals w.WordId
                        join a in context.Articles on wf.ArticleId equals a.ArticleId
                        where   (a.DateRetrieved <= end) &&
                                (a.DateRetrieved >= start) &&
                                (w.Text.ToLower() == word.ToLower())
                        select new {a.DateRetrieved, w.Text, wf.Frequency };

            var res = from wf in words
                      group wf by new { wf.DateRetrieved, wf.Text }  into g
                      select new
                      {
                          Date = g.FirstOrDefault().DateRetrieved,
                          Frequency = g.Sum(x => x.Frequency)
                      };

            var ret = new object[res.Count() + 1];

            ret[0] = new[] { "Data", word };

            int cnt = 1;
                foreach (var record in res)
                {
                    ret[cnt] = new object[] { record.Date.ToShortDateString(), record.Frequency };
                    cnt++;
                }
            return new JsonResult()
            {
                Data = ret,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
Example #5
0
 public ArticleRepository(ArticleDBContext _db)
 {
     db = _db;
 }
Example #6
0
        public ActionResult AddArticle( string url)
        {
            if (String.IsNullOrEmpty(url))
            {
                ViewBag.Message = "Ju lutem shtoeni adresen a artikullit!";
                return View();
            }

            //e lexojme permbajtjen e faqes se dhene
            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            Stream stream = response.GetResponseStream();
            StreamReader reader = new StreamReader(stream);
            string permbajtja = reader.ReadToEnd();
            reader.Close();
            response.Close();

            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(permbajtja);
              string paHtml = "";

              ArticleDBContext context = new ArticleDBContext();

              Article a = new Article();

            //Te shekulli permbajtja eshte brenda klases tekst

                if (url.ToLower().Contains("shekulli"))
                {
                    paHtml = doc.DocumentNode.SelectNodes("//div[@class='tekst']").First().InnerText;

                    a.Author  = doc.DocumentNode.SelectNodes("//ul[@class='li-info1lajmi']").Descendants()
                                .Where(x => x.Name == "li" && x.ChildNodes.FirstOrDefault().Name == "a").FirstOrDefault().InnerText;

                    string data = doc.DocumentNode.SelectNodes("//ul[@class='li-info1lajmi']").Descendants()
                                .Where(x => x.Name == "li").ToList()[1].InnerText;

                    int dita, muaji, viti;
                    dita = Convert.ToInt32(data.Split(',')[0].Split(' ')[0]);
                    Helper.Helper h = new Helper.Helper();

                    muaji = h.Muajt[data.Split(',')[0].Split(' ')[1]];
                    viti = Convert.ToInt32(data.Split(',')[0].Split(' ')[2]);
                    a.DateRetrieved = new DateTime(viti, muaji, dita);
                    a.MediaName = "Shekulli";
                }
                else if (url.ToLower().Contains("gazeta-shqip"))
                {
                    paHtml = doc.DocumentNode.SelectNodes("//div[@class='td-ss-main-content']").First().InnerText;
                    a.Author = doc.DocumentNode.SelectNodes("//div[@class='td-post-author-name']")[0].InnerText.Replace("Nga", "").Replace("-", "");
                    a.MediaName = "Gazeta Shqip";

                    string data = doc.DocumentNode.SelectNodes("//div[@class='td-post-date']")[0].InnerText;

                    int dita, muaji, viti;
                    dita = Convert.ToInt32(data.Split(' ')[0]);
                    Helper.Helper h = new Helper.Helper();

                    muaji = h.Muajt[data.Split(' ')[1]];
                    viti = Convert.ToInt32(data.Split(' ')[2]);
                    a.DateRetrieved = new DateTime(viti, muaji, dita);

                }
                else if (url.ToLower().Contains("mapo"))
                {
                  paHtml = doc.GetElementbyId("content").InnerText;
                  a.Author = "Mapo";
                  a.MediaName = "Mapo";
                }
                else //if (url.ToLower().Contains("sot.com.al")) //kjo pjese te c'komentohet nese shtohen mediume tjera
                {
                  paHtml = doc.GetElementbyId("block-system-main").InnerText;
                  a.MediaName = "Gazeta Sot";
                  //Sunday, July 26, 2015
                  string data = doc.DocumentNode.SelectNodes("//span[@class='date-display-single']")[0].InnerText.Split('-')[0];

                  a.Author = "Gazeta Sot";

                  int dita, muaji, viti;
                  dita = Convert.ToInt32(data.Split(',')[1].Split(' ')[2]);
                  Helper.Helper h = new Helper.Helper();

                  muaji = h.Muajt[data.Split(',')[1].Split(' ')[1]];
                  viti = Convert.ToInt32(data.Split(',')[2]);
                  a.DateRetrieved = new DateTime(viti, muaji, dita);

                }

            a.Url = url;
            context.Articles.Add(a);

            context.SaveChanges();

            //Pastrojme hapesirat e shprazeta
            var regex = new Regex(
               "(\\<script(.+?)\\</script\\>)|(\\<style(.+?)\\</style\\>)",
               RegexOptions.Singleline | RegexOptions.IgnoreCase
            );

            paHtml = regex.Replace(paHtml, "");
            paHtml = paHtml.Replace('\n', ' ');
            paHtml = paHtml.Replace('\t', ' ');

            //nxjerim fjalite. Supozojme se fjalite ndahen me !, ., ;, ?
            char[] delim_fjalite = {'.', '!', '?', ';' };
            string[] fjalite = paHtml.Split(delim_fjalite);
            char[] delim_fjalet = { '“', '”', ' ', '.', '!', '?', ';', '"', '}', '{', '(', ')', '-', '=', '+', '*', '"', ';', ':', '!', '?','<', '>', ',' };

            foreach (string fjali in fjalite)
            {
                Phrase f = new Phrase();
                string[] words = fjali.Split(delim_fjalet);
                f.Text = String.Join(" ", words.Where(x => x.Length > 3));

                f.ArticleId = a.ArticleId;

                foreach (char c in delim_fjalet)
                {
                    f.Text = f.Text.Replace(c, ' ');
                }

                context.Phrases.Add(f);
            }

            //nxjerim fjalet. Supozojme se fjalet ndahen si fjalite, por edhe me hapesire te shprazet dhe presje

            string[] fjalet = paHtml.Split(delim_fjalet);
            foreach (string fjale in fjalet.Where(x=>x.Length > 3))
            {
                //nese fjala nuk ekziston ne databaze
                if (context.Words.Where(x => x.Text.ToLower() == fjale.ToLower()).Count() == 0)
                {
                    Word w = new Word();
                    w.Text = fjale.ToLower();

                    context.Words.Add(w);
                    context.SaveChanges();

                    WordFrequency wf = new WordFrequency();
                    wf.ArticleId = a.ArticleId;
                    wf.WordId = w.WordId;
                    wf.Frequency = 1;
                    context.WordFrequenies.Add(wf);
                    context.SaveChanges();
                }
                else
                {
                    Word w = context.Words.Where(x => x.Text.ToLower() == fjale.ToLower()).First();
                    WordFrequency wf = context.WordFrequenies.Where(x => x.WordId == w.WordId && x.ArticleId == a.ArticleId).FirstOrDefault();

                    if (wf != null)
                    {
                        wf.Frequency++;
                        context.SaveChanges();
                    }
                    else
                    {
                       WordFrequency wfNew = new WordFrequency();
                       wfNew.ArticleId = a.ArticleId;
                       wfNew.WordId = w.WordId;
                       wfNew.Frequency = 1;
                       context.WordFrequenies.Add(wfNew);
                       context.SaveChanges();
                    }
                }
            }

            //regjistrojme ne db

            return View("DocumentUploader");
        }
Example #7
0
        public JsonResult GetCloudData(string media_name, string date_from, string date_to, string author_name, int word)
        {
            List<string> ret = new List<string>();
            DateTime start = Convert.ToDateTime(date_from);
            DateTime end = Convert.ToDateTime(date_to);
            ArticleDBContext context = new ArticleDBContext();
            if (word == 1)
            {
                var words = from wf in context.WordFrequenies
                            join w in context.Words on wf.WordId equals w.WordId
                            join a in context.Articles on wf.ArticleId equals a.ArticleId
                            where (a.MediaName == media_name) &&
                                    (a.DateRetrieved <= end) &&
                                    (a.DateRetrieved >= start) &&
                                    w.Text.Length > 3 &&
                                    (author_name == "0" || a.Author == author_name)
                            select new { w.Text, wf.Frequency };

                var res = from wf in words
                          group wf by wf.Text into g
                          select new
                          {
                              Word = g.FirstOrDefault().Text,
                              Frequency = g.Sum(x => x.Frequency)
                          };

                foreach (var record in res)
                {
                    for (int i = 0; i < record.Frequency; i++)
                    {
                        ret.Add(record.Word.ToLower());
                    }
                }

            }
            else //ne kete rast marim fjalet ndryshe. Do ti mar prej frazave duke injoruar fjalet e shkurtra
            {
                var phrases  = from phrase in context.Phrases
                               join art in context.Articles on phrase.ArticleId equals art.ArticleId
                               where (art.MediaName == media_name) &&
                                    (art.DateRetrieved <= end) &&
                                     (art.DateRetrieved >= start) &&
                                     (author_name == "0" || art.Author == author_name)
                                     select new {phrase.Text};

                char[] delim = {',',' '};
                foreach (var p in phrases)
                {
                    string[] words = p.Text.Split(delim);
                    string ngram;

                    if (words.Where(x => x.Trim().Length > 3).Count() <= word)
                    {
                        ngram = string.Join(" ", words.Where(x => x.Length > 3));
                        ret.Add(ngram);
                    }
                    else
                    {
                        //vetem fjalet me te medha se 3 shkronja
                        for (int i = 0; i < words.Where(x => x.Trim().Length > 3).Count(); i++)
                        {
                            try
                            {
                                if (word == 2)
                                {
                                    ngram = words[i] + " " + words[i + 1];
                                    ret.Add(ngram);
                                }
                                else
                                {

                                        ngram = words[i] + " " + words[i + 1] + " " + words[i + 2];
                                        ret.Add(ngram);

                                }
                            }
                            catch (Exception ex)
                            {
                            }

                        }
                    }

                }

            }

            return new JsonResult()
            {
                Data = ret,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
Example #8
0
        public JsonResult GetAuthors(string media_name)
        {
            ArticleDBContext context = new ArticleDBContext();
            var authors = from a in context.Articles
                          where a.MediaName == media_name
                          select new { a.Author };
            List<string> ret = new List<string>();
            foreach (var record in authors)
            {
                if (!ret.Contains(record.Author) && !String.IsNullOrEmpty(record.Author.Trim()))
                    ret.Add(record.Author);
            }

            return new JsonResult()
            {
                Data = ret,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }
Example #9
0
 public UnitOfWork(ArticleDBContext dbContext)
 {
     _dbContext = dbContext;
 }
Example #10
0
        private void createAdminUserAndApplicationRoles()
        {
            ApplicationDbContext context = new ApplicationDbContext();
            var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
            var UserManager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(context));

            // Se adauga rolurile aplicatiei
            if (!roleManager.RoleExists("Administrator"))
            {
                // Se adauga rolul de administrator
                var role = new IdentityRole();
                role.Name = "Administrator";
                roleManager.Create(role);
                // se adauga utilizatorul administrator
                var user = new ApplicationUser();
                user.UserName = "******";
                user.Email    = "*****@*****.**";
                var adminCreated = UserManager.Create(user, "P@rola1");
                if (adminCreated.Succeeded)
                {
                    UserManager.AddToRole(user.Id, "Administrator");
                }

                ArticleDBContext db = new ArticleDBContext();

                //adding default categories
                Category cat        = new Category();
                var      categories = from category in db.Categories
                                      orderby category.Name
                                      select category;

                string[] catString = { "World",         "Business", "Technology",
                                       "Entertainment", "Sports",   "Science",
                                       "Health" };
                foreach (var name in catString)
                {
                    var found = (from x in categories.OfType <Category>() where x.Name == name select x).FirstOrDefault();
                    if (found == null)
                    {
                        cat.Name = name;
                        db.Categories.Add(cat);
                        db.SaveChanges();
                    }
                }

                //adding some random articles
                Article art      = new Article();
                var     articles = from article in db.Articles
                                   orderby article.Title
                                   select article;
                var admin = UserManager.FindByName("*****@*****.**");

                //first
                art.Title     = "Out with a Bang! New Year's Eve fireworks from around the world";
                art.Date      = DateTime.Now;
                art.Author    = user.Id;
                art.Username  = user.UserName;
                art.Thumbnail = "https://cdn.images.express.co.uk/img/dynamic/1/590x/New-Years-Eve-fireworks-1065509.jpg?r=1546273653627";
                art.Headline  = "Amazing!!";
                art.URL       = "https://cdn.images.express.co.uk/img/dynamic/1/590x/New-Years-Eve-fireworks-1065509.jpg?r=1546273653627";

                db.Articles.Add(art);
                db.SaveChanges();

                //second
                art           = new Article();
                art.Title     = "North Korea's Kim Warns Trump Talks at Risk Over Sanctions";
                art.Date      = DateTime.Now;
                art.Author    = user.Id;
                art.Username  = user.UserName;
                art.Thumbnail = "https://assets.bwbx.io/images/users/iqjWHBFdfxIU/iVQmYqsv.yOQ/v0/-1x-1.jpg";
                art.Headline  = "Amazing!!";
                art.Content   = "Kim Jong Un used his New Year’s address to issue a pointed warning to President Donald Trump, saying North Korea would take a “new path” in nuclear talks if the U.S. didn’t relax economic sanctions. While Kim affirmed his willingness to meet Trump again, his nationally televised speech offered no new initiatives to advance talks that have sputtered since their first summit in June. Instead, Kim said his patience with the U.S.-led sanctions regime designed as punishment for his nuclear weapons program was running out.";

                db.Articles.Add(art);
                db.SaveChanges();

                //third
                art           = new Article();
                art.Title     = "Google is primed to go big at CES again";
                art.Date      = DateTime.Now;
                art.Author    = user.Id;
                art.Username  = user.UserName;
                art.Thumbnail = "https://lh3.googleusercontent.com/proxy/ogwfaF0iwa05OnTNQFyD0rZ384sAN74p5xwJE6qfJmrEFcmgxlXo4zg22lrlaLcaS_hp9pFCu8s8QZ-GgDy37DxWVOHpq2B4IV35vb4wgHBWfJiYqI_AVARVMaguPane4Raedg=w530-h212-p";
                art.Headline  = "Last year was the search giant's first official appearance in years at the massive tech show. With round two, Google looks to make an even bigger splash.";
                art.Content   = "Last year at CES, Google set up a three-story Wonka factory of smart home devices. In a massive booth near the Las Vegas Convention Center, the search giant showed off how its Google Assistant could work with everything from washing machines to miniature train sets. There was a giant, voice-controlled gumball machine full of giveaway devices. A big, blue slide spiraled to the ground. Elsewhere at the conference, white-suited Google workers greeted people in booths across the show floor, and the company plastered the words Hey Google -- one of the trigger phrases for the Google Assistant -- over the Las Vegas Monorail. The message was clear: After years of laying low at the world's largest tech show, Google had finally arrived at CES. The company is primed to go even bigger at this year's show, which starts next week. Before last year's CES, the company had happily stayed on the sidelines and let its manufacturing partners, including Samsung and LG, grab all the attention. Now Google is using the trade show to trumpet its software and hardware, complete with an official stage. That's because CES has become an important staging ground for the tech giant as it pushes devices to consumers that compete against the likes of Amazon, Apple and Samsung. In the next three years, Google's hardware division -- which includes its Google Home smart speakers, Nest thermostats and Chromecast streaming devices -- could hit $20 billion in revenue, RBC Capital Markets said last month.";

                db.Articles.Add(art);
                db.SaveChanges();

                //forth
                art           = new Article();
                art.Title     = "Queen rocker Brian May releases space song dedicated to Ultima Thule";
                art.Date      = DateTime.Now;
                art.Author    = user.Id;
                art.Username  = user.UserName;
                art.Thumbnail = "https://i.amz.mshcdn.com/X5uLdPupVxHAqh1DMT8lE5faKwQ=/950x534/filters:quality(90)/https%3A%2F%2Fblueprint-api-production.s3.amazonaws.com%2Fuploads%2Fcard%2Fimage%2F910620%2F3dd52838-69ad-4531-b6fd-6e15e729b92d.jpg";
                art.Headline  = "Amazing!!";
                art.URL       = "https://mashable.com/article/ultima-thule-brian-may-rock-song-new-horizons/?europe=true#ziCYBFKhvqqo";

                db.Articles.Add(art);
                db.SaveChanges();
            }
            if (!roleManager.RoleExists("Editor"))
            {
                var role = new IdentityRole();
                role.Name = "Editor";
                roleManager.Create(role);
            }
            if (!roleManager.RoleExists("User"))
            {
                var role = new IdentityRole();
                role.Name = "User";
                roleManager.Create(role);
            }
        }