Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- This script is created for "Twitter REST API tools for Unity" to support media files fetching (images, videos etc.)
- https://ctinney94.itch.io/unity-twitter-tools
- Add this class in "Twitter Tools\Plugins\Twitter Rest API Access Tools\DataStructures"
- =====================
- Name: Media Classes.cs
- Author: Krishna Kumar
- Update Date: 29, Jul, 2020
- Description:
- =====================
- */
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using System;
- namespace Web.Twitter.DataStructures
- {
- [Serializable]
- public class Hashtag
- {
- public string text; // { get; set; }
- public List<int> indices; // { get; set; }
- }
- [Serializable]
- public class Symbol
- {
- public string text; // { get; set; }
- public List<int> indices; // { get; set; }
- }
- [Serializable]
- public class Url
- {
- public string url; // { get; set; }
- public string expanded_url; // { get; set; }
- public string display_url; // { get; set; }
- public List<int> indices; // { get; set; }
- }
- [Serializable]
- public class UserMention
- {
- public string screen_name; // { get; set; }
- public string name; // { get; set; }
- public int id; // { get; set; }
- public string id_str; // { get; set; }
- public List<int> indices; // { get; set; }
- }
- [Serializable]
- public class Medium
- {
- public int w; // { get; set; }
- public int h; // { get; set; }
- public string resize; // { get; set; }
- }
- [Serializable]
- public class Large
- {
- public int w; // { get; set; }
- public int h; // { get; set; }
- public string resize; // { get; set; }
- }
- [Serializable]
- public class Thumb
- {
- public int w; // { get; set; }
- public int h; // { get; set; }
- public string resize; // { get; set; }
- }
- [Serializable]
- public class Small
- {
- public int w; // { get; set; }
- public int h; // { get; set; }
- public string resize; // { get; set; }
- }
- [Serializable]
- public class Sizes
- {
- public Medium medium; // { get; set; }
- public Large large; // { get; set; }
- public Thumb thumb; // { get; set; }
- public Small small; // { get; set; }
- }
- [Serializable]
- public class Media
- {
- 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 VideoInfo video_info; // { get; set; }
- }
- [Serializable]
- public class Entities
- {
- //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object
- public List<Hashtag> hashtags; // { get; set; }
- public List<Symbol> symbols; // { get; set; }
- public List<Url> urls; // { get; set; }
- public List<UserMention> user_mentions; // { get; set; }
- public List<Media> media; // { get; set; }
- }
- [Serializable]
- public class ExtendedEntities
- {
- //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/extended-entities-object
- public List<Media> media; //{ get; set; }
- }
- [Serializable]
- public class Variant
- {
- public int bitrate; // { get; set; }
- public string content_type; // { get; set; }
- public string url; // { get; set; }
- }
- [Serializable]
- public class VideoInfo
- {
- public List<int> aspect_ratio; // { get; set; }
- public int duration_millis; // { get; set; }
- public List<Variant> variants; // { get; set; }
- }
- }
- =======Updated Tweet.cs (Replace with older Tweet.cs file )======
- using System;
- using kFramework;
- using UnityEngine;
- using System.Collections;
- using System.Collections.Generic;
- namespace Web.Twitter.DataStructures
- {
- [Serializable]
- public class Tweet
- {
- //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object
- const string Const_TwitterDateTemplate = "ddd MMM dd HH:mm:ss +ffff yyyy";
- public string created_at;
- public string id;
- public string lang;
- public string text;
- public string source;
- public string in_reply_to_status_id;
- public string in_reply_to_user_id;
- public string in_reply_to_screen_name;
- public UserProfile user;
- public Tweet retweeted_status;
- public string geo;
- public string coordinates;
- public string place;
- public string contributors;
- public bool is_quote_status;
- public int retweet_count;
- public int favorite_count;
- public bool retweeted;
- public bool favourited;
- //AddedByKK
- public Entities entities;
- public ExtendedEntities extended_entities;
- //==============
- public DateTime GetDateTime()
- {
- DateTime createdAt = DateTime.ParseExact(created_at,
- Const_TwitterDateTemplate, new System.Globalization.CultureInfo("en-US"));
- // Debug.Log("entities " + entities == null);
- //GetThumbnailImageURL();
- return createdAt;
- }
- public string GetAnyMediaImageURL()
- {
- string photoMediaURL = null;
- if (photoMediaURL == null)
- photoMediaURL = TryGetEntitiesImageURL();
- if (photoMediaURL == null)
- photoMediaURL = TryGetExtendedEntitiesImageURL();
- return photoMediaURL;
- }
- public string TryGetEntitiesImageURL()
- {
- string photoMediaURL = null;
- if (entities != null && entities.media != null && entities.media.Count > 0)
- {
- //Debug.Log(entities.media.Count);
- List<Media> medias = entities.media;
- for (int index = 0; index < medias.Count; index++)
- {
- var curMedia = medias[index];
- //Debug.Log("Count " + entities.media.Count + " Media URL " + curMedia.media_url + " URL " + curMedia.url + " Media Type " + curMedia.type);
- if (curMedia != null)// if (curMedia.type == "photo")
- {
- photoMediaURL = curMedia.media_url;
- break;
- }
- }
- }
- return photoMediaURL;
- }
- public string TryGetExtendedEntitiesImageURL()
- {
- string mediaURL = null;
- if (extended_entities != null && extended_entities.media != null && extended_entities.media.Count > 0)
- {
- //Debug.Log(entities.media.Count);
- List<Media> medias = extended_entities.media;
- for (int index = 0; index < medias.Count; index++)
- {
- var curMedia = medias[index];
- //Debug.Log("Count " + entities.media.Count + " Media URL " + curMedia.media_url + " URL " + curMedia.url + " Media Type " + curMedia.type);
- if (curMedia != null)// if (curMedia.type == "photo")
- {
- mediaURL = curMedia.media_url;
- break;
- }
- }
- }
- return mediaURL;
- }
- public string GetThumbnailImageURL()
- {
- //https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/entities-object#size
- string thumbnailImageURL = GetAnyMediaImageURL();
- if (thumbnailImageURL != null)
- {
- //Debug.Log("FileNameFromURL " + kWebUtility.GetFileNameFromURL(thumbnailImageURL, false, false));
- //Appending keyword to get thumbnail image of the twitter pic
- thumbnailImageURL += ":thumb";
- }
- return thumbnailImageURL;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement