Last active
March 16, 2020 11:45
-
-
Save amitkhare/54f3ca677ecebc78ab80cfb643fa9ca9 to your computer and use it in GitHub Desktop.
TwitterHelper API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Collections.Generic; | |
using System.Windows.Forms; | |
namespace TwitterHelper | |
{ | |
public class Twitter | |
{ | |
string RootPath, handle; | |
public TwitterUser User { get; private set; } | |
public Twitter(string handle, string rootPath = @"X:\TwitterImages\") | |
{ | |
this.RootPath = rootPath; | |
this.handle = handle; | |
if (!Directory.Exists(this.RootPath)) | |
{ | |
Directory.CreateDirectory(this.RootPath); | |
} | |
this.User = Helpers.GetUserByHandle(handle); | |
} | |
public string SaveImageToFS() | |
{ | |
string handle = User.handle; | |
string path = this.RootPath + handle; | |
if (!Directory.Exists(path)) | |
{ | |
Directory.CreateDirectory(path); | |
} | |
if (this.User == null) | |
{ | |
MessageBox.Show("User Not Found", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); | |
return null; | |
} | |
try | |
{ | |
WebClient webClient = new WebClient(); | |
string filePathWithName = path + "\\" + DateTime.Now.ToString().Split(' ')[0] + GetFileExtensionFromUrl(User.profile_image_url_https_original); | |
if(File.Exists(filePathWithName)){ | |
try { | |
File.Delete(filePathWithName); | |
} catch (Exception) { | |
filePathWithName = path + "\\" + DateTime.Now.ToString().Split(' ')[0] + DateTime.UtcNow.Second.ToString() + GetFileExtensionFromUrl(User.profile_image_url_https_original); | |
} | |
} | |
MessageBox.Show(filePathWithName); | |
webClient.DownloadFile(User.profile_image_url_https_original, filePathWithName); | |
return filePathWithName; | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message, ex.ToString()); | |
return null; | |
} | |
} | |
private static string GetFileExtensionFromUrl(string url) | |
{ | |
url = url.Split('?')[0]; | |
url = url.Split('/').Last(); | |
return url.Contains('.') ? url.Substring(url.LastIndexOf('.')) : ""; | |
} | |
} | |
} | |
namespace TwitterHelper | |
{ | |
public class Helpers | |
{ | |
public static string ApiURL = "https://twitter.khare.co.in/api/twitter/"; | |
public static string GetUserStringByHandle(string handle) | |
{ | |
RESTClient rClient = new RESTClient(); | |
rClient.endPoint = ApiURL + handle.ToLower(); | |
return rClient.makeRequest(); | |
} | |
public static TwitterUser GetUserByHandle(string handle) | |
{ | |
string strJSON = Helpers.GetUserStringByHandle(handle); | |
TwitterUser user = new TwitterUser(); | |
Newtonsoft.Json.JsonConvert.PopulateObject(strJSON, user); | |
return (user != null && user.id != null) ? user : null; | |
} | |
public static TwitterUser CreateUserByRawJson(string RawJson) | |
{ | |
string strJSON = RawJson; | |
TwitterUser user = new TwitterUser(); | |
Newtonsoft.Json.JsonConvert.PopulateObject(strJSON, user); | |
return (user != null && user.id != null) ? user : null; | |
} | |
} | |
} | |
namespace TwitterHelper | |
{ | |
public enum httpVerb | |
{ | |
GET, | |
POST, | |
PUT, | |
DELETE | |
} | |
class RESTClient | |
{ | |
public string endPoint { get; set; } | |
public httpVerb httpMethod { get; set; } | |
public RESTClient() | |
{ | |
endPoint = ""; | |
httpMethod = httpVerb.GET; | |
} | |
public string makeRequest() | |
{ | |
string strResponseValue = string.Empty; | |
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint); | |
request.Method = httpMethod.ToString(); | |
HttpWebResponse response = null; | |
try | |
{ | |
response = (HttpWebResponse)request.GetResponse(); | |
//Proecess the resppnse stream... (could be JSON, XML or HTML etc..._ | |
using (Stream responseStream = response.GetResponseStream()) | |
{ | |
if (responseStream != null) | |
{ | |
using (StreamReader reader = new StreamReader(responseStream)) | |
{ | |
strResponseValue = reader.ReadToEnd(); | |
} | |
} | |
} | |
} | |
catch (Exception ex) | |
{ | |
strResponseValue = "{\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}"; | |
} | |
finally | |
{ | |
if (response != null) | |
{ | |
((IDisposable)response).Dispose(); | |
} | |
} | |
return strResponseValue; | |
} | |
} | |
} | |
namespace TwitterHelper | |
{ | |
public class Thumb | |
{ | |
public int w { get; set; } | |
public int h { get; set; } | |
public string resize { get; set; } | |
} | |
public class Large | |
{ | |
public int w { get; set; } | |
public int h { get; set; } | |
public string resize { get; set; } | |
} | |
public class Small | |
{ | |
public int w { get; set; } | |
public int h { get; set; } | |
public string resize { get; set; } | |
} | |
public class Medium2 | |
{ | |
public int w { get; set; } | |
public int h { get; set; } | |
public string resize { get; set; } | |
} | |
public class Sizes | |
{ | |
public Thumb thumb { get; set; } | |
public Large large { get; set; } | |
public Small small { get; set; } | |
public Medium2 medium { get; set; } | |
} | |
public class Medium | |
{ | |
public long id { get; set; } | |
public string id_str { get; set; } | |
public List<int> indices { get; set; } | |
public string media_url { get; set; } | |
public string media_url_https { get; set; } | |
public string url { get; set; } | |
public string display_url { get; set; } | |
public string expanded_url { get; set; } | |
public string type { get; set; } | |
public Sizes sizes { get; set; } | |
} | |
public class Status | |
{ | |
public string created_at { get; set; } | |
public string id { get; set; } | |
public string full_text { get; set; } | |
public string text { get; set; } | |
public List<Medium> media { get; set; } | |
} | |
public class TwitterUser | |
{ | |
public string id { get; set; } | |
public string handle { get; set; } | |
public string name { get; set; } | |
public string description { get; set; } | |
public string url { get; set; } | |
public string location { get; set; } | |
public int followers_count { get; set; } | |
public int friends_count { get; set; } | |
public int listed_count { get; set; } | |
public int statuses_count { get; set; } | |
public bool verified { get; set; } | |
public Status status { get; set; } | |
public string profile_background_color { get; set; } | |
public string profile_link_color { get; set; } | |
public string profile_sidebar_border_color { get; set; } | |
public string profile_sidebar_fill_color { get; set; } | |
public string profile_text_color { get; set; } | |
public string profile_background_image_url_https { get; set; } | |
public string profile_banner_url { get; set; } | |
public string profile_image_url_https { get; set; } | |
public string profile_image_url_https_original { get; set; } | |
} | |
} |
Author
amitkhare
commented
Mar 10, 2020
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment