lib/ayadn/api.rb in ayadn-0.6.4 vs lib/ayadn/api.rb in ayadn-1.0.0
- old
+ new
@@ -1,302 +1,313 @@
-#!/usr/bin/env ruby
# encoding: utf-8
-class AyaDN
- class API
- def initialize(token)
- @token = token
- @endpoints = AyaDN::Endpoints.new(@token)
- end
- def makeAuthorizeURL
- @endpoints.authorize_url
- end
+module Ayadn
+ class API
- def getHash
- JSON.parse(http_get(@url))
- end
+ def get_unified(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['unified']}
+ end
+ get_parsed_response(Endpoints.new.unified(options))
+ end
- def checkLastPageID(last_page_id)
- @url += "&since_id=#{last_page_id}" if last_page_id != nil
- end
+ def get_checkins(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['explore:checkins']}
+ end
+ get_parsed_response(Endpoints.new.checkins(options))
+ end
- def getAPIConfig
- @url = CONFIG_API_URL
- getHash
- end
+ def get_global(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['global']}
+ end
+ get_parsed_response(Endpoints.new.global(options))
+ end
- def getGlobal(last_page_id)
- @url = @endpoints.global
- @url += @endpoints.base_params
- @url += @endpoints.include_directed if $tools.config['timeline']['directed']
- checkLastPageID(last_page_id)
- getHash
- end
- def getUnified(last_page_id)
- @url = @endpoints.unified
- @url += @endpoints.base_params
- @url += @endpoints.include_directed if $tools.config['timeline']['directed']
- checkLastPageID(last_page_id)
- getHash
- end
- def getSimpleUnified
- @url = @endpoints.unified_streamback
- @url += @endpoints.base_params
- @url += @endpoints.include_directed if $tools.config['timeline']['directed']
- getHash
- end
- def getInteractions
- @url = @endpoints.interactions
- #checkLastPageID(last_page_id)
- getHash
- end
- def getHashtags(tag)
- @url = @endpoints.hashtags(tag)
- getHash
- end
- def getExplore(stream, last_page_id)
- @url = @endpoints.explore(stream)
- @url += @endpoints.base_params
- checkLastPageID(last_page_id)
- getHash
- end
- def getUserMentions(username, last_page_id)
- @url = @endpoints.mentions(username)
- @url += @endpoints.light_params
- checkLastPageID(last_page_id)
- getHash
- end
- def getUserPosts(username, last_page_id)
- @url = @endpoints.posts(username)
- @url += @endpoints.base_params
- checkLastPageID(last_page_id)
- getHash
- end
- def getUserInfos(username)
- @url = @endpoints.user_info(username)
- @url += @endpoints.base_params
- getHash
- end
- def getWhoReposted(post_id)
- @url = @endpoints.who_reposted(post_id)
- @url += @endpoints.light_params
- getHash
- end
- def getWhoStarred(post_id)
- @url = @endpoints.who_starred(post_id)
- @url += @endpoints.light_params
- getHash
- end
- def getPostInfos(post_id)
- @url = @endpoints.single_post(post_id)
- @url += @endpoints.base_params
- getHash
- end
- def getSinglePost(post_id)
- @url = @endpoints.single_post(post_id)
- @url += @endpoints.light_params
- getHash
- end
- def getStarredPosts(username)
- @url = @endpoints.starred_posts(username)
- @url += @endpoints.light_params
- getHash
- end
- def getPostReplies(post_id)
- @url = @endpoints.replies(post_id)
- @url += @endpoints.base_params
- getHash
- end
- def getPostMentions(post_id)
- @url = @endpoints.single_post(post_id)
- @url += @endpoints.light_params
- theHash = getHash
- postInfo = theHash['data']
- #rawText = postInfo['text']
- postMentionsArray = []
- postInfo['entities']['mentions'].each { |item| postMentionsArray.push(item['name']) }
- return postMentionsArray, postInfo['user']['username'], postInfo['repost_of']
- end
- def getUserName(username)
- @url = @endpoints.user_info(username)
- @url += @endpoints.light_params
- theHash = getHash
- theHash['data']['username']
- end
- def goDelete(post_id)
- @url = @endpoints.single_post(post_id)
- @url += @endpoints.light_params
- isTherePost, isYours = ifExists(post_id)
- return isTherePost, isYours
- end
- def starPost(post_id)
- @url = @endpoints.star(post_id)
- @url += @endpoints.light_params
- httpPost(@url)
- end
- def unstarPost(post_id)
- @url = @endpoints.star(post_id)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def repostPost(post_id)
- @url = @endpoints.repost(post_id)
- @url += @endpoints.light_params
- httpPost(@url)
- end
- def unrepostPost(post_id)
- @url = @endpoints.repost(post_id)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def ifExists(post_id)
- theHash = getHash
- postInfo = theHash['data']
- return postInfo['text'], postInfo['user']['username']
- end
- def getOriginalPost(post_id)
- theHash = getHash
- theHash['data']['repost_of']['id']
- end
- def getUserFollowInfo(username)
- @url = @endpoints.user_info(username)
- @url += @endpoints.light_params
- theHash = getHash
- {you_follow: theHash['data']['you_follow'], follows_you: theHash['data']['follows_you']}
- end
- def getUserMuteInfo(username)
- @url = @endpoints.user_info(username)
- @url += @endpoints.light_params
- theHash = getHash
- theHash['data']['you_muted']
- end
- def getUserBlockInfo(username)
- @url = @endpoints.user_info(username)
- @url += @endpoints.light_params
- theHash = getHash
- theHash['data']['you_blocked']
- end
- def muteUser(username)
- @url = @endpoints.mute(username)
- @url += @endpoints.light_params
- httpPost(@url)
- end
- def unmuteUser(username)
- @url = @endpoints.mute(username)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def blockUser(username)
- @url = @endpoints.block(username)
- @url += @endpoints.light_params
- httpPost(@url)
- end
- def unblockUser(username)
- @url = @endpoints.block(username)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def followUser(username)
- @url = @endpoints.follow(username)
- @url += @endpoints.light_params
- httpPost(@url)
- end
- def unfollowUser(username)
- @url = @endpoints.follow(username)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def getFollowings(username, beforeID)
- @url = @endpoints.following(username)
- @url += @endpoints.light_params
- @url += "&count=200"
- @url += "&before_id=#{beforeID}" if beforeID != nil
- getHash
- end
- def getFollowers(username, beforeID)
- @url = @endpoints.followers(username)
- @url += @endpoints.light_params
- @url += "&count=200"
- @url += "&before_id=#{beforeID}" if beforeID != nil
- getHash
- end
- def getMuted(username, beforeID)
- @url = @endpoints.muted(username)
- @url += @endpoints.light_params
- @url += "&count=200"
- @url += "&before_id=#{beforeID}" if beforeID != nil
- getHash
- end
- def getBlocked(username, beforeID)
- @url = @endpoints.blocked(username)
- @url += @endpoints.light_params
- @url += "&count=200"
- @url += "&before_id=#{beforeID}" if beforeID != nil
- getHash
- end
- def getSearch(words)
- @url = @endpoints.search(words)
- @url += @endpoints.base_params
- getHash
- end
- def unique_message(channel_id, message_id)
- @url = @endpoints.get_message(channel_id, message_id)
- @url += @endpoints.base_params
- end
- def getUniqueMessage(channel_id, message_id)
- @url = @endpoints.get_message(channel_id, message_id)
- @url += @endpoints.base_params
- getHash
- end
- def getMessages(channel, last_page_id)
- @url = @endpoints.messages(channel)
- @url += @endpoints.base_params
- @url += "&include_machine=1"
- checkLastPageID(last_page_id)
- getHash
- end
- def get_pm_channels
- @url = @endpoints.channels
- @url += @endpoints.base_params
- @url += "&channel_types=net.app.core.pm"
- @url += "&include_recent_message=1"
- getHash
- end
- def get_channels
- @url = @endpoints.channels
- @url += @endpoints.base_params
- @url += "&include_recent_message=1"
- getHash
- end
- def getFilesList(beforeID)
- @url = @endpoints.files_list
- @url += @endpoints.light_params
- @url += "&before_id=#{beforeID}" if beforeID != nil
- getHash
- end
- def getSingleFile(file_id)
- @url = @endpoints.get_file(file_id)
- @url += @endpoints.light_params
- getHash
- end
- def getMultipleFiles(file_ids)
- @url = @endpoints.get_multiple_files(file_ids)
- @url += @endpoints.light_params
- getHash
- end
- def deleteFile(file_id)
- @url = @endpoints.get_file(file_id)
- @url += @endpoints.light_params
- $tools.checkHTTPResp(http_delete())
- end
- def deleteMessage(channel_id, message_id)
- @url = @endpoints.get_message(channel_id, message_id)
- @url += @endpoints.access_token
- $tools.checkHTTPResp(http_delete())
- end
- # def deactivateChannel(channel_id)
- # @url = CHANNELS_URL + "#{channel_id}?"
- # @url += @endpoints.access_token
- # resp = http_delete
- # $tools.checkHTTPResp(resp)
- # end
- end
-end
\ No newline at end of file
+ def get_trending(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['explore:trending']}
+ end
+ get_explore(:trending, options)
+ end
+ def get_photos(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['explore:photos']}
+ end
+ get_explore(:photos, options)
+ end
+ def get_conversations(options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination['explore:replies']}
+ end
+ get_explore(:conversations, options)
+ end
+
+ def get_explore(explore, options)
+ url = Endpoints.new.trending(options) if explore == :trending
+ url = Endpoints.new.photos(options) if explore == :photos
+ url = Endpoints.new.conversations(options) if explore == :conversations
+ get_parsed_response(url)
+ end
+
+ def get_mentions(username, options)
+ get_parsed_response(Endpoints.new.mentions(username, options))
+ end
+
+ def get_posts(username, options)
+ get_parsed_response(Endpoints.new.posts(username, options))
+ end
+
+ def get_whatstarred(username, options)
+ get_parsed_response(Endpoints.new.whatstarred(username, options))
+ end
+
+ def get_interactions
+ get_parsed_response(Endpoints.new.interactions)
+ end
+
+ def get_token_info
+ get_parsed_response(Endpoints.new.token_info)
+ end
+
+ def get_whoreposted(post_id)
+ get_parsed_response(Endpoints.new.whoreposted(post_id))
+ end
+
+ def get_whostarred(post_id)
+ get_parsed_response(Endpoints.new.whostarred(post_id))
+ end
+
+ def get_convo(post_id, options)
+ get_parsed_response(Endpoints.new.convo(post_id, options))
+ end
+
+ def get_hashtag(hashtag)
+ get_parsed_response(Endpoints.new.hashtag(hashtag))
+ end
+
+ def get_search(words, options)
+ get_parsed_response(Endpoints.new.search(words, options))
+ end
+
+ def get_followings(username)
+ build_list(username, :followings)
+ end
+
+ def get_followers(username)
+ build_list(username, :followers)
+ end
+
+ def get_muted
+ build_list(nil, :muted)
+ end
+
+ def get_blocked
+ build_list(nil, :blocked)
+ end
+
+ def get_raw_list(username, target)
+ options = {:count => 200, :before_id => nil}
+ big = []
+ loop do
+ url = get_list_url(username, target, options)
+ resp = get_parsed_response(url)
+ big << resp
+ break if resp['meta']['min_id'] == nil || resp['meta']['more'] == false
+ options = {:count => 200, :before_id => resp['meta']['min_id']}
+ end
+ big
+ end
+
+ def get_user(username)
+ get_parsed_response(Endpoints.new.user(username))
+ end
+
+ def get_details(post_id, options = {})
+ get_parsed_response(Endpoints.new.single_post(post_id, options))
+ end
+
+ def get_files_list(options)
+ array_of_hashes = []
+ unless options[:all]
+ resp = get_parsed_response(Endpoints.new.files_list(options))
+ resp['data'].each { |p| array_of_hashes << p }
+ else
+ options = {:count => 200, :before_id => nil}
+ loop do
+ resp = get_parsed_response(Endpoints.new.files_list(options))
+ resp['data'].each { |p| array_of_hashes << p }
+ break unless resp['meta']['more']
+ options = {:count => 200, :before_id => resp['meta']['min_id']}
+ end
+ end
+ array_of_hashes
+ end
+
+ def get_file(file_id)
+ get_parsed_response(Endpoints.new.file(file_id))
+ end
+
+ def star(post_id)
+ JSON.parse(CNX.post(Endpoints.new.star(post_id)))
+ end
+
+ def follow(post_id)
+ JSON.parse(CNX.post(Endpoints.new.follow(post_id)))
+ end
+
+ def mute(post_id)
+ JSON.parse(CNX.post(Endpoints.new.mute(post_id)))
+ end
+
+ def block(username)
+ JSON.parse(CNX.post(Endpoints.new.block(username)))
+ end
+
+ def repost(post_id)
+ JSON.parse(CNX.post(Endpoints.new.repost(post_id)))
+ end
+
+ def delete_post(post_id)
+ JSON.parse(CNX.delete(Endpoints.new.delete_post(post_id)))
+ end
+
+ def unstar(post_id)
+ JSON.parse(CNX.delete(Endpoints.new.star(post_id)))
+ end
+
+ def unfollow(username)
+ JSON.parse(CNX.delete(Endpoints.new.follow(username)))
+ end
+
+ def unmute(username)
+ JSON.parse(CNX.delete(Endpoints.new.mute(username)))
+ end
+
+ def unblock(username)
+ JSON.parse(CNX.delete(Endpoints.new.block(username)))
+ end
+
+ def unrepost(post_id)
+ resp = JSON.parse(CNX.delete(Endpoints.new.repost(post_id)))
+ if resp['data']['repost_of']
+ JSON.parse(CNX.delete(Endpoints.new.repost(resp['data']['repost_of']['id'])))
+ else
+ resp
+ end
+ end
+
+ def get_channels
+ options = {:count => 200, :recent_message => 1, :annotations => 1, :before_id => nil}
+ get_parsed_response(Endpoints.new.channels(options))
+ # big = []
+ # loop do
+ # resp = get_parsed_response(Endpoints.new.channels(options))
+ # #check_response_meta_code(resp)
+ # big << resp
+ # break if resp['meta']['more'] == false
+ # options = {:count => 200, :before_id => resp['meta']['min_id']}
+ # end
+ # big
+ end
+
+ def get_messages(channel_id, options)
+ if options[:new] || options[:scroll]
+ options = {since_id: Databases.pagination["channel:#{channel_id}"]}
+ end
+ get_parsed_response(Endpoints.new.messages(channel_id, options))
+ end
+
+ def get_config
+ get_parsed_response(Endpoints.new.config_api_url)
+ end
+
+ def check_response_meta_code(res)
+ if res['meta']['code'] == 200
+ res
+ else
+ Errors.global_error("api/check_response_meta_code", nil, res['meta'])
+ end
+ end
+
+ def self.build_query(arg)
+ count = Settings.options[:counts][:default]
+ if arg[:count]
+ if arg[:count].to_s.is_integer?
+ count = arg[:count]
+ end
+ end
+ directed = Settings.options[:timeline][:directed]
+ if arg[:directed]
+ if arg[:directed] == 0 || arg[:directed] == 1
+ directed = arg[:directed]
+ end
+ end
+ deleted = Settings.options[:timeline][:deleted]
+ if arg[:deleted]
+ if arg[:deleted] == 0 || arg[:deleted] == 1
+ deleted = arg[:deleted]
+ end
+ end
+ html = Settings.options[:timeline][:html]
+ if arg[:html]
+ if arg[:html] == 0 || arg[:html] == 1
+ html = arg[:html]
+ end
+ end
+ annotations = Settings.options[:timeline][:annotations]
+ if arg[:annotations]
+ if arg[:annotations] == 0 || arg[:annotations] == 1
+ annotations = arg[:annotations]
+ end
+ end
+ if arg[:since_id]
+ "&count=#{count}&include_html=#{html}&include_directed=#{directed}&include_deleted=#{deleted}&include_annotations=#{annotations}&since_id=#{arg[:since_id]}"
+ elsif arg[:recent_message]
+ "&count=#{count}&include_html=#{html}&include_directed=#{directed}&include_deleted=#{deleted}&include_annotations=#{annotations}&include_recent_message=#{arg[:recent_message]}"
+ else
+ "&count=#{count}&include_html=#{html}&include_directed=#{directed}&include_deleted=#{deleted}&include_annotations=#{annotations}"
+ end
+ end
+
+ private
+
+ def get_parsed_response(url)
+ JSON.parse(CNX.get_response_from(url))
+ end
+
+ def get_original_if_repost(resp)
+ if resp['repost_of']
+ resp['repost_of']
+ else
+ resp
+ end
+ end
+
+ def build_list(username, target)
+ options = {:count => 200, :before_id => nil}
+ big_hash = {}
+ loop do
+ url = get_list_url(username, target, options)
+ resp = get_parsed_response(url)
+ big_hash.merge!(Workers.extract_users(resp))
+ break if resp['meta']['min_id'] == nil
+ options = {:count => 200, :before_id => resp['meta']['min_id']}
+ end
+ big_hash
+ end
+
+ def get_list_url(username, target, options)
+ case target
+ when :followings
+ Endpoints.new.followings(username, options)
+ when :followers
+ Endpoints.new.followers(username, options)
+ when :muted
+ Endpoints.new.muted(options)
+ when :blocked
+ Endpoints.new.blocked(options)
+ end
+ end
+
+ end
+end