Skip to content

Instantly share code, notes, and snippets.

@Gictorbit
Last active March 19, 2021 22:04
Show Gist options
  • Save Gictorbit/1880be5f126644ad9743b593702e6aa8 to your computer and use it in GitHub Desktop.
Save Gictorbit/1880be5f126644ad9743b593702e6aa8 to your computer and use it in GitHub Desktop.
digikala login and print personal information
#! /usr/bin/env python3
import requests
from bs4 import BeautifulSoup
from pprint import pprint
from furl import furl
import sys
from prettytable import PrettyTable
import getpass
def main():
global session,website_host
login()
personal_info={}
#get profile information
info_list=[]
profile_resp = session.get(url=f"{website_host}/profile/personal-info/")
soup = BeautifulSoup(profile_resp.text,"lxml")
for info in soup.findAll("div",attrs={'class':'c-profile-personal__grid-item-value'}):
info_list.append(str(info.text).strip())
info_list[1] = fa_to_en(info_list[1])
info_list[2] = fa_to_en(info_list[2])
fields =['full name','national_id','phone','email','birth_date','job','pay_back','password']
print()
if sys.argv[2] == '--json':
index=0
for f in fields:
personal_info[f] = info_list[index]
index+=1
pprint(personal_info)
elif sys.argv[2] == '--table':
table = PrettyTable()
for element in range(8):
table.add_row([fields[element],info_list[element]])
print(table)
else:
raise Exception("Invalid Option")
def fa_to_en(number:str)->str:
numbers_dict={
'۰':'0',
'۱':'1',
'۲':'2',
'۳':'3',
'۴':'4',
'۵':'5',
'۶':'6',
'۷':'7',
'۸':'8',
'۹':'9'
}
result=''
for digit in number:
en_digit = numbers_dict.get(digit)
if en_digit == None:
raise Exception(f"Error: '{digit}' charachter is not a number")
else:
result+=en_digit
return result
def login()->str:
global session,website_host
loginURL=f"{website_host}/users/login-register/"
#post username
login_resp = session.get(url=loginURL)
post_username_data = find_hidden_inputs(response=login_resp)
post_username_data['login[email_phone]'] = sys.argv[1]
post_username_resp = session.post(loginURL,data=post_username_data,allow_redirects=False)
location = post_username_resp.headers['Location']
login_token = furl(location).args['token']
#post password
login_resp = session.get(url=website_host+location)
post_password_data = find_hidden_inputs(response=login_resp)
post_password_data['login[password]'] = getpass.getpass("enter your password: ",)
post_password_resp = session.post(url=website_host+location,data=post_password_data)
# print(post_password_resp.text)
return login_token
def find_hidden_inputs(response)->dict:
data={}
soup = BeautifulSoup(response.content,"lxml")
hidden_inputs = soup.find_all('input',attrs={'type':'hidden'})
for input in hidden_inputs:
data[input['name']] = input['value']
return data
if __name__ == "__main__":
session = requests.session()
website_host="https://www.digikala.com"
main()
requests==2.25.1
furl==2.1.0
prettytable==0.7.2
beautifulsoup4==4.9.3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment