Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using DataModel;
- using LinqToDB;
- using LinqToDB.Data;
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Net.Http;
- using System.ServiceModel.Syndication;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.Xml;
- using ultimatebot.core.classes;
- using ultimatebot.Properties;
- using static ultimatebot.core.classes.systemcore;
- namespace ultimatebot.reddit.classes.poster
- {
- class posting
- {
- public static event EventHandler<string> StatusTextChanged;
- public List<article> All(string PostUrl)
- {
- // Get the settings for our database
- DataConnection.DefaultSettings = new MySettings();
- // Make sure we've not submitted this before
- using (var db = new articlesDB())
- {
- var query = from a in db.articles
- where a.url == PostUrl
- select a;
- return query.ToList();
- }
- }
- // get voting designer and userclass
- votingcore voting = new votingcore();
- user user = new user();
- /// <summary>
- /// Main method for creating Reddit Posts
- /// </summary>
- /// <returns></returns>
- public async Task RssPoster()
- {
- try
- {
- while (true) // continous
- {
- XmlReader reader = XmlReader.Create(redditcore.RedditRssFeed);
- SyndicationFeed feed = SyndicationFeed.Load(reader);
- reader.Close();
- // loop through text file to get the account information
- var lines = File.ReadAllLines(redditcore.AccountsFile);
- var pairs = feed.Items.Select((item, i) => new { item, line = lines[i % lines.Length] });
- Parallel.ForEach(pairs, new ParallelOptions { MaxDegreeOfParallelism = Convert.ToInt32(systemcore.Threads) }, (pair) =>
- {
- string[] temp = pair.line.Split('|');
- redditcore.username = temp[0];
- redditcore.password = temp[1];
- string PostUrl = pair.item.Links[0].Uri.ToString();
- string PostTitle = pair.item.Title.Text;
- string Location = null;
- // get the reidrected url in case it's a short url
- using (HttpClient client = new HttpClient())
- {
- using (HttpResponseMessage response = client.GetAsync(PostUrl).Result)
- {
- response.EnsureSuccessStatusCode();
- Location = response.RequestMessage.RequestUri.ToString();
- }
- }
- PostArticle(PostTitle, Location).Wait();
- });
- }
- }
- catch (Exception ex)
- {
- // Exception
- StatusTextChanged?.Invoke(this, ex.Message);
- }
- }
- public async Task PostArticle(string PostTitle, string PostUrl)
- {
- // make sure it's not already been posted
- bool isEmpty = !All(PostUrl).Any();
- if (isEmpty)
- {
- // log the user in, run through accounts, complete actions then use next account
- user.login().Wait();
- // Access created post
- string CreatedPost = null;
- // we've logged in time to post
- Post(PostTitle, CreatedPost, PostUrl).Wait();
- // add to datagrid
- // addtoque().Wait();
- //add to database
- AddtoDb(PostUrl).Wait() ;
- // if we have enabled voting the newly created post
- if (redditcore.UpvoteCreated == true)
- {
- votingcore.StatusTextChanged += (sender, text) =>
- {
- StatusTextChanged?.Invoke(this, text);
- };
- //await voting.main("");
- }
- }
- }
- /// <summary>
- /// Post a Reddit Article
- /// </summary>
- /// <returns></returns>
- private async Task Post(string PostTitle, String CreatedPost, string PostUrl)
- {
- // Post data for our Reddit post
- var postdata = new Dictionary<string, string>
- {
- { "uh", user.uh },
- { "kind", "link" },
- { "url", PostUrl },
- { "title", PostTitle },
- { "sr", redditcore.SubReddit },
- { "selected_sr_names", "" },
- { "sendreplies", "true" },
- { "id", "#newlink" },
- { "r", "debugtestingpurpose" },
- { "renderstyle", "html" },
- };
- var content = new FormUrlEncodedContent(postdata);
- // get the reidrected url for the created post
- using (HttpClient client = new HttpClient())
- {
- using (HttpResponseMessage response = systemcore.client.PostAsync("https://www.reddit.com/api/submit", content).Result)
- {
- response.EnsureSuccessStatusCode();
- CreatedPost = response.RequestMessage.RequestUri.ToString();
- }
- }
- // Posted
- StatusTextChanged?.Invoke(this, $"Posted: " + PostTitle);
- }
- /// <summary>
- /// Add the article link to the Database, so we don't post it again
- /// </summary>
- /// <returns></returns>
- private async Task AddtoDb(string PostUrl)
- {
- using (var db = new articlesDB())
- {
- db.articles
- .Value(a => a.url, PostUrl)
- .Insert();
- }
- }
- private async Task AddToQue()
- {
- try
- {
- //posted.Rows.Add(PostTitle, SubReddit, username);
- }
- catch (Exception ex)
- {
- StatusTextChanged?.Invoke(this, ex.Message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement