Last active
June 18, 2023 08:53
-
-
Save Realiserad/099765497ad3ab7a83e61e3145976a0c to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
| from bs4 import BeautifulSoup | |
| import pickle | |
| from urllib.request import urlopen | |
| import json | |
| from urllib.parse import quote_plus | |
| import sys | |
| import os | |
| # Letar efter nya annonser på Sporthoj.com | |
| template = """Ny annons på Sporthoj.com. | |
| Titel: {title} | |
| Pris: {price} | |
| Länk: https://sporthoj.com/annonser/id/{id} | |
| {description}""" | |
| storage = os.path.join(sys.path[0], 'id') | |
| failures = 0 | |
| try: | |
| next_id = pickle.load(open(storage, 'rb')) | |
| except: | |
| next_id = 10458 | |
| while True: | |
| next_id += 1 | |
| print('Trying to load ad #{0}'.format(next_id)) | |
| page = requests.get("https://sporthoj.com/annonser/id/{0}".format(next_id)) | |
| if (failures == 10): | |
| # This probably means that there are no new ads | |
| break | |
| if (page.status_code != 200): | |
| failures += 1 | |
| continue | |
| failures = 0 | |
| soup = BeautifulSoup(page.content, 'html.parser') | |
| parsed_title = soup.select('h1')[0].get_text() | |
| parsed_price = soup.find_all('span', class_ = 'buysell-price')[0].get_text() | |
| parsed_description = soup.select('p')[0].get_text() | |
| message = template.format( | |
| id = next_id, | |
| title = parsed_title, | |
| price = parsed_price, | |
| description = parsed_description) | |
| #print(message) | |
| libsignal = urlopen("https://signal.stormhub.org/v1/send?recipient=46733907485&message={0}&apikey=bastian-8a85xM5e5Rbfak".format(quote_plus(message))) | |
| response = json.loads(libsignal.read()) | |
| print('-> ' + json.dumps(response, indent = 4, sort_keys = True)) | |
| pickle.dump(next_id, open(storage, 'wb')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment