Steven Smith revised this gist . Go to revision
1 file changed, 11 insertions, 2 deletions
instagram-cloudbot.py
| @@ -1,12 +1,21 @@ | |||
| 1 | 1 | from util import hook,http,web | |
| 2 | + | from json import loads | |
| 2 | 3 | from urllib2 import HTTPError | |
| 3 | 4 | ||
| 4 | - | @hook.regex(r'instagram.com/p/(.+?)/') | |
| 5 | + | oldplugin = """@hook.regex(r'instagram.com/p/(.+?)/') | |
| 5 | 6 | def ig_regex(match, bot=None): | |
| 6 | - | """ Returns info for instagram urls """ | |
| 7 | + | "Returns info for instagram urls " | |
| 7 | 8 | token = bot.config.get("api_keys", {}).get("instagram", None) | |
| 8 | 9 | try: | |
| 9 | 10 | data = http.get_json("https://api.instagram.com/v1/media/shortcode/{}?access_token={}".format(match.group(1), token))["data"] | |
| 10 | 11 | return u"{caption[from][full_name]}: \"{caption[text]}\" ({likes[count]} likes)".format(**dict(emoji=u"\u1F5BC" if data["type"] == "image" else u"\u1F4FC", **data)) | |
| 11 | 12 | except (ValueError, HTTPError, KeyError) as e: | |
| 12 | 13 | raise | |
| 14 | + | """ | |
| 15 | + | ||
| 16 | + | @hook.regex(r'instagram.com/p/(.+?)/') | |
| 17 | + | def ig_regex(match): | |
| 18 | + | "Now with more scraping because of stupid api use cases" | |
| 19 | + | page = http.get("https://instagram.com/p/{}/".format(match.group(1)), user_agent=http.ua_chrome) | |
| 20 | + | data = loads(page.split('<script type="text/javascript">window._sharedData = ')[1].split(";</script>")[0])["entry_data"]["PostPage"][0]["media"] | |
| 21 | + | return u'{owner[full_name]} ({owner[username]}): "{caption}" ({likes[count]} likes)'.format(**data) | |
Steven Smith revised this gist . Go to revision
1 file changed, 12 insertions
instagram-cloudbot.py(file created)
| @@ -0,0 +1,12 @@ | |||
| 1 | + | from util import hook,http,web | |
| 2 | + | from urllib2 import HTTPError | |
| 3 | + | ||
| 4 | + | @hook.regex(r'instagram.com/p/(.+?)/') | |
| 5 | + | def ig_regex(match, bot=None): | |
| 6 | + | """ Returns info for instagram urls """ | |
| 7 | + | token = bot.config.get("api_keys", {}).get("instagram", None) | |
| 8 | + | try: | |
| 9 | + | data = http.get_json("https://api.instagram.com/v1/media/shortcode/{}?access_token={}".format(match.group(1), token))["data"] | |
| 10 | + | return u"{caption[from][full_name]}: \"{caption[text]}\" ({likes[count]} likes)".format(**dict(emoji=u"\u1F5BC" if data["type"] == "image" else u"\u1F4FC", **data)) | |
| 11 | + | except (ValueError, HTTPError, KeyError) as e: | |
| 12 | + | raise | |