/core/externals/update-engine/externals/gdata-objectivec-client/Source/Clients/YouTube/GDataServiceGoogleYouTube.m

http://macfuse.googlecode.com/ · Objective C · 256 lines · 144 code · 63 blank · 49 comment · 9 complexity · 62c65c2a24b15a2a74718a9e2475c997 MD5 · raw file

  1. /* Copyright (c) 2008 Google Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. //
  16. // GDataServiceGoogleYouTube.m
  17. //
  18. #if !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE
  19. #define GDATASERVICEYOUTUBE_DEFINE_GLOBALS 1
  20. #import "GDataServiceGoogleYouTube.h"
  21. #import "GDataYouTubeConstants.h"
  22. #import "GDataQueryYouTube.h"
  23. #import "GDataEntryYouTubeVideo.h"
  24. #import "GDataEntryYouTubeUpload.h"
  25. @interface GDataServiceGoogle (PrivateMethods)
  26. // The YouTube service will be overriding this private superclass method
  27. - (GDataServiceTicket *)fetchAuthenticatedObjectWithURL:(NSURL *)objectURL
  28. objectClass:(Class)objectClass
  29. objectToPost:(GDataObject *)objectToPost
  30. ETag:(NSString *)etag
  31. httpMethod:(NSString *)httpMethod
  32. delegate:(id)delegate
  33. didFinishSelector:(SEL)finishedSelector
  34. completionHandler:(GDataServiceGoogleCompletionHandler)completionHandler;
  35. @end
  36. @implementation GDataServiceGoogleYouTube
  37. - (void)dealloc {
  38. [developerKey_ release];
  39. [super dealloc];
  40. }
  41. + (NSURL *)youTubeURLForFeedID:(NSString *)feedID {
  42. // like
  43. //
  44. // http://gdata.youtube.com/feeds/api/videos
  45. //
  46. // or
  47. //
  48. // http://gdata.youtube.com/feeds/api/standardfeeds/feedid
  49. //
  50. // See http://code.google.com/apis/youtube/2.0/reference.html#Standard_feeds for feed IDs
  51. NSString *endPart;
  52. if (feedID == nil) {
  53. endPart = @"videos";
  54. } else {
  55. endPart = [NSString stringWithFormat:@"standardfeeds/%@", feedID];
  56. }
  57. NSString *root = [self serviceRootURLString];
  58. NSString *const templateStr = @"%@api/%@";
  59. NSString *urlString = [NSString stringWithFormat:templateStr, root, endPart];
  60. return [NSURL URLWithString:urlString];
  61. }
  62. + (NSURL *)youTubeURLForChannelsFeeds {
  63. // feed counterpart to http://www.youtube.com/channels
  64. //
  65. // this is most useful as part of a query searching for text
  66. NSString *root = [self serviceRootURLString];
  67. NSString *const templateStr = @"%@api/channels";
  68. NSString *urlString = [NSString stringWithFormat:templateStr, root];
  69. return [NSURL URLWithString:urlString];
  70. }
  71. + (NSURL *)youTubeURLForChannelStandardFeedID:(NSString *)feedID {
  72. NSString *root = [self serviceRootURLString];
  73. NSString *const templateStr = @"%@api/channelstandardfeeds/%@";
  74. NSString *urlString = [NSString stringWithFormat:templateStr, root, feedID];
  75. return [NSURL URLWithString:urlString];
  76. }
  77. + (NSURL *)youTubeURLForUserID:(NSString *)userID
  78. userFeedID:(NSString *)feedID {
  79. // Make a URL like
  80. // http://gdata.youtube.com/feeds/api/users/username/favorites
  81. //
  82. // userID may be kGDataServiceDefaultUser
  83. NSString *encodedUserID = [GDataUtilities stringByURLEncodingForURI:userID];
  84. NSString *endPart;
  85. if (feedID == nil) {
  86. endPart = @"";
  87. } else {
  88. endPart = [NSString stringWithFormat:@"/%@", feedID];
  89. }
  90. NSString *root = [self serviceRootURLString];
  91. NSString *const templateStr = @"%@api/users/%@%@";
  92. NSString *urlString = [NSString stringWithFormat:templateStr, root,
  93. encodedUserID, endPart];
  94. return [NSURL URLWithString:urlString];
  95. }
  96. + (NSURL *)youTubeActivityFeedURLForUserID:(NSString *)userID {
  97. // Make a URL like
  98. // http://gdata.youtube.com/feeds/api/events?author=usernames
  99. // (usernames can be a comma-separated list)
  100. //
  101. // For a friends activity feed, call youTubeURLForUserID:userFeedID:
  102. // with kGDataServiceDefaultUser and kGDataYouTubeUserFeedIDFriendsActivity
  103. NSString *encodedUserID = [GDataUtilities stringByURLEncodingStringParameter:userID];
  104. NSString *root = [self serviceRootURLString];
  105. NSString *const templateStr = @"%@api/events?author=%@";
  106. NSString *urlString = [NSString stringWithFormat:templateStr,
  107. root, encodedUserID];
  108. return [NSURL URLWithString:urlString];
  109. }
  110. + (NSURL *)youTubeUploadURLForUserID:(NSString *)userID {
  111. // Make a URL like
  112. // https://uploads.gdata.youtube.com/feeds/api/users/username/uploads
  113. //
  114. // userID may be "default" to indicate the currently authenticated user
  115. NSString *encodedUserID = [GDataUtilities stringByURLEncodingForURI:userID];
  116. NSString *root = [self serviceUploadRootURLString];
  117. NSString *const templateStr = @"%@api/users/%@/uploads";
  118. NSString *urlString = [NSString stringWithFormat:templateStr,
  119. root, encodedUserID];
  120. return [NSURL URLWithString:urlString];
  121. }
  122. - (NSString *)youTubeDeveloperKey {
  123. return developerKey_;
  124. }
  125. - (void)setYouTubeDeveloperKey:(NSString *)str {
  126. [developerKey_ autorelease];
  127. developerKey_ = [str copy];
  128. }
  129. #pragma mark -
  130. // overrides of the superclass
  131. - (NSMutableURLRequest *)requestForURL:(NSURL *)url
  132. ETag:(NSString *)etag
  133. httpMethod:(NSString *)httpMethod
  134. ticket:(GDataServiceTicketBase *)ticket {
  135. // if the request is for posting, add the developer key, if it's known
  136. NSMutableURLRequest *request = [super requestForURL:url
  137. ETag:etag
  138. httpMethod:httpMethod
  139. ticket:ticket];
  140. // set the developer key, if any
  141. NSString *developerKey = [self youTubeDeveloperKey];
  142. if ([developerKey length] > 0) {
  143. NSString *value = [NSString stringWithFormat:@"key=%@", developerKey];
  144. [request setValue:value forHTTPHeaderField:@"X-GData-Key"];
  145. }
  146. return request;
  147. }
  148. // when authenticating, add the Content-Type header required by YouTube
  149. - (NSDictionary *)customAuthenticationRequestHeaders {
  150. return [NSDictionary dictionaryWithObject:@"application/x-www-form-urlencoded"
  151. forKey:@"Content-Type"];
  152. }
  153. - (GDataServiceTicket *)fetchAuthenticatedObjectWithURL:(NSURL *)objectURL
  154. objectClass:(Class)objectClass
  155. objectToPost:(GDataObject *)objectToPost
  156. ETag:(NSString *)etag
  157. httpMethod:(NSString *)httpMethod
  158. delegate:(id)delegate
  159. didFinishSelector:(SEL)finishedSelector
  160. completionHandler:(GDataServiceGoogleCompletionHandler)completionHandler {
  161. if ([objectClass isSubclassOfClass:[GDataEntryYouTubeUpload class]]) {
  162. // when uploading, expect a full video entry back
  163. objectClass = [GDataEntryYouTubeVideo class];
  164. }
  165. return [super fetchAuthenticatedObjectWithURL:objectURL
  166. objectClass:objectClass
  167. objectToPost:objectToPost
  168. ETag:etag
  169. httpMethod:httpMethod
  170. delegate:delegate
  171. didFinishSelector:finishedSelector
  172. completionHandler:completionHandler];
  173. }
  174. + (NSString *)serviceID {
  175. return @"youtube";
  176. }
  177. + (NSString *)serviceRootURLString {
  178. return @"https://gdata.youtube.com/feeds/";
  179. }
  180. + (NSString *)serviceUploadRootURLString {
  181. return @"https://uploads.gdata.youtube.com/resumable/feeds/";
  182. }
  183. + (NSString *)defaultServiceVersion {
  184. return kGDataYouTubeDefaultServiceVersion;
  185. }
  186. + (NSUInteger)defaultServiceUploadChunkSize {
  187. return kGDataStandardUploadChunkSize;
  188. }
  189. + (NSDictionary *)standardServiceNamespaces {
  190. return [GDataYouTubeConstants youTubeNamespaces];
  191. }
  192. @end
  193. #endif // !GDATA_REQUIRE_SERVICE_INCLUDES || GDATA_INCLUDE_YOUTUBE_SERVICE