forked from mislav/instagram
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.rb
More file actions
138 lines (111 loc) · 3.58 KB
/
Copy pathmodels.rb
File metadata and controls
138 lines (111 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require 'mingo'
require 'active_support/memoizable'
require 'indextank'
require 'will_paginate/finders/base'
require 'hashie/mash'
require 'net/http'
class User < Mingo
property :user_id
property :username
property :twitter
property :twitter_id
extend ActiveSupport::Memoizable
class NotFound < RuntimeError; end
def self.lookup(id)
if id =~ /\D/
first(username: id) or raise NotFound
else
self[id]
end
end
def self.[](id)
(first(user_id: id.to_i) || new(user_id: id.to_i)).tap do |user|
unless user.username
user.username = user.instagram_info.username
user.save
end
end
end
def self.find_by_instagram_url(url)
id = Instagram::Discovery.discover_user_id(url)
self[id] if id
end
def instagram_info
Instagram::user(self.user_id)
end
memoize :instagram_info
def photos(max_id = nil, raw = false)
params = { count: 20 }
params[:max_id] = max_id.to_s if max_id
params[:raw] = raw if raw
Instagram::user_recent_media(self.user_id, params)
end
end
module Instagram
module Discovery
def self.discover_user_id(url)
url = Addressable::URI.parse url unless url.respond_to? :host
$1.to_i if get_url(url) =~ %r{profiles/profile_(\d+)_}
end
PermalinkRe = %r{http://instagr\.am/p/[/\w-]+}
TwitterSearch = Addressable::URI.parse 'http://search.twitter.com/search.json'
TwitterTimeline = Addressable::URI.parse 'http://api.twitter.com/1/statuses/user_timeline.json'
def self.search_twitter(username)
detect = Proc.new { |tweet| return [$&, tweet['id']] if tweet['text'] =~ PermalinkRe }
url = TwitterSearch.dup
url.query_values = { q: "from:#{username} instagr.am" }
data = JSON.parse get_url(url)
data['results'].each(&detect)
url = TwitterTimeline.dup
url.query_values = { screen_name: username, count: "200", trim_user: '1' }
data = JSON.parse get_url(url)
data.each(&detect)
return nil
end
def self.get_url(url)
Net::HTTP.get(url)
end
end
end
# mimics Instagram::Media
class IndexedPhoto < Struct.new(:id, :caption, :thumbnail_url, :large_url, :username, :taken_at, :filter_name)
Fields = 'text,thumbnail_url,username,timestamp,big,filter'
class << self
include WillPaginate::Finders::Base
protected
def wp_query(query_options, pager, args, &block)
query = args.first
filter = query_options.delete(:filter)
query = "#{query} AND filter:#{filter}" if filter
params = {:len => pager.per_page, :start => pager.offset, :fetch => Fields}.update(query_options)
data = ActiveSupport::Notifications.instrument('search.indextank', {:query => query}.update(params)) do
search_index.search(query, params)
end
pager.total_entries = data['matches']
pager.replace data['results'].map { |item| new item }
end
def search_index
Sinatra::Application.settings.search_index
end
end
self.per_page = 32
def initialize(hash)
super hash['docid'], hash['text'], hash['thumbnail_url'], hash['big'],
hash['username'], Time.at(hash['timestamp'].to_i), hash['filter']
end
User = Struct.new(:id, :full_name, :username)
Caption = Struct.new(:text)
def user
@user ||= User.new(nil, nil, username)
end
def caption
if text = super
@caption ||= Caption.new(text)
end
end
def images
@images ||= Hashie::Mash.new \
thumbnail: { url: thumbnail_url, width: 150, height: 150 },
standard_resolution: { url: large_url, width: 612, height: 612 }
end
end