Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
emad-elsaid / text-generator.rb
Created March 8, 2014 11:38
generate random string from a canonical for, useful for software responding like humans
#!/usr/bin/env ruby
# generate a single sentence from a canonical form
# canonical sentence is a multi sentences combined in one
# form, generator will generate a sentence from it randomly
# based on the form, for example:
# "Hello [Emad|Elsaid]" , may generate "Hello Emad" or
# "Hello Elsaid" the result is random.
# also you could nest [] inside each other to gain a multi level
# canonical sentence example:
# "[[Hi|Hello] [Emad|elsaid] | good [morning|night] sir]"
@emad-elsaid
emad-elsaid / twitterbot.rb
Created February 23, 2014 10:41
twitter bot to auto favourite tweets
require 'Twitter' #gem install twitter
while true
begin
# Create a read write application from :
# https://apps.twitter.com
# authenticate it for your account
# fill in the following
config = {
consumer_key: '',
consumer_secret: '',
@emad-elsaid
emad-elsaid / pdf2txt.rb
Created March 23, 2014 13:06
PDF to Text converter using ruby
#!/usr/bin/env ruby
require 'pdf/reader' # gem install pdf-reader
# credits to :
# https://github.com/yob/pdf-reader/blob/master/examples/text.rb
# usage example:
# ruby pdf2txt.rb /path-to-file/file1.pdf [/path-to-file/file2.pdf..]
ARGV.each do |filename|
PDF::Reader.open(filename) do |reader|
@emad-elsaid
emad-elsaid / ask-wikipedia.rb
Created February 25, 2014 10:26
ask wikipedia from command line using ruby
require 'open-uri'
require 'json'
language = 'en'
print 'What do you need to know? : '
article = URI::encode gets.chomp
request_url = "http://#{language}.wikipedia.org/w/api.php?action=parse&page=#{article}&format=json&prop=text&section=0"
@emad-elsaid
emad-elsaid / facebook-friends-to-json.rb
Created March 18, 2014 16:01
get facebook friends data and save them as json file
#!/usr/bin/env ruby
require 'koala' # gem install koala --no-document
require 'json'
# create a facebook app and get access token from here
# https://developers.facebook.com/tools/explorer
# select "user_friends" when authenticating
oauth_access_token = 'xxxxxxxxxxxxxxxx'
graph = Koala::Facebook::API.new(oauth_access_token)
fields = 'bio,name,birthday,first_name,last_name,gender,hometown,relationship_status,username,website,languages'
@emad-elsaid
emad-elsaid / share-screen.rb
Created February 22, 2014 10:30
share your screen on the local network
require 'socket'
require 'base64'
Refresh = 1 # seconds to refresh image on server
screen_capture_command = 'screencapture -C -x tmp.png'
image = ''
latest = Time.now
server = TCPServer.new 3000
loop do
@emad-elsaid
emad-elsaid / markdown.rb
Created March 9, 2014 11:35
Everytime i write markdown and commit i find error, so i solved the problem ;)
#!/usr/bin/env ruby
# gem install sinatra --no-document
# gem install github-markdown --no-document
require 'sinatra'
require 'github/markdown'
set :port, 3000
get '/' do
<<-EOT
<!DOCTYPE html>
@emad-elsaid
emad-elsaid / age.rb
Created February 24, 2014 10:54
age in seconds, minutes, hours, days and auto update
require 'time'
Date_of_birth = '1988-8-31'
# Credit to : http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails
# and modified a little bit
def humanize secs
[
[60, :seconds],
[60, :minutes],
@emad-elsaid
emad-elsaid / ago.go
Last active June 19, 2022 23:02
Converts a time duration to string. for example: 38 minutes 53 seconds ago OR 2 days 58 minutes ago. maxPrecision is the max number of time components in the result.
func ago(t time.Duration) (o string) {
const day = time.Hour * 24
const week = day * 7
const month = day * 30
const year = day * 365
const maxPrecision = 2
if t.Seconds() < 1 {
return "seconds ago"
}
@emad-elsaid
emad-elsaid / working-time.rb
Created March 31, 2014 13:02
Track your computer open/shutdown times this is a very simple script, i created to track when did i open and closes my computer, on the long run, i can read these data and analyse it to see detect my usage patterns and how to improve my computer usage for a better work/life balance. this script uses simple text file as log file, it write the eve…
#!/usr/bin/env ruby
# Author : Emad Elsaid (https://github.com/blazeeboy)
FILENAME = 'db.txt'
OPEN, CLOSE = 1 , 0
file = File.open(FILENAME, 'a')
file.write "#{OPEN} : #{Time.new}\n"
file.flush
begin