Skip to content

Instantly share code, notes, and snippets.

@gallexme
Created February 10, 2019 00:29
Show Gist options
  • Save gallexme/4c27a3363ba55c2bb58c14014a975bb0 to your computer and use it in GitHub Desktop.
Save gallexme/4c27a3363ba55c2bb58c14014a975bb0 to your computer and use it in GitHub Desktop.
defmodule CryptoDiscord.Application do
use Application
alias Alchemy.Client
defmodule Commands do
use Alchemy.Cogs
alias Alchemy.{Client, Cache, User, Embed}
import Embed
def get_fields(res) do
res
|> Enum.map(fn article ->
Task.async(fn ->
article_url =
case CryptoDiscord.SciHub.get_article(article.url) do
nil -> article.url
url -> url
end
%{name: article.title, value: article_url}
end)
end)
|> Enum.map(fn task -> Task.await(task, 25000) end)
|> Enum.to_list()
end
Cogs.def sci(article) do
article = message.content |> String.trim_leading("!sci") |> String.trim()
if Enum.count(String.split(article)) > 1 do
# Cogs.say "Searching Research Papers by Name/Title not yet Supported :("
res = CryptoDiscord.SciHub.search_article(String.split(article))
if Enum.count(res) > 0 do
Cogs.say("Getting #{Enum.count(res)} Research Articles ...")
fields = get_fields(res)
IO.inspect(fields)
%Embed{
fields: fields
}
|> title("Found Research Articles")
|> Embed.send()
else
Cogs.say("No Article Found :(")
end
else
Cogs.say("Getting Research Article ...")
case CryptoDiscord.SciHub.get_article(article) do
nil -> "Research Article #{article} not Found"
result -> result
end
|> Cogs.say()
end
end
end
def start(_type, _args) do
run = Client.start("NDIyMDQ1NjY4OTI4OTEzNDA4.DYWELQ.8wSuxhdtFqBj3rPbagiNW8e-9c0")
use Commands
run
end
end
defmodule CryptoDiscord.CoinMarketGap do
use Tesla
plug(Tesla.Middleware.BaseUrl, "https://api.coinmarketcap.com/v1")
plug(Tesla.Middleware.JSON)
adapter(Tesla.Adapter.Hackney, recv_timeout: 30_000)
# same as above with a slightly change to `user_repos/1`
def get_top() do
# pass `client` argument to `get` function
get(%Tesla.Client{}, "/ticker/")
end
# build dynamic client based on runtime arguments
end
defmodule CryptoDiscord.SciHub do
use Tesla
import Meeseeks.CSS
plug(Tesla.Middleware.BaseUrl, "http://sci-hub.tw")
adapter(Tesla.Adapter.Hackney, recv_timeout: 30_000)
def search_article(words) do
result = Tesla.get("https://scholar.google.de/scholar?q=" <> Enum.join(words, "+"))
html = Codepagex.from_string!(result.body, :ascii, Codepagex.use_utf_replacement())
articles = Meeseeks.all(Meeseeks.parse(html), css(".gs_rt > a"))
articles =
Enum.map(articles, fn article ->
%{
:title => Meeseeks.text(article),
:url => Meeseeks.attr(article, "href")
}
end)
articles
end
def get_article(address) do
result = get(%Tesla.Client{}, "/" <> address)
Meeseeks.parse(result.body)
|> Meeseeks.one(css("#pdf"))
|> Meeseeks.attr("src")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment