Advertisement
krishx007

Twitter REST API tools for Unity (MediaFileSupport)

Jul 29th, 2020 (edited)
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.42 KB | None | 0 0
  1. /**
  2. This script is created for "Twitter REST API tools for Unity" to support media files fetching (images, videos etc.)
  3. https://ctinney94.itch.io/unity-twitter-tools
  4.  
  5. Add this class in "Twitter Tools\Plugins\Twitter Rest API Access Tools\DataStructures"
  6. =====================
  7. Name: Media Classes.cs
  8. Author: Krishna Kumar
  9. Update Date: 29, Jul, 2020
  10. Description:
  11. =====================
  12. */
  13.  
  14. using System.Collections;
  15. using System.Collections.Generic;
  16. using UnityEngine;
  17. using System;
  18.  
  19. namespace Web.Twitter.DataStructures
  20. {
  21. [Serializable]
  22. public class Hashtag
  23. {
  24. public string text; // { get; set; }
  25. public List<int> indices; // { get; set; }
  26. }
  27.  
  28. [Serializable]
  29. public class Symbol
  30. {
  31. public string text; // { get; set; }
  32. public List<int> indices; // { get; set; }
  33. }
  34.  
  35. [Serializable]
  36. public class Url
  37. {
  38. public string url; // { get; set; }
  39. public string expanded_url; // { get; set; }
  40. public string display_url; // { get; set; }
  41. public List<int> indices; // { get; set; }
  42. }
  43.  
  44. [Serializable]
  45. public class UserMention
  46. {
  47. public string screen_name; // { get; set; }
  48. public string name; // { get; set; }
  49. public int id; // { get; set; }
  50. public string id_str; // { get; set; }
  51. public List<int> indices; // { get; set; }
  52. }
  53.  
  54. [Serializable]
  55. public class Medium
  56. {
  57. public int w; // { get; set; }
  58. public int h; // { get; set; }
  59. public string resize; // { get; set; }
  60. }
  61.  
  62. [Serializable]
  63. public class Large
  64. {
  65. public int w; // { get; set; }
  66. public int h; // { get; set; }
  67. public string resize; // { get; set; }
  68. }
  69.  
  70. [Serializable]
  71. public class Thumb
  72. {
  73. public int w; // { get; set; }
  74. public int h; // { get; set; }
  75. public string resize; // { get; set; }
  76. }
  77.  
  78. [Serializable]
  79. public class Small
  80. {
  81. public int w; // { get; set; }
  82. public int h; // { get; set; }
  83. public string resize; // { get; set; }
  84. }
  85.  
  86. [Serializable]
  87. public class Sizes
  88. {
  89. public Medium medium; // { get; set; }
  90. public Large large; // { get; set; }
  91. public Thumb thumb; // { get; set; }
  92. public Small small; // { get; set; }
  93. }
  94.  
  95. [Serializable]
  96. public class Media
  97. {
  98. public long id; //{ get; set; }
  99. public string id_str; // { get; set; }
  100. public List<int> indices; // { get; set; }
  101. public string media_url; // { get; set; }
  102. public string media_url_https; // { get; set; }
  103. public string url; // { get; set; }
  104. public string display_url; // { get; set; }
  105. public string expanded_url; // { get; set; }
  106. public string type; // { get; set; }
  107. public Sizes sizes; // { get; set; }
  108.  
  109. public VideoInfo video_info; // { get; set; }
  110. }
  111.  
  112. [Serializable]
  113. public class Entities
  114. {
  115. //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object
  116.  
  117. public List<Hashtag> hashtags; // { get; set; }
  118. public List<Symbol> symbols; // { get; set; }
  119. public List<Url> urls; // { get; set; }
  120. public List<UserMention> user_mentions; // { get; set; }
  121. public List<Media> media; // { get; set; }
  122. }
  123.  
  124. [Serializable]
  125. public class ExtendedEntities
  126. {
  127. //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object
  128.  
  129. public List<Media> media; //{ get; set; }
  130. }
  131.  
  132. [Serializable]
  133. public class Variant
  134. {
  135. public int bitrate; // { get; set; }
  136. public string content_type; // { get; set; }
  137. public string url; // { get; set; }
  138. }
  139. [Serializable]
  140. public class VideoInfo
  141. {
  142. public List<int> aspect_ratio; // { get; set; }
  143. public int duration_millis; // { get; set; }
  144. public List<Variant> variants; // { get; set; }
  145. }
  146.  
  147. }
  148.  
  149.  
  150. =======Updated Tweet.cs (Replace with older Tweet.cs file )======
  151. using System;
  152. using kFramework;
  153.  
  154. using UnityEngine;
  155. using System.Collections;
  156. using System.Collections.Generic;
  157.  
  158. namespace Web.Twitter.DataStructures
  159. {
  160. [Serializable]
  161. public class Tweet
  162. {
  163. //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object
  164. const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
  165.  
  166. public string created_at;
  167. public string id;
  168. public string lang;
  169. public string text;
  170. public string source;
  171. public string in_reply_to_status_id;
  172. public string in_reply_to_user_id;
  173. public string in_reply_to_screen_name;
  174. public UserProfile user;
  175. public Tweet retweeted_status;
  176. public string geo;
  177. public string coordinates;
  178. public string place;
  179. public string contributors;
  180. public bool is_quote_status;
  181. public int retweet_count;
  182. public int favorite_count;
  183. public bool retweeted;
  184. public bool favourited;
  185.  
  186. //AddedByKK
  187. public Entities entities;
  188. public ExtendedEntities extended_entities;
  189. //==============
  190.  
  191.  
  192.  
  193. public DateTime GetDateTime()
  194. {
  195. DateTime createdAt = DateTime.ParseExact(created_at,
  196. Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
  197.  
  198. // Debug.Log("entities " + entities == null);
  199. //GetThumbnailImageURL();
  200.  
  201. return createdAt;
  202. }
  203.  
  204. public string GetAnyMediaImageURL()
  205. {
  206. string photoMediaURL = null;
  207.  
  208. if (photoMediaURL == null)
  209. photoMediaURL = TryGetEntitiesImageURL();
  210.  
  211. if (photoMediaURL == null)
  212. photoMediaURL = TryGetExtendedEntitiesImageURL();
  213.  
  214. return photoMediaURL;
  215. }
  216.  
  217. public string TryGetEntitiesImageURL()
  218. {
  219. string photoMediaURL = null;
  220.  
  221. if (entities != null && entities.media != null && entities.media.Count > 0)
  222. {
  223. //Debug.Log(entities.media.Count);
  224. List<Media> medias = entities.media;
  225.  
  226. for (int index = 0; index < medias.Count; index++)
  227. {
  228. var curMedia = medias[index];
  229. //Debug.Log("Count " + entities.media.Count + " Media URL " + curMedia.media_url + " URL " + curMedia.url + " Media Type " + curMedia.type);
  230.  
  231. if (curMedia != null)// if (curMedia.type == "photo")
  232. {
  233. photoMediaURL = curMedia.media_url;
  234. break;
  235. }
  236.  
  237.  
  238. }
  239. }
  240.  
  241. return photoMediaURL;
  242. }
  243.  
  244. public string TryGetExtendedEntitiesImageURL()
  245. {
  246. string mediaURL = null;
  247.  
  248. if (extended_entities != null && extended_entities.media != null && extended_entities.media.Count > 0)
  249. {
  250. //Debug.Log(entities.media.Count);
  251. List<Media> medias = extended_entities.media;
  252.  
  253. for (int index = 0; index < medias.Count; index++)
  254. {
  255. var curMedia = medias[index];
  256. //Debug.Log("Count " + entities.media.Count + " Media URL " + curMedia.media_url + " URL " + curMedia.url + " Media Type " + curMedia.type);
  257.  
  258. if (curMedia != null)// if (curMedia.type == "photo")
  259. {
  260. mediaURL = curMedia.media_url;
  261. break;
  262. }
  263.  
  264.  
  265. }
  266. }
  267.  
  268. return mediaURL;
  269. }
  270.  
  271.  
  272.  
  273. public string GetThumbnailImageURL()
  274. {
  275. //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object#size
  276.  
  277. string thumbnailImageURL = GetAnyMediaImageURL();
  278.  
  279. if (thumbnailImageURL != null)
  280. {
  281. //Debug.Log("FileNameFromURL " + kWebUtility.GetFileNameFromURL(thumbnailImageURL, false, false));
  282. //Appending keyword to get thumbnail image of the twitter pic
  283. thumbnailImageURL += ":thumb";
  284.  
  285. }
  286.  
  287. return thumbnailImageURL;
  288. }
  289.  
  290. }
  291. }
  292.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement