Advertisement
Guest User

Untitled

a guest
May 25th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.83 KB | None | 0 0
  1. import Names = require("../../names");
  2. import HttpiService = require("./HttpiService");
  3. import IHttpService = angular.IHttpService;
  4. import IHttpPromise = angular.IHttpPromise;
  5.  
  6. export interface IInstagramService {
  7. getPhotosFromLocation(locationId): IHttpPromise<any>;
  8. getLocationByCoordinates(long, lat): IHttpPromise<any>;
  9. login();
  10. }
  11.  
  12. export class InstagramService implements IInstagramService{
  13. public isLoggedIn = false;
  14. public loggedInUser = null;
  15. private accessToken = null;
  16. private clientId = "3300cb45b8a243ddafd1b09d693df0fd";
  17. private redirectUri = "http://latishka.com/";
  18.  
  19. // @ngInject
  20. constructor(private $httpi: HttpiService,
  21. private $q: ng.IQService) {
  22. }
  23.  
  24. private parseRedirectUri(url){
  25. return _.chain( url.slice(this.redirectUri.length).split('&') )
  26. // Split each array item into [key, value]
  27. // ignore empty string if search is empty
  28. .map(function(item) { if (item) return item.split('='); })
  29. // Remove undefined in the case the search is empty
  30. .compact()
  31. // Turn [key, value] arrays into object parameters
  32. .object()
  33. // Return the value of the chain operation
  34. .value();
  35. }
  36.  
  37. public login() {
  38. var deferred = this.$q.defer();
  39.  
  40. var config = {
  41. method: "get",
  42. url: "https://api.instagram.com/oauth/authorize/?client_id=:clientId&redirect_uri=:redirectUri&response_type=token",
  43. data: {
  44. clientId: this.clientId,
  45. redirectUri: "http://latishka.com/"
  46. }
  47. };
  48.  
  49. this.$httpi.httpProxy(config).then((response: any) => {
  50. debugger;
  51.  
  52. var params = this.parseRedirectUri(response);
  53. if(params["access_token"]){
  54. this.accessToken = params["access_token"];
  55. }
  56. deferred.resolve(this.accessToken);
  57. }).catch((rejection) => {
  58. deferred.reject(rejection);
  59. });
  60.  
  61. return deferred.promise;
  62. }
  63.  
  64. public getLocationByCoordinates(lng, lat): IHttpPromise<any> {
  65. var deferred = this.$q.defer();
  66.  
  67. var config = {
  68. method: "get",
  69. url: "https://api.instagram.com/v1/locations/search?lat=:latitude&lng=:longtitude",
  70. data: {
  71. latitude: lat,
  72. longtitude: lng
  73. }
  74. };
  75.  
  76. this.request(config).then((response) => {
  77. if(response && response.data){
  78. deferred.resolve(response.data);
  79. }
  80. });
  81.  
  82. return deferred.promise;
  83. }
  84.  
  85. public getPhotosFromLocation(locationId): IHttpPromise<any> {
  86. var deferred = this.$q.defer();
  87.  
  88. var config = {
  89. method: "get",
  90. url: "https://api.instagram.com/v1/locations/:locationId/media/recent",
  91. data: {
  92. locationId: locationId,
  93. }
  94. };
  95.  
  96. this.request(config).then((response)=>{
  97. deferred.resolve(response);
  98. }).catch((rejection) => {
  99. deferred.reject(rejection);
  100. });
  101.  
  102. return deferred.promise;
  103. }
  104.  
  105.  
  106. private request(config, jsonPCallback?): IHttpPromise<any> {
  107. var deferred = this.$q.defer();
  108.  
  109. if(!this.isLoggedIn) {
  110. var that = this;
  111. this.login().then(()=> {
  112. if(config.method.toLocaleLowerCase() === "get"){
  113. config.url += "&access_token=" + that.accessToken;
  114. } else {
  115. config.data["access_token"] = that.accessToken;
  116. }
  117. if(jsonPCallback) {
  118. config.url += "&callback=jsonPCallback";
  119. }
  120. that.$httpi.httpProxy(config).then((response: any) => {
  121. deferred.resolve(response);
  122. }).catch((rejection) => {
  123. deferred.reject(rejection);
  124. });
  125. }).catch((rejection) => {
  126. deferred.reject(rejection);
  127. });
  128. } else {
  129. if(config.method.toLocaleLowerCase() === "get"){
  130. config.url += "&access_token=" + that.accessToken;
  131. } else {
  132. config.data["access_token"] = that.accessToken;
  133. }
  134. if(jsonPCallback) {
  135. config.url += "&callback=jsonPCallback";
  136. }
  137. this.$httpi.httpProxy(config).then((response: any) => {
  138. deferred.resolve(response);
  139. }).catch((rejection) => {
  140. deferred.reject(rejection);
  141. });
  142. }
  143.  
  144. return deferred.promise;
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement