Created
January 27, 2016 19:34
-
-
Save SkyLeite/73b952eccf6f457330f9 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 discord | |
| from discord.ext import commands | |
| from shutil import copyfile | |
| import pickle | |
| import asyncio | |
| import json, unidecode, requests, threading, fileinput, copy | |
| client = discord.Client() | |
| channel = discord.Object("121156929409122306") | |
| @client.async_event | |
| def on_ready(): | |
| print("Bot is running as the user '{}'".format(client.user.name)) | |
| #Save Data | |
| @client.async_event | |
| def on_message(message): | |
| if message.content.startswith('!highfive'): | |
| yield from client.send_message(message.channel, '*high fives %s*' % message.author) | |
| elif message.content.startswith('!pso2'): | |
| pso2msg = '**English Installer:** http://goo.gl/nRm9wo \n**Signup Guide:** http://goo.gl/3MPTHL \n**News:** http://goo.gl/UJJu57 \n**Cirnopedia:** http://goo.gl/UlfvKv' | |
| yield from client.send_message(message.channel, pso2msg) | |
| elif message.content.startswith('!commands'): | |
| commands = " **!manga manganame** = adds manga to the watchlist. \n **!pso2** = shows useful PSO2 links \n **!highfive** = highfives" | |
| yield from client.send_message(message.channel, commands) | |
| elif message.content.startswith('(╯°□°)╯︵ ┻━┻'): | |
| yield from client.send_message(message.channel, "┬─┬ノ(ಠ_ಠノ) Please respect tables") | |
| elif message.content.startswith('!elsword'): | |
| yield from client.send_message(message.channel, "https://www.youtube.com/watch?v=jt8UHzk_mjQ") | |
| elif message.content == 'Elsword': | |
| yield from client.send_message(message.channel, "@%s shut up" % message.author) | |
| elif message.content.startswith('!manga'): | |
| manganame = message.content.split(" ", 1)[1] | |
| query = requests.get("https://doodle-manga-scraper.p.mashape.com/mangafox.me/search?cover=0&info=0&l=1&q=%s" % manganame, | |
| headers={ | |
| "X-Mashape-Key": "Q7n8VApOzPmshmeZQNtthye630hmp1LDw0HjsnZIoR8eOu4JX6", | |
| "Accept": "text/plain" | |
| } | |
| ) | |
| dataq = unidecode.unidecode(query.text) | |
| parsedq = json.loads(dataq) | |
| try: | |
| mangaid = parsedq[0]['mangaId'] | |
| r = requests.get("https://doodle-manga-scraper.p.mashape.com/mangafox.me/manga/%s/" % mangaid, | |
| headers={ | |
| "X-Mashape-Key": "Q7n8VApOzPmshmeZQNtthye630hmp1LDw0HjsnZIoR8eOu4JX6", | |
| "Accept": "text/plain" | |
| } | |
| ) | |
| data = unidecode.unidecode(r.text) | |
| parsed = json.loads(data) | |
| manga = "%s:%s\n" % (mangaid, parsed["chapters"][-1]["chapterId"]) | |
| f = open("manga.txt","a") #in append mode | |
| f.write(manga) | |
| f.close() | |
| except KeyError: | |
| yield from client.send_message(channel, 'Something went wrong. Try again with another name.') | |
| if parsed: | |
| yield from client.send_message(channel, '%s added to the watchlist.' % parsed['name']) | |
| else: | |
| yield from client.send_message(channel, 'Something went wrong.') | |
| @asyncio.coroutine | |
| def my_background_task(): | |
| yield from client.wait_until_ready() | |
| channel = discord.Object(id="121156929409122306") | |
| while not client.is_closed: | |
| copyfile("manga.txt", "bak.txt") | |
| f = open("bak.txt", "r") | |
| for line in f: | |
| m = line.split(':', 1) | |
| for n,i in enumerate(m): | |
| if i=="\n": | |
| m[n]="" | |
| rs = requests.get("https://doodle-manga-scraper.p.mashape.com/mangafox.me/manga/%s/" % m[0], | |
| headers={ | |
| "X-Mashape-Key": "Q7n8VApOzPmshmeZQNtthye630hmp1LDw0HjsnZIoR8eOu4JX6", | |
| } | |
| ) | |
| datas = unidecode.unidecode(rs.text) | |
| parseds = json.loads(datas) | |
| lastChapter = parseds["chapters"][-1]["chapterId"] | |
| if m[1] != str(lastChapter) + '\n': | |
| channel = discord.Object("121156929409122306") | |
| yield from client.send_message(channel, 'New chapter for %s is available at mangafox.me' % m[0]) | |
| with fileinput.FileInput("manga.txt", inplace=True, backup='.bak') as file: | |
| for line in file: | |
| print(line.replace(m[1], str(lastChapter) + '\n'), end='') | |
| yield from asyncio.sleep(1200) # task runs every 5 seconds | |
| loop = asyncio.get_event_loop() | |
| try: | |
| loop.create_task(my_background_task()) | |
| loop.run_until_complete(client.login('mangabotdiscord@gmail.com', '54286502rr')) | |
| loop.run_until_complete(client.connect()) | |
| except Exception: | |
| loop.run_until_complete(client.close()) | |
| finally: | |
| loop.close() | |
| print("Starting bot...") | |
| client.run("x", "y") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment