Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aryan29/week1 #4

Open
wants to merge 27 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Week-1/aryan29/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
## Problem 1 - Ankit's Phone

This is a python program which can get you JSON of features of any number of mobiles in simple steps and store it in file named "data.json" and provide you with regular updates about decrease in price of mobiles mentioned

## Usage

The program initially comes with a "user_config.json" file in which you can add your custom settings to get started with using the product and make sure to add smartprix code of the product you want to keep track of after that just type this to get your json file

```python
python api.py -j
```
You can add mysmartprice code too as a second code for keeping track of best price from both of the websites
![Example UserConfig.json](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/user_config.png)
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
## Generating Codes from Smartprix and MySmartPrice
You can get smartprix code directly form your terminal
```python
python api.py -gc "Asus Zenfone Max Pro M1 6GB"
```
You can also use their website too to get the link

As shown below:-
Getting Link from Smartprix
![Getting Link from Smartprix](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/mysmartprixcode.png)

Getting Link from MySmartprice
![Getting Link from MySmartprice](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/mysmartpricecode.png)
## Setting Up Server
Setup comes with a file called keeptrack.py which you can run infiniely on your system for keep receiving regular updates for linux systems just run this command
```python
nohup keeptrack.py &
```
And for Windows try
[this](https://stackoverflow.com/questions/55932829/how-to-make-sure-that-python-script-will-run-forever-on-windows)
Remeber to restart the service each time you reboot or try adding a new cronjob or use any of the ways mentioned on [this](https://stackoverflow.com/questions/24518522/run-python-script-at-startup-in-ubuntu/25805871) link
Windows user can make use of Microsoft Task Scheduler to do the Same
## Features
We get you the JSON of features Any Number of Mobiles in just a single Click

Example JSON file Generated
![Example JSON file Generated](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/data(json).png)
We get you the best price of mobile by making use of the websites like [Smartprix](https://www.smartprix.com/) and [MySmartPrice](https://www.mysmartprice.com/) which compares price of mobile from various stores like Amazon,Flipkart,Ebay,Shopclues and various others

For Smartprix there is no need to get the code as well we can do that for you by a single click

We Provide you with Emails and Telegram Messages whenever there is a price decerease in your products from the time when you checked it last(We check for decrease in Price in every 3 hours)

Example Image:-
![Price Change Updates](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/Screenshot_20191215-224146.png)
We Provide you with Daily Emails and messages regarding whether your server is running or not if it is not Working you will not recive Email so you can switch it on
Example Image:-
![Server Running Update](https://github.com/aryan29/Git-Freeze/blob/aryan29/week1/Week-1/aryan29/images/Screenshot_20191215-224219.png)
## Updates
Added a smart feature to directly compare 2 phones on basis of their spec score just use
```python
python api.py -cm "Asus Zenfone Max Pro M1" "Redmi Mi A2"
```
## Contributing
Feel Free to Contribute and for any suggestions you can mail me at <nk28agra@gmail.com>
## License
[MIT](https://choosealicense.com/licenses/mit/)
Binary file added Week-1/aryan29/__pycache__/api.cpython-36.pyc
Binary file not shown.
110 changes: 110 additions & 0 deletions Week-1/aryan29/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import requests
import bs4
import sys
from termcolor import colored,cprint
import json
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
with open("user_config.json","r") as f:
conf=json.load(f)
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
if(len(conf["mob"])==0):
cprint("Please Configure your user_config.json file and add Code of Atleast 1 mobile Phone to generate json",'red')
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
exit(0)
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
class Api:
def get_data(self,mob):
b=requests.get(f"https://www.smartprix.com/mobiles/{mob[0]}")
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
l1=bs4.BeautifulSoup(b.text,'lxml')
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
price2=l1.select("#product-price .price")[0]
try:
a=requests.get(f"https://www.mysmartprice.com/mobile/{mob[1]}")
l=bs4.BeautifulSoup(a.text,'lxml')
price1=l.select(".prdct-dtl__prc-val")[0]
price=self.getminprice(price1,price2)
except:
price=price2
a=l1.select(".has-favorite-button , .cons span , .pros span")
f_list=a
f_list.append(price)
return f_list
def getminprice(self,price1,price2):
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
#print((price1.getText()),price2.getText()[1:])
if((price1.getText(),10)<(price2.getText()[1:],10)):
return price1
else:
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
return price2
def name_data(self,mob,code):
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
list=[]
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
for i in range(len(mob)):
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
list.append(mob[i].getText().replace('\u2009',' ').replace('\u20b9',''))
#print(list)
dict={"name":list[0],"RAM/Internal Storage":list[3],"Battery":list[4],"Camera":list[6],"Screen-Size":list[5],"Connectivity":list[1],"OS":list[8],"Best Price":list[9],"Processor":list[2],"Code":code}
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
return dict
def generate_json(self,mob):
list=[]
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
try:
f=open("data.json","r")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single character variable is highly discouraged.

data=json.load(f)
f.close()
except:
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
f=open("data.json","w")
print("[]",file=f)
data=[]
f.close()
with open("data.json","w") as f:
data.append(mob)
json.dump(data,f,indent=4)
def organize(self):
for i in conf["mob"]:
mob=conf["mob"][i]
m=self.get_data(mob)
j=self.name_data(m,mob)
self.generate_json(j)
cprint("Json File successfull generated Do you want to Receive Price Updates on this email?(y/n)",'yellow')
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
res=input()
if(res=="Y" or res=="y"):
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
email=input("Enter your Email")
conf['receiver_email'].append(email)
data=conf
with open("user_config.json","w") as f:
json.dump(data,f,indent=4)
cprint("We added your email successfully",'green')
else:
cprint("Thanks for using us",'magenta')
def get_smartprix_code(self,mob_name):
print(mob_name)
q=requests.get(f"https://www.smartprix.com/products/?q={mob_name}")
s=bs4.BeautifulSoup(q.text,'lxml')
sel=s.select(".f-mobiles:nth-child(1) h2 a")
st=sel[0]['href']
x=st[st.find("mobiles/")+8:]
cprint(f"Smartprix Code -> {x}",'magenta')
def smart_compare(self,mob1,mob2):
q1=requests.get(f"https://www.smartprix.com/products/?q={mob1}")
q2=requests.get(f"https://www.smartprix.com/products/?q={mob2}")
s1=bs4.BeautifulSoup(q1.text,'lxml')
sel1=s1.select(".f-mobiles:nth-child(1) .score-val")[0]
s2=bs4.BeautifulSoup(q2.text,'lxml')
sel2=s2.select(".f-mobiles:nth-child(1) .score-val")[0]
#print(sel2,sel1)
if(int(sel2.text)>int(sel1.text)):
mob1,mob2=mob2,mob1
cprint(f"{mob1} is better than {mob2} comparing all the features",'cyan')

if __name__ == "__main__":
if(len(sys.argv)>1):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you familiar with 'argparse'?

if(sys.argv[1]=='-j'):
Api().organize()
elif(sys.argv[1]=='-gc'):
if(len(sys.argv)!=3):
cprint("Enter a valid mobile name enter -h for help",'cyan')
else:
Api().get_smartprix_code(sys.argv[2])
elif(sys.argv[1]=='-cm'):
if(len(sys.argv)!=4):
cprint("Enter a valid mobile name enter -h for help",'cyan')
else:
Api().smart_compare(sys.argv[2],sys.argv[3])
elif(sys.argv[1]=='-h'):
cprint("-j -> Gets you a json file of Mobiles\n"+"-gc [Mobile Name]-> Gets you Smartprix Code for a particular Mobile\n"+"-cm [Mobile 1] [Mobie 2] to compare phones on basis of their spec score\n"+"-h -> Help Menu\n",'cyan')
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
else:
cprint("Enter a valid mobile name enter -h for help",'cyan')
else:
cprint("Invalid Command -h for help",'red')
Empty file added Week-1/aryan29/data.json
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week-1/aryan29/images/data(json).png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week-1/aryan29/images/mysmartpricecode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week-1/aryan29/images/mysmartprixcode.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Week-1/aryan29/images/user_config.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
67 changes: 67 additions & 0 deletions Week-1/aryan29/keeptrack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import time,json
import smtplib
from termcolor import colored,cprint
import requests
from datetime import datetime,timedelta
from api import Api
dateIndex=datetime.now()
with open("user_config.json","r") as f:
conf=json.load(f)

if(conf["sender_email"]=="" or conf["sender_password"]=="" or len(conf["receiver_email"])==0):
cprint("Please Make sure to Enter Sender_Email, Receiver_Email and Sender_Password for Gmail before continuing",'red')
exit(0)

def keeptrack():
with open("data.json","r") as f:
obj=json.load(f)
#print(obj)
for mobile in obj:
name=mobile['name']
oldPrice=mobile['Best Price']
newPrice=get_current_price(mobile['Code'])
print(oldPrice,newPrice)
if(newPrice<oldPrice):
message=f"Best Price of {name} has changed from {oldPrice} to {newPrice} at {time.ctime(time.time())}"
sendEmail(message)
telegram_alert(message)
with open("logs.txt","w") as f:
print(message,file=f)
def get_current_price(mob):
x=Api().get_data(mob)
return Api().name_data(x,mob)['Best Price']
def sendEmail(msg):
try:
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(conf["sender_email"], conf["sender_password"])
list=conf["receiver_email"]
message = "Subject:Price Updates\n\n"+msg
print(f"Message is {message}")
for lines in list:
s.sendmail(conf['sender_email'], lines, message)
s.quit()
except:
cprint("Email Credentials not avilable or they are not valid please allow less secure apps from your gmail if your credentials are correct",'red')
def telegram_alert(bot_message):
try:
bot_token=conf['telegram_bot_token']
bot_chatID=conf['telegram_chatID']
url="https://api.telegram.org/bot"+bot_token+'/sendMessage?chat_id'+bot_chatID+'&parse_mode=Markdown&text='+bot_message
except:
cprint("Telegram Credentials not avilable or they are not valid",'red')
res=requests.get(url)
def checkserver():
msg="Daily Server Check"
#telegram_alert(msg)
sendEmail(msg)
if __name__ == "__main__":
updation_time=60*60*3 #3 hour updates
c=0
while(1):
c=c+1
keeptrack()
if(c%8==1):
checkserver() #1day updates
time.sleep(updation_time)
time.sleep(updation_time)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not reviewing each line. But these are the general errors I saw after having a look:

  • Some lines seem too long to follow PEP8
  • Documentation is None.
  • Please defer from using multiple imports in a single line.
  • For constants, using UPPER_CASE should be the way to go.
  • Pick a style.
  • Some imports are unused.
  • Correct your import order.
  • Few variables are unused. e.g.: res
  • f is used in two scopes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now using PEP8
Import Rules Done
Line Too Long Done
Using mixedCase for Variables, snake_case for functions and UPPER_CASE for constants
Unused variables and functions removed

1 change: 1 addition & 0 deletions Week-1/aryan29/logs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Best Price of Asus Zenfone Max Pro M1 has changed from 9,499 to 8,499 at Sun Dec 15 23:05:41 2019
26 changes: 26 additions & 0 deletions Week-1/aryan29/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
appdirs==1.4.3
beautifulsoup4==4.8.1
bs4==0.0.1
certifi==2019.11.28
chardet==3.0.4
cssselect==1.1.0
fake-useragent==0.1.11
idna==2.8
itsdangerous==1.1.0
lxml==4.4.2
MarkupSafe==1.1.1
parse==1.12.1
pyee==6.0.0
pyppeteer==0.0.25
pyquery==1.4.1
requests==2.22.0
requests-html==0.10.0
six==1.13.0
soupsieve==1.9.5
telegram==0.0.1
termcolor==1.1.0
tqdm==4.40.2
urllib3==1.25.7
w3lib==1.21.0
websockets==8.1
Werkzeug==0.16.0
23 changes: 23 additions & 0 deletions Week-1/aryan29/user_config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"sender_email": "admin@gmail.com",
"sender_password": "password",
"receiver_email": [
"aaa@gmail.com",
"bbb@gmail.com"
],
"telegram_bot_token": "",
"telegram_chatID": "",
"mob": {
"1": [
"apple-iphone-11-ppd17p0q4s2c",
"apple-iphone-xir-msp15688"
],
"2": [
"oneplus-7t-pro-ppd16isrkf1u",
"oneplus-7t-pro-msp15861"
],
"3": [
"asus-zenfone-max-pro-m1-p11010rm1y0x"
]
}
}