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 all 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](images/user_config.png)
## 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](images/mysmartprixcode.png)

Getting Link from MySmartprice
![Getting Link from MySmartprice](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](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](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](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/)
139 changes: 139 additions & 0 deletions Week-1/aryan29/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import sys
import json
aryan29 marked this conversation as resolved.
Show resolved Hide resolved
import requests
import bs4
from termcolor import cprint
with open("user_config.json", "r") as f:
CONF = json.load(f)
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')
sys.exit()


class Api:
def get_data(self, mob):
req1 = requests.get(f"https://www.smartprix.com/mobiles/{mob[0]}")
soup1 = bs4.BeautifulSoup(req1.text, 'lxml')
price1 = soup1.select("#product-price .price")[0]
try:
req2 = requests.get(
f"https://www.mysmartprice.com/mobile/{mob[1]}"
)
soup2 = bs4.BeautifulSoup(req2.text, 'lxml')
price2 = soup2.select(".prdct-dtl__prc-val")[0]
price = self.get_min_price(price2, price1)
except Exception as ex:
price = price1
myList = soup1.select(".has-favorite-button , .cons span , .pros span")
myList.append(price)
return myList

@staticmethod
def get_min_price(price1, price2):
# 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

@staticmethod
def name_data(mob, code):
myList = []
for spec in mob:
myList.append(spec.getText()
.replace('\u2009', ' ').replace('\u20b9', ''))
# print(List)
myDict = {"name": myList[0],
"RAM/Internal Storage": myList[3],
"Battery": myList[4],
"Camera": myList[6],
"Screen-Size": myList[5],
"Connectivity": myList[1],
"OS": myList[8],
"Best Price": myList[9],
"Processor": myList[2],
"Code": code}
return myDict

def generate_json(self, mob):
try:
f = open("data.json", "r")
data = json.load(f)
f.close()
except Exception as ex:
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')
res = input()
if (res in ("Y", "y")):
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)
req = requests.get(f"https://www.smartprix.com/products/?q={mob_name}")
soup = bs4.BeautifulSoup(req.text, 'lxml')
sel = soup.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):
req1 = requests.get(f"https://www.smartprix.com/products/?q={mob1}")
req2 = requests.get(f"https://www.smartprix.com/products/?q={mob2}")
soup1 = bs4.BeautifulSoup(req1.text, 'lxml')
sel1 = soup1.select(".f-mobiles:nth-child(1) .score-val")[0]
soup2 = bs4.BeautifulSoup(req2.text, 'lxml')
sel2 = soup2.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}", 'cyan')


if __name__ == "__main__":
if (len(sys.argv) > 1):
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'):
HELPMENU = ('-j -> GetsyouajsonfileofMobiles\n'
'-gc [Mobile Name]-> Gets you Smartprix Code'
'for a Mobile\n'
'-cm [Mobile 1] [Mobie 2] to compare phones on basis '
'of their spec score\n'
'-h -> Help Menu\n')
cprint(HELPMENU, 'cyan')
else:
cprint("Enter a valid mobile name enter -h for help", 'cyan')
else:
cprint("Invalid Command -h for help", 'red')
46 changes: 46 additions & 0 deletions Week-1/aryan29/data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[
{
"name": "Apple iPhone 11",
"RAM/Internal Storage": "4 GB RAM, 64 GB inbuilt",
"Battery": "3110 mAh Battery",
"Camera": "12 MP + 12 MP Dual Rear & 12 MP Front Camera",
"Screen-Size": "6.1 inches, 828 x 1792 px Display with Large Notch",
"Connectivity": "Dual Sim, 3G, 4G, VoLTE, Wi-Fi, NFC",
"OS": "iOS v13",
"Best Price": "63,999",
"Processor": "A13 Bionic, Hexa Core, 2.65 GHz Processor",
"Code": [
"apple-iphone-11-ppd17p0q4s2c",
"apple-iphone-xir-msp15688"
]
},
{
"name": "OnePlus 7T Pro",
"RAM/Internal Storage": "8 GB RAM, 256 GB inbuilt",
"Battery": "4085 mAh Battery with Fast Charging ",
"Camera": "48 MP + 16 MP + 8 MP Triple Rear & 16 MP Front Camera",
"Screen-Size": "6.67 inches, 1440 x 3120 px Display",
"Connectivity": "Dual Sim, 3G, 4G, VoLTE, Wi-Fi, NFC",
"OS": "Android v10.0",
"Best Price": "55,499",
"Processor": "Snapdragon 855+, Octa Core, 2.96 GHz Processor",
"Code": [
"oneplus-7t-pro-ppd16isrkf1u",
"oneplus-7t-pro-msp15861"
]
},
{
"name": "Asus Zenfone Max Pro M1",
"RAM/Internal Storage": "4 GB RAM, 64 GB inbuilt",
"Battery": "5000 mAh Battery",
"Camera": "13 MP + 5 MP Dual Rear & 8 MP Front Camera",
"Screen-Size": "5.99 inches, 1080 x 2160 px Display",
"Connectivity": "Dual Sim, 3G, 4G, VoLTE, Wi-Fi",
"OS": "Android v8.1 (Oreo)",
"Best Price": "8,499",
"Processor": "Snapdragon 636, Octa Core, 1.8 GHz Processor",
"Code": [
"asus-zenfone-max-pro-m1-p11010rm1y0x"
]
}
]
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.
83 changes: 83 additions & 0 deletions Week-1/aryan29/keeptrack.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import time
import sys
import json
import smtplib
import requests
from termcolor import cprint
from api import Api
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')
sys.exit()


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}"
f"to {newPrice} at {time.ctime(time.time())}")
send_email(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 send_email(msg):
try:
s = smtplib.SMTP('smtp.gmail.com', 587)
s.starttls()
s.login(CONF["sender_email"], CONF["sender_password"])
myList = CONF["receiver_email"]
message = "Subject:Price Updates\n\n"+msg
print(f"message is {message}")
for lines in myList:
s.sendmail(CONF['sender_email'], lines, message)
s.quit()
except Exception as ex:
cprint(("Email Credentials not avilable or they are not valid "
"pleaseallow less secure apps from your "
" gmail if your credentials are correct"), 'red')
sys.exit()


def telegram_alert(BotMessage):
try:
botToken = CONF['telegram_bot_token']
botChatId = CONF['telegram_chatID']
url = ('https://api.telegram.org/bot' + botToken + '/sendMessage'
'?chat_id'+botChatId+'&parse_mode=Markdown&text='+BotMessage)
except Exception as ex:
cprint(f"While logging in Telegram {ex} occured", 'red')
requests.get(url)


def checkserver():
msg = "Daily Server Check"
# telegram_alert(msg)
send_email(msg)


if __name__ == "__main__":
updationTime = 60 * 60 * 3 # 3 hour updates
COUNT = 0
while(1):
COUNT = COUNT + 1
keeptrack()
if (COUNT % 8 == 1):
checkserver() # 1day updates
time.sleep(updationTime)
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 OnePlus 7T Pro has changed from55,499to 53,499 at Tue Dec 24 00:24:19 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"
]
}
}