-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstatomd.rb
More file actions
111 lines (95 loc) · 3.52 KB
/
instatomd.rb
File metadata and controls
111 lines (95 loc) · 3.52 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
# nethttp2.rb
require 'uri'
require 'net/http'
require 'json'
require "open-uri"
require './env'
app_id = ENV['app_id']
app_secret = ENV['app_secret']
puts("https://api.instagram.com/oauth/authorize?client_id=#{app_id}&redirect_uri=https://al.astudillo.com/&scope=user_profile,user_media&response_type=code")
puts("Pega el código obtenido en el URL al que redirige la autenticación:")
code = gets
code = code.chomp
client_secret = "#{app_secret}"
client_secret = client_secret.chomp
#Exchange code for token
uri = URI('https://api.instagram.com/oauth/access_token')
res = Net::HTTP.post_form(uri, 'client_id' => "#{app_id}", 'client_secret' => client_secret, 'grant_type' => 'authorization_code',
'redirect_uri' => 'https://al.astudillo.com/', 'code' => code)
if res.is_a?(Net::HTTPSuccess)
token_hash = JSON.parse(res.body)
else
puts("Ha habido un error generando el token de acceso")
puts(res.body)
exit
end
#puts("acces_token = #{token_hash['access_token']} ")
#puts("user_id = #{token_hash['user_id']}")
uri = URI('https://graph.instagram.com/me/media')
params = { :fields => 'media_type,media_url,children{media_url,media_type},caption,timestamp', :access_token => token_hash['access_token'] }
uri.query = URI.encode_www_form(params)
res = Net::HTTP.get_response(uri)
if res.is_a?(Net::HTTPSuccess)
posts_data = JSON.parse(res.body)
else
puts("Ha habido un error obteniendo los posts")
exit
end
post_list = posts_data['data']
puts(res.body)
i = 0
for post in post_list
multi = false
puts "POST #{i}: " + "\n\n"
desc = post['caption']
date = post['timestamp']
date = date.split('T')[0].strip
desc = desc.split("°°°")
if desc.size() == 1
desc = desc[0].split("ººº")
end
title = desc[0].strip.gsub(" ", "-")
puts "TITLE: #{title}"
unless desc[1].nil?
content = desc[1].strip
else
content = ""
end
puts "date: #{date} "
single_media = post['media_url']
children = post['children']
file_name = "posts/#{date}-#{title}.md"
title = title.gsub("-", " ")
File.write(file_name,
"---\n" +
"layout: post\n" +
"title: #{title}\n" +
"---\n")
image_name = "#{date}-#{title}.jpg".gsub(" ", "-")
unless(children.nil?)
File.write(file_name, "<link rel=\"stylesheet\" href=\"/style.css\">", mode: 'a')
File.write(file_name,"<div class=\"slider\">", mode: 'a');
child_data = children['data']
j = 0
for child in child_data
image_name = "#{date}-#{title}_#{j}.jpg".gsub(" ", "-")
File.write(file_name,"<div class=\"slide\">\n<img src=\"/images/#{image_name}\" alt=\"\" /> </div> ", mode: 'a');
#File.write(file_name, "", mode: 'a')
File.open("images/#{image_name}", 'wb') do |fo|
fo.write URI.open("#{child['media_url']}").read
end
j+=1
end
File.write(file_name, "<button class=\"btn btn-next\">></button> <button class=\"btn btn-prev\"><</button>", mode: 'a')
File.write(file_name, "<script src=\"/script.js\"></script>", mode: 'a')
File.write(file_name, "</div>", mode: 'a')
else
File.write(file_name, "", mode: 'a')
File.open("images/#{image_name}", 'wb') do |fo|
fo.write URI.open("#{single_media}").read
end
end
File.write(file_name, content, mode: 'a')
i = i + 1
puts "\n\n"
end