-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rakefile
198 lines (163 loc) · 5.05 KB
/
Rakefile
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
POST_DIR = "_posts"
desc 'create new post'
# rake new My New Post
task :new do
require 'rubygems'
require 'chronic'
ARGV.each { |a| task a.to_sym do ; end }
title = ARGV[1..-1].join(' ') || "New Title"
slug = title.gsub(' ','-').downcase
filename = "#{Time.new.strftime('%Y-%m-%d')}-#{slug}.md"
permalink = "/#{Time.new.strftime('%Y/%m')}/#{slug}/"
path = File.join(POST_DIR, filename)
post = <<-HTML
---
title: "TITLE"
author: Greg
layout: post
permalink: PERMALINK
date: DATE
comments: True
licence: Creative Commons
categories:
- category
tags:
- tag
---
HTML
post \
.gsub!('TITLE', title) \
.gsub!('DATE', Time.new.to_s) \
.gsub!('PERMALINK', permalink)
File.open(path, 'w') do |file|
file.puts post
end
puts "new post generated in #{path}"
system "code #{path}"
end
desc 'create YouTube channel video posts'
task :ytvids do
require 'yaml'
require 'yt'
creds = YAML.load_file('_creds.yml')
api_key = creds["google"]["api_key"]
Yt.configure do |config|
config.api_key = api_key
end
playlist = Yt::Playlist.new id: 'PL-3STrNltv3dVQBJPtpfRxdF3oKZeoIvp'
puts playlist.title
playlist.playlist_items.each do |video|
filename = (video.published_at.strftime("%Y-%m-%d") + " " + video.title.downcase).parameterize + ".md"
permalink = "/" + video.published_at.strftime("%Y/%m") + "/" + video.title.downcase.parameterize
path = File.join(POST_DIR, filename)
unless File.exist?(path)
post = <<-HTML
---
title: "TITLE"
author: Greg
layout: post
permalink: PERMALINK
published_at: DATE
comments: True
licence: Creative Commons
categories:
- YouTube
tags:
- sailing
---
{% include youtube_player.html id='VIDEO_ID' %}
DESCRIPTION
HTML
post \
.gsub!('TITLE', video.title) \
.gsub!('DATE', video.published_at.to_s) \
.gsub!('PERMALINK', permalink) \
.gsub!('VIDEO_ID', video.video_id) \
.gsub!('DESCRIPTION', video.description)
File.open(path, 'w') do |file|
file.puts post
end
puts "new post generated in #{path}"
end
end
end
desc 'collect tweets'
task :tweets do
require 'yaml'
require 'twitter'
creds = YAML.load_file('_creds.yml')
client = Twitter::REST::Client.new do |config|
config.consumer_key = creds["twitter"]["api_key"]
config.consumer_secret = creds["twitter"]["api_secret_key"]
config.access_token = creds["twitter"]["access_token"]
config.access_token_secret = creds["twitter"]["access_token_secret"]
end
twitter_accounts = ['gregologynet', 'memairapp', 'smileyom', 'svcatsaway', 'wikijam', 'ghostisp']
tweets = []
twitter_accounts.each do |twitter_account|
puts "collecting tweets for #{twitter_account}"
max_id = nil
max_tries = 20
max_tries.times do
new_tweets = max_id.nil? ? client.user_timeline(twitter_account, count: 200, include_rts: true, tweet_mode: 'extended') : client.user_timeline(twitter_account, count: 200, include_rts: true, tweet_mode: 'extended', max_id: max_id)
break if new_tweets.empty?
puts "#{new_tweets.count} tweets extracted from #{new_tweets.last.created_at} to #{new_tweets.first.created_at}"
tweets += new_tweets
max_id = new_tweets[-1].id - 1
end
end
puts "collected #{tweets.count} tweets"
def clean_tweet(raw_tweet)
tweet = raw_tweet.to_hash
tweet[:user] = tweet[:user].slice(:id,:name)
tweet
end
tweets = tweets.sort_by!(&:created_at)
tweets = tweets.map { |tweet| clean_tweet(tweet) }
File.open('api/tweets.json', 'w+') do |file|
file.puts tweets.to_json
end
end
desc 'collect Wikipedia edits'
task :wikipedia do
require 'json'
require 'net/http'
require 'active_support/core_ext/hash'
edits = []
years = (2005..Time.new.year).to_a
years.each do | year |
puts "collecting wikipedia edits from #{year}"
url = "https://en.wikipedia.org/w/api.php?action=feedcontributions&user=gregology&year=#{year}"
s = Net::HTTP.get_response(URI.parse(url)).body
new_edits = Hash.from_xml(s)
edits += new_edits['rss']['channel']['item']
end
edits = edits.uniq
puts "#{edits.count} edits collected"
edits = edits.each {|edit| edit['pubDate'] = edit['pubDate'].to_datetime.to_s }
edits = edits.sort_by { |edit| edit['pubDate'] }
File.open('api/wikipedia.json', 'w+') do |file|
file.puts edits.to_json
end
end
desc 'collect Reddit posts'
task :reddit do
require 'json'
require 'open-uri'
useragent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36'
posts = []
after = nil
max_tries = 50
max_tries.times do
url = "https://www.reddit.com/user/gregologynet.json" + (after.nil? ? '' : "?after=#{after}")
response = open(url, 'User-Agent' => useragent).read
new_posts = JSON.parse(response)
puts "collected #{new_posts['data']['children'].count} posts from #{url}"
posts += new_posts['data']['children']
after = new_posts['data']['after']
break unless after
end
File.open('api/reddit.json', 'w+') do |file|
file.puts posts.to_json
end
end