Categories
All

INSTALL AND RUN FLASK(PYTHON) ON UBUNTU 19.04 on VpsServer

Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications. It began as a simple wrapper around Werkzeug and Jinja and has become one of the most popular Python web application frameworks.


Step #0:
Signup (I appreciate if you use my referral link)
https://www.vpsserver.com/?affcode=21edd2c7bade
after your account becomes verified, create a Ubuntu 19.04 server.

Step #1:
Install python pip and flask by run:
$ sudo apt install python-pip
$ sudo apt install python3-pip
$ pip install -U Flask
$ pip3 install -U Flask

Step #2:
Add your project files on the server (ex. from GitHub using clone command)
in this tutorial let’s use a very simple flask app example that prints Hello, World! and save the app in (ex. /var/www/flask):

$ mkdir /var/www/flask
$ cd /var/www/flask
$ nano app.py
copy below code and paste it in app.py file using CTRL + SHIFT + V keys.

# app.py file

from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello, World!"

then press ( CTRL+ X )-> Y -> ENTER.


Step #3:
Run the server by run:
$ env FLASK_APP=app.py flask run -h xxx.xxx.xxx
change xxx.xxx.xxx to your server ip

output:

Serving Flask app "app.py"
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://xxx.xxx.xxx:5000/ (Press CTRL+C to quit)
Categories
All

Video Generator

Video Generator is a python tool that allows converting a collection of photos from local and online sources to video.

Preview


Reflection

While I was working on some computer vision task related to extracting some information from public surveillance cameras streams, in some stage I wanted to record some videos from a collection of photos URLs so I decided to create this simple script to help with this stage and share it in public as well.

Usage

Install
pip install -r requirements.txt

set source in main.py file

Sources:
- online photos urls        ex. 'http://78.186.19.184:81/record/current.jpg'
- local photos directory    ex. 'local_samples/'

videoGenerator = VideoGenerator(SOURCE_HERE)

Run

python main.py

on GitHub: https://github.com/khaledalam/video-generator

Categories
All

API Multiprocessing

Motivation

In this article, I am going to show you how we can improve the computing power of simple API script from total overall (6 minutes and 17 seconds) to (1 minute 14 seconds)

The Idea

I will share with you one of my simple favourite technique that I prefer to use especially when I work on data science tasks such as data visualization, data analysis, code optimization, and big data processing.

Processing a task in a sequential way may take a long time especially when we are talking about a huge amount of data(eg. big inputs)

This technique takes advantage of parallelization capabilities in order to reduce the processing time.

The idea is to divide the data into chunks so that each engine takes care of classifying the entries in their corresponding chunks. Once performed, each engine reads, writes and processes its chunks, each chunk be processed in the same amount of time.

Example

The example I choose to use for this article is Genderize names that consist of 2 alphabetic characters.

Output Analysis Chart

Explanation

Clone GitHub Repo and follow instructions in Usage section.

Let’s generate all alphabet names that consist of 2 characters(to make the testing process easy)

we can use some Linux Kali penetration testing tool[1] such as crunch
$ crunch 2 2 > names.txt
so we generate all possible alphabet names with length 2 (676 lines)

then let’s create directories which are needed for splitting process
$ mkdir subs/ subs/inputs subs/outputs subs/outputs/parts subs/outputs/all

now we can split out input data, there are many ways to do that but I prefer to use Unix split command [2]
$ split -l 100 -d names.txt ./subs/inputs/
so we split names.txt file into small files, each file consists of 100 lines

now let’s run all processes: ./init.bash
after finish use merger.py script to merge all outputs.
merging process separated to avoid conflicts behaviours and sorting-save.


The Project on GitHub:
https://github.com/khaledalam/api-multiprocessing

An application uses this technique:
Hiring-Related Email(https://github.com/khaledalam/amazon-jobs)

Interesting related ideas:
Parallelizing using GPUs
– MapReduce (https://en.wikipedia.org/wiki/MapReduce)

[1] https://tools.kali.org/password-attacks/crunch
[2] https://en.wikipedia.org/wiki/Split_(Unix)

Categories
All

Python Instagram Followers Scraper





import sys
# import instaloader
from include import *


'''
username = 'INSTAGRAM_USERNAME_HERE'
password = 'INSTAGRAM_PASSWORD_HERE'


username = ''
password = ''


L = instaloader.Instaloader()
L.login(insta_username, insta_password)

profile = instaloader.Profile.from_username(L.context, 'areyoukhaled')

print ('you have ' + str(len(profile.get_followers())))

for post in profile.get_followers():
    print(post)

# followers = set(profile.get_followers())
# print('Fetching followers of profile: {} ' + len(followers) + ' .'.format(profile.username))
#
# print(followers)

print('Storing Followers into file.')
with open( PROFILE + ' (followers).txt', 'w+') as f:
 cnt = 1
 for follower in followers:
 print(follower.username, file=f)
 sys.stdout.write("%d of %d\r" %( cnt, len(followers)) )
 sys.stdout.flush()
 cnt+=1
'''
Categories
All

Time Tracking

python working time toggl tracking

http://www.online-stopwatch.com/cash-clock/

import sys
import datetime
import keyboard 
import time

timeInSeconds = 0
tillNowTime = 0
startedTime = str(datetime.datetime.now())
rate = 1.2
eq = 0

while True:
	if keyboard.is_pressed("esc"):
		break

	tillNowTime = str(datetime.timedelta(seconds = timeInSeconds))
	eq += rate
	sys.stdout.write("Started at: %s  |  Till now: %s , %s[X]\r" %( startedTime, tillNowTime, str(eq)) )
	sys.stdout.flush()
	timeInSeconds += 1
	
	time.sleep(1)	
	
print("\n--------\nEnded at: " + str(datetime.datetime.now()) + " | Total: " + str(tillNowTime) )
Categories
All

PyShifterPro

Python and prolog encryption project based on “caesar cipher” encryption technique.

How to use:

  • in terminal: python PrologGenerator.py :> prologCode.pl
  • run prolog code via terminal(swipl prologCode.pl) or gui then use:
    • shift(a, X).
    • hideChar(a).
    • showChar(a).
    • guessChar(a).
    • hideString(test).
    • showString(nkdohg).
    • guessString(abc).
    • readFile.
    • dfs(a, X, z). continue pressing ;

for windows users: download and use python and prolog gui.

Examples:

#Ex.1 walk start from a with key = 3

?- walk(a,X,z).
X = d ;
X = g ;
X = j ;
X = m ;
X = p ;
X = s ;
X = v ;
X = y ;
X = b ;
X = e ;
X = h ;
X = k ;
X = n ;
X = q ;
X = t ;
X = w ;
X = z ;
X = c ;
false.

#Ex.2 encrypt string met3cs with key = 3

?- hideString(met3cs).
phw6fv
true.

#Ex.3 decrypt string phw6fv with key = 3

?- showString(phw6fv).
met3cs
true.

#Ex.4 encrypt char z with key = 3

?- hideChar(z).
c
true.

Files Structure:

examples
   \ input.txt
   \ prologCode.pl
PrologGenerator.py
input.txt
prologCode.pl

——-

prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

input.txt

khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.

PrologGenerator.py

# Encryption and Decryption Project using Prolog and Python
# \_based on "Caesar cipher" encryption technique..
#
# Author: Khaled Alam


import string



# START Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>

shiftingKey = 3      # << 3 default(Caesar cipher)
factName = 'shift'
bothPredicateName = 'guess'
encryptPredicateName = 'hide'
decryptPredicateName = 'show'


factLower = True
factUpper = False  # not handled
factDigits = True

inputFileName = 'input.txt'


# END Configuration(changeable): >>>>>>>>>>>>>>>>>>>>>>>>>>>>>



# START BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

#lower chars:
if factLower is True:
	for char in string.ascii_lowercase:
		if ord(char) + shiftingKey > ord('z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'

#upper chars:
if factUpper is True:
	for char in string.ascii_uppercase:
		if ord(char) + shiftingKey > ord('Z'):
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey - 26) + ').'
		else:
		    print factName + '(' + char + ', ' + chr(ord(char) + shiftingKey) + ').'
#digits:
if factDigits is True:
	for digit in range(10):
	    print factName + '(' + str(digit) + ', ' + str((digit + shiftingKey) % 9) + ').'


print('shift(., *).\nshift(*,.).')


# END BUILD THE FACTS BASE.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')


# START BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


# >>> 1 CHAR/DIGIT <<<<<<<<<<<<<<<<<<

#encrypt 1 char or digit:
print(encryptPredicateName + 'Char(X):- ' + factName + '(X,Y), write(Y).')

#decrypt 1 char or digit:
print(decryptPredicateName + 'Char(X):- ' + factName + '(Y,X), write(Y).')

#print both guesses of 1 char or digit:
print(bothPredicateName + 'Char(X):- shift(X,Y), shift(Z,X), write(\"maybe: \"), write(Y), '+
'write(\"\\nOR\\nmaybe: \"), write(Z), write(\'\\n=========\\n\').')


print('\n')


# >>>    STRINGS   <<<<<<<<<<<<<<<<<<

print('numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).')
print('numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).')

#encrypt string:
print(encryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).')

#decrypt string:
print(decryptPredicateName + 'String(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).')

#print both guesses of string:
print(bothPredicateName + 'String(String) :- write(\"maybe: \"), hideString(String), write(\"\\nOR\\nmaybe: \"),'+
' showString(String), write(\'\\n=========\\n\').')

# END BUILD BASE FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')



# START BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


#Read from file
print('readFile :-')
print('open(\'' + inputFileName + '\', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.')
print('read_file(Stream,[]) :- at_end_of_stream(Stream).')
print('read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(\' : \'),'+
' write(\'\\n\'), ' + bothPredicateName + 'String(X), write(\'\\n\'), read_file(Stream,L).')

print('\n')

print('walk(X, Z, _ORG):- shift(X,Z).\nwalk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).')


# END BUILD EXTRA FUNCTIONALITY.. >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>


print('\n')

examples/prologCode.pl

shift(a, d).
shift(b, e).
shift(c, f).
shift(d, g).
shift(e, h).
shift(f, i).
shift(g, j).
shift(h, k).
shift(i, l).
shift(j, m).
shift(k, n).
shift(l, o).
shift(m, p).
shift(n, q).
shift(o, r).
shift(p, s).
shift(q, t).
shift(r, u).
shift(s, v).
shift(t, w).
shift(u, x).
shift(v, y).
shift(w, z).
shift(x, a).
shift(y, b).
shift(z, c).
shift(0, 3).
shift(1, 4).
shift(2, 5).
shift(3, 6).
shift(4, 7).
shift(5, 8).
shift(6, 0).
shift(7, 1).
shift(8, 2).
shift(9, 3).
shift(., *).
shift(*,.).


hideChar(X):- shift(X,Y), write(Y).
showChar(X):- shift(Y,X), write(Y).
guessChar(X):- shift(X,Y), shift(Z,X), write("maybe: "), write(Y), write("\nOR\nmaybe: "), write(Z), write('\n=========\n').


numToCharEnc(Number, Character) :- name(Character, [Number]), hideChar(Character).
numToCharDec(Number, Character) :- name(Character, [Number]), showChar(Character).
hideString(String) :- name(String, Xs), maplist( numToCharEnc, Xs, _Characters ).
showString(String) :- name(String, Xs), maplist( numToCharDec, Xs, _Characters ).
guessString(String) :- write("maybe: "), hideString(String), write("\nOR\nmaybe: "), showString(String), write('\n=========\n').


readFile :-
open('input.txt', read, Str), read_file(Str,Lines), close(Str), write(Lines), nl.
read_file(Stream,[]) :- at_end_of_stream(Stream).
read_file(Stream,[X|L]) :- \+ at_end_of_stream(Stream), read(Stream,X), write(X), write(' : '), write('\n'), guessString(X), write('\n'), read_file(Stream,L).


walk(X, Z, _ORG):- shift(X,Z).
walk(X, Z, _ORG):- _ORG \= X -> shift(X,Y), walk(Y, Z, _ORG).

examples/input.txt

'khaled'.
'000000'.
'a.goal'.
'without.a'.
'plan.is'.
'just.a.wish.'.