Advertisement
Guest User

noir 2

a guest
Sep 23rd, 2017
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.46 KB | None | 0 0
  1. using DataModel;
  2. using LinqToDB;
  3. using LinqToDB.Data;
  4. using MySql.Data.MySqlClient;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Net.Http;
  10. using System.ServiceModel.Syndication;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14. using System.Xml;
  15. using ultimatebot.core.classes;
  16. using ultimatebot.Properties;
  17. using static ultimatebot.core.classes.systemcore;
  18.  
  19. namespace ultimatebot.reddit.classes.poster
  20. {
  21.     class posting
  22.     {
  23.         public static event EventHandler<string> StatusTextChanged;
  24.  
  25.         public List<article> All(string PostUrl)
  26.         {
  27.             // Get the settings for our database
  28.             DataConnection.DefaultSettings = new MySettings();
  29.  
  30.             // Make sure we've not submitted this before
  31.             using (var db = new articlesDB())
  32.             {
  33.                 var query = from a in db.articles
  34.                             where a.url == PostUrl
  35.                             select a;
  36.                 return query.ToList();
  37.             }
  38.         }
  39.  
  40.         // get voting designer and userclass
  41.         votingcore voting = new votingcore();
  42.         user user = new user();
  43.  
  44.         /// <summary>
  45.         /// Main method for creating Reddit Posts
  46.         /// </summary>
  47.         /// <returns></returns>
  48.         public async Task RssPoster()
  49.         {
  50.             try
  51.             {
  52.                 while (true) // continous
  53.                 {
  54.                     XmlReader reader = XmlReader.Create(redditcore.RedditRssFeed);
  55.                     SyndicationFeed feed = SyndicationFeed.Load(reader);
  56.                     reader.Close();
  57.  
  58.                     // loop through text file to get the account information
  59.                     var lines = File.ReadAllLines(redditcore.AccountsFile);
  60.                     var pairs = feed.Items.Select((item, i) => new { item, line = lines[i % lines.Length] });
  61.  
  62.                     Parallel.ForEach(pairs, new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(systemcore.Threads) }, (pair) =>
  63.                     {
  64.                         string[] temp = pair.line.Split('|');
  65.                         redditcore.username = temp[0];
  66.                         redditcore.password = temp[1];
  67.  
  68.                         string PostUrl = pair.item.Links[0].Uri.ToString();
  69.                         string PostTitle = pair.item.Title.Text;
  70.                         string Location = null;
  71.  
  72.                         // get the reidrected url in case it's a short url  
  73.                         using (HttpClient client = new HttpClient())
  74.                         {
  75.                             using (HttpResponseMessage response = client.GetAsync(PostUrl).Result)
  76.                             {
  77.                                 response.EnsureSuccessStatusCode();
  78.                                 Location = response.RequestMessage.RequestUri.ToString();
  79.                             }
  80.                         }
  81.  
  82.                         PostArticle(PostTitle, Location).Wait();
  83.                     });
  84.                 }
  85.             }
  86.             catch (Exception ex)
  87.             {
  88.                 // Exception
  89.                 StatusTextChanged?.Invoke(this, ex.Message);
  90.             }
  91.         }
  92.  
  93.         public async Task PostArticle(string PostTitle, string PostUrl)
  94.         {
  95.             // make sure it's not already been posted
  96.             bool isEmpty = !All(PostUrl).Any();
  97.             if (isEmpty)
  98.             {
  99.  
  100.                 // log the user in, run through accounts, complete actions then use next account
  101.                 user.login().Wait();
  102.  
  103.                 // Access created post
  104.                 string CreatedPost = null;
  105.  
  106.                 // we've logged in time to post
  107.                 Post(PostTitle, CreatedPost, PostUrl).Wait();
  108.  
  109.                 // add to datagrid
  110.                 // addtoque().Wait();
  111.  
  112.                 //add to database
  113.                 AddtoDb(PostUrl).Wait() ;
  114.  
  115.                 // if we have enabled voting the newly created post
  116.                 if (redditcore.UpvoteCreated == true)
  117.                 {
  118.                     votingcore.StatusTextChanged += (sender, text) =>
  119.                     {
  120.                         StatusTextChanged?.Invoke(this, text);
  121.                     };
  122.  
  123.                     //await voting.main("");
  124.                 }
  125.             }
  126.         }
  127.  
  128.         /// <summary>
  129.         /// Post a Reddit Article
  130.         /// </summary>
  131.         /// <returns></returns>
  132.         private async Task Post(string PostTitle, String CreatedPost, string PostUrl)
  133.         {
  134.             // Post data for our Reddit post
  135.             var postdata = new Dictionary<string, string>
  136.             {
  137.                 { "uh", user.uh },
  138.                 { "kind", "link" },
  139.                 { "url", PostUrl },
  140.                 { "title", PostTitle },
  141.                 { "sr", redditcore.SubReddit },
  142.                 { "selected_sr_names", "" },
  143.                 { "sendreplies", "true" },
  144.                 { "id", "#newlink" },
  145.                 { "r", "debugtestingpurpose" },
  146.                 { "renderstyle", "html" },
  147.             };
  148.             var content = new FormUrlEncodedContent(postdata);
  149.  
  150.             // get the reidrected url for the created post
  151.             using (HttpClient client = new HttpClient())
  152.             {
  153.                 using (HttpResponseMessage response = systemcore.client.PostAsync("https://www.reddit.com/api/submit", content).Result)
  154.                 {
  155.                     response.EnsureSuccessStatusCode();
  156.                     CreatedPost = response.RequestMessage.RequestUri.ToString();
  157.                 }
  158.             }
  159.  
  160.             // Posted
  161.             StatusTextChanged?.Invoke(this, $"Posted: " + PostTitle);
  162.         }
  163.  
  164.         /// <summary>
  165.         /// Add the article link to the Database, so we don't post it again
  166.         /// </summary>
  167.         /// <returns></returns>
  168.         private async Task AddtoDb(string PostUrl)
  169.         {
  170.             using (var db = new articlesDB())
  171.             {
  172.                 db.articles
  173.                   .Value(a => a.url, PostUrl)
  174.                   .Insert();
  175.             }
  176.         }
  177.  
  178.         private async Task AddToQue()
  179.         {
  180.             try
  181.             {
  182.                 //posted.Rows.Add(PostTitle, SubReddit, username);
  183.             }
  184.             catch (Exception ex)
  185.             {
  186.                 StatusTextChanged?.Invoke(this, ex.Message);
  187.             }
  188.         }
  189.  
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement