Skip to content
Extraits de code Groupes Projets
Valider 9d935aff rédigé par Loïc Wikle DUBARD's avatar Loïc Wikle DUBARD
Parcourir les fichiers

shitpostbot insta parser

parent 3ff95db5
Branches
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -59,7 +59,7 @@ class Bot(Client): ...@@ -59,7 +59,7 @@ class Bot(Client):
'!memes <image_name_or_url> ; <texte1> ; <texte2(facultatif)> ... not yet implemented\n' + \ '!memes <image_name_or_url> ; <texte1> ; <texte2(facultatif)> ... not yet implemented\n' + \
'!ping \n' + \ '!ping \n' + \
'!translate <phrase> ; <lang_source> ; <lang_dest>\n' + \ '!translate <phrase> ; <lang_source> ; <lang_dest>\n' + \
'!shitpostbot5000 [status|on|off|random] ... ne pas utiliser PLEASE\n' + \ '!shitpostbot5000 [status|on|off|random|timer] ... ne pas utiliser PLEASE\n' + \
'!about -> vas-y test-moi !\n' '!about -> vas-y test-moi !\n'
return help_text return help_text
elif '!translate' in texte: elif '!translate' in texte:
...@@ -102,8 +102,12 @@ class Bot(Client): ...@@ -102,8 +102,12 @@ class Bot(Client):
elif texte == '!shitpostbot5000 off': elif texte == '!shitpostbot5000 off':
if thread_id in Bot.shitpostbot: if thread_id in Bot.shitpostbot:
Bot.shitpostbot[thread_id][0].set_off() Bot.shitpostbot[thread_id][0].set_off()
elif texte == '!shitpostbot5000 random': elif '!shitpostbot5000 random' in texte:
self.sendLocalImage(Shitpostbot5000.random(), thread_id=thread_id, thread_type=thread_type) texte = texte.replace('!shitpostbot5000 random', '')
if texte == '':
return Shitpostbot5000.random()
else:
return Shitpostbot5000.random(texte)
# pour rigoler # pour rigoler
elif '!memes templates' in texte: elif '!memes templates' in texte:
......
...@@ -3,7 +3,12 @@ from urllib.request import urlopen ...@@ -3,7 +3,12 @@ from urllib.request import urlopen
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
import re import re
import urllib.request import urllib.request
import urllib.parse
import urllib.error
import datetime import datetime
import ssl
import json
import random
class Shitpostbot5000(object): class Shitpostbot5000(object):
...@@ -39,15 +44,27 @@ class Shitpostbot5000(object): ...@@ -39,15 +44,27 @@ class Shitpostbot5000(object):
return self.out return self.out
@classmethod @classmethod
def random(self): def getlinks(self, hashtag, url):
html = urlopen('https://www.facebook.com/pg/shitpostbot5k/photos') html = urllib.request.urlopen(url, context=self.ctx).read()
bs = BeautifulSoup(html, 'html.parser') soup = BeautifulSoup(html, 'html.parser')
images = bs.find_all('img', {'src': re.compile('.jpg')}) script = soup.find('script', text=lambda t: t.startswith('window._sharedData'))
images2 = bs.find_all('image_src') page_json = script.text.split(' = ', 1)[1].rstrip(';')
for image in images: data = json.loads(page_json)
print(image["src"]) print('Scraping links with #' + hashtag + "...........")
return None links = []
for post in data['entry_data']['TagPage'][0]['graphql']['hashtag']['edge_hashtag_to_media']['edges']:
image_src = post['node']['thumbnail_resources'][1]['src']
links.append(image_src)
return links
@classmethod
def random(self, hashtag=random.choice(['shitpostmeme', 'shitpost', 'shitposts', 'memes', 'climbingmemes', 'rockclimbingmemes', 'climbingmemesaredank', 'climbingmemesofinstagram'])):
self.ctx = ssl.create_default_context()
self.ctx.check_hostname = False
self.ctx.verify_mode = ssl.CERT_NONE
return random.choice(self.getlinks(hashtag, 'https://www.instagram.com/explore/tags/' + hashtag + '/'))
if __name__ == "__main__": if __name__ == "__main__":
img = Shitpostbot5000() img = Shitpostbot5000()
print(img.get_latest()) print(Shitpostbot5000.random('shitposts'))
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter