Created
January 24, 2021 21:39
-
-
Save Tchekda/172292a03befcc828e72b00fae163342 to your computer and use it in GitHub Desktop.
Apartement Searcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import json | |
import time | |
import urllib.parse | |
import logging | |
import sys | |
def countSite(url, regex, name): | |
global sites | |
try: | |
page = requests.get(url) | |
except: | |
logging.error("An error occured %s " % sys.exc_info()[0]) | |
return [] | |
else : | |
result = re.findall(regex, page.text) | |
unique = [] | |
for link in result: | |
if link not in unique: | |
unique.append(link) | |
new = [] | |
previous = len(sites[name]['offers']) | |
for offer in unique: | |
if offer not in sites[name]['offers']: | |
if previous > 0: | |
new.append(offer) | |
sites[name]['offers'].append(offer) | |
removed = 0 | |
#for offer in sites[name]['offers']: | |
# if offer not in unique: | |
# removed += 1 | |
# sites[name]['offers'].remove(offer) | |
# print("removed %s" % offer) | |
if len(unique) == 0: | |
message = "Aucune offre trouvée sur %s" % name | |
url = "https://smsapi.free-mobile.fr/sendmsg?user=XXXXXXX&pass=XXXXXXXXX&msg=" + urllib.parse.quote(message) | |
requests.get(url) | |
logging.info("Found %s offers on %s (%s new, %s remove, %s previous)" % (len(unique), name, len(new), removed, previous)) | |
return new | |
if __name__ == "__main__": | |
logging.basicConfig(filename='immo.log', level=logging.INFO, format='%(asctime)s Immo %(levelname)s: %(message)s') | |
#logging.getLogger().addHandler(logging.StreamHandler()) | |
sites = { | |
'CA': { | |
'url': "https://www.ca-immobilier.fr/louer/recherche?sortby=undefined&codes=69000%3Alyon%3A69123%2C69100%3Avilleurbanne%3A69266%2C69200%3Av%C3%A9nissieux%3A69259%2C69500%3Abron%3A69029§ions=location&types=appartment&zones=&distance=0&displayMode=mosaic", | |
're': r"<a role=\"link\" href=\"(/biens/location/\S+/\d+)\" class=\"tc-link\"", | |
'prefix': "https://www.ca-immobilier.fr", | |
'offers': [] | |
}, | |
'Citya': { | |
'url': "https://www.citya.com/annonces/location/appartement/lyon-3e-arrondissement-69003,lyon-7e-arrondissement-69007,lyon-8e-arrondissement-69008?prixMax=750&surfaceMin=30", | |
're': r"a href=\"(/annonces/location/appartement/.+)\" class=", | |
'prefix': "https://www.citya.com", | |
'offers': [] | |
}, | |
'Foncia': { | |
'url': "https://fr.foncia.com/location/lyon-69--villeurbanne-69100--venissieux-69200/appartement/(params)/on/(surface_min)/30/(prix_max)/750/(pieces)/1--2/(parking)/1", | |
're': r"<a href=\"(/location/\S+/appartement/\d+\.htm)\" class=\"TeaserOffer-ill\"", | |
'prefix': "https://fr.foncia.com", | |
'offers': [] | |
}, | |
} | |
while True: | |
for site, data in sites.items(): | |
for offer in countSite(data['url'], data['re'], site): | |
link = data['prefix'] + offer | |
logging.info("New offer appeared on %s : %s" % (site, link)) | |
message = "Nouvel appartement sur %s : %s" % (site, link) | |
url = "https://smsapi.free-mobile.fr/sendmsg?user=XXXXXXXXX&pass=XXXXXXXXXXX&msg=" + urllib.parse.quote(message) | |
requests.get(url) | |
time.sleep(1 * 60) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment