Created
October 31, 2016 17:25
-
-
Save DanBradbury/2434bbc9d5fb1391e03da7543a49ca39 to your computer and use it in GitHub Desktop.
Autoemoji
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'uri' | |
require 'net/https' | |
require 'nokogiri' | |
if ARGV.size != 1 | |
puts "Usage: ruby googleFetch.rb SEARCH_VALUE" | |
exit | |
end | |
search_value = ARGV[0] | |
ARGV.clear # needed for gets | |
uri = URI.parse("https://www.google.com/search?q=#{search_value}&tbm=isch&tbs=isch&tbs=isz:ex,iszw:128,iszh:128") | |
headers = { | |
'Host': 'www.google.com', | |
'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0', | |
'Cookie': 'NID=89=NAM3LulJEeAgroZkD6nEr0D1nwsaalj5P7dOHunXvTEcc0gTXB_yD7KVJGdUrC0-rPabe3t_P9Cov1mGN89hi2sdboggg4gJ0JYJoFUSclxcxkN-BD-6duR6V-qv00mlwc5WGPS2aMwBAr0DgaQ6; OGPC=5061451-8:5061821-4:; OGP=-5061451:-5061821:; DV=YnF1F8Qz6bEYLEWZLaMIXfQ_3SgvsAI', | |
} | |
http = Net::HTTP.new(uri.host, uri.port) | |
http.use_ssl = true | |
http.verify_mode = OpenSSL::SSL::VERIFY_NONE | |
request = Net::HTTP::Get.new(uri.request_uri, headers) | |
response = http.request(request) | |
doc = Nokogiri::HTML::Document.parse(response.body) | |
f = File.open("#{__dir__}/check_image.html", 'w') | |
f << '<html><body>' | |
f << '<script>function postMe() {//do XMLHttpRequest post here}</script>' | |
doc.xpath('//img').each_with_index do |img_tag, index| | |
filename = "#{__dir__}/img_tmp/#{index}" | |
tmp = File.open(filename, 'w') | |
tmp.write(Net::HTTP.get(URI.parse(img_tag.attributes['src'].value))) | |
tmp.close | |
f << "#{index}<img src='#{filename}'><br>" | |
# method to add all images with an onclick handler | |
#f << img_tag.to_s.gsub('>', ' onclick="test()">') | |
end | |
f << '<form></form>' | |
f << '</body></html>' | |
f.close | |
puts "Opening check_image.html.." | |
system("open #{__dir__}/check_image.html") | |
puts "Enter image number you want to upload" | |
img_num = gets | |
puts "Running python upload script" | |
system("python3 #{__dir__}/slackUpload.py --file #{__dir__}/img_tmp/#{img_num.chomp} --alias #{search_value}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import os | |
import sys | |
import argparse | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--file', required=True) | |
parser.add_argument('--alias', required=True) | |
args = parser.parse_args() | |
file_name = args.file | |
emoji_name = args.alias | |
def fetch_crumb(request_text): | |
crumb_pre = request_text.find('name="crumb" value="')+ 20 #len of find string | |
partial = request_text[crumb_pre:] | |
end_id = partial.find('"') | |
crumb = partial[:end_id] | |
ss = crumb.split('-') | |
ss.pop(-1) | |
fixed_crumb = '-'.join(ss) + '-☃' | |
return fixed_crumb | |
s = requests.Session() | |
get_request = s.get('https://yardclub.slack.com/') | |
data = { | |
'signin': 1, | |
'redir': '', | |
'crumb': fetch_crumb(get_request.text), | |
'email': os.environ['SLACK_USERNAME'], | |
'password': os.environ['SLACK_PASSWORD'], | |
'remember': 'on' | |
} | |
response = s.post('https://yardclub.slack.com/', verify=True, data=data) | |
if(response.text.find('Sign in') == '-1'): | |
print('try again') | |
exit() | |
else: | |
print('Successfully signed into slack') | |
customize_page = s.get('https://yardclub.slack.com/customize/emoji') | |
post_data = { | |
'add': '1', | |
'crumb': fetch_crumb(customize_page.text), | |
'name': emoji_name, | |
'mode': 'data' | |
} | |
files = {'img': open(file_name, 'rb')} | |
image_upload = s.post('https://yardclub.slack.com/customize/emoji', data=post_data, files=files) | |
if(image_upload.status_code == 200): | |
print('Upload successful.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Setup:
SLACK_USERNAME
andSLACK_PASSWORD
envs to make upload workUsage:
Run
googleFetch ALIAS_NAME