import praw
import OAuth2Util
import requests
import re
from bs4 import BeautifulSoup


STORE_LINK = "https://www.windowsphone.com/en-us/search"
SUBREDDIT = ""
REPLIED_COMMENTS = []
BOT_BY = """------

^Bot ^by ^[/u/iammrinal0](/user/iammrinal0)"""


def get_url(app_name):

    set_of_links = ""
    ctrl = 0
    heads = {"User-Agent": "Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0)"
             " Gecko/20100101 Firefox/36.0"}

    results = requests.get(STORE_LINK, params={"q": app_name},
                           headers=heads)
    soup = BeautifulSoup(results.content, "html.parser")
    for tag in soup.find_all("a", {"data-os": True}):
        if tag["data-os"] == "app":
            if tag.string is not None:
                univ_url = universal_url(tag["href"])
                if tag.string.lower() == app_name.lower():
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, univ_url)
                    break
                elif app_name.lower() in tag.string.lower():
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, univ_url)
                    ctrl += 1
                    if ctrl == 3:
                        break
                else:
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, univ_url)
                    ctrl += 1
                    if ctrl == 3:
                        break

    return set_of_links


def universal_url(url):
    return("{0}s?appid={1}".format(url[:28], url[-36:]))


def replied_file(comm_id):
    with open("comments.txt", "a") as f:
        f.write("\n")
        f.write("\n".join(comm_id))


def post_comment(comment, reply):
    comment.reply(reply)
    return comment.id


def get_app_name(stri):
    trigger = "\w*wpapp\[([^]]*)\]"
    exp = re.compile(trigger, re.I)
    found = exp.findall(stri)
    if found:
        return found


def main():

    comment_id = []
    with open("comments.txt", "r") as f:
        REPLIED_COMMENTS = f.read().splitlines()

    r = praw.Reddit(user_agent="WP Store Linker v0.1 by /u/iammrinal0")
    o = OAuth2Util.OAuth2Util(r)
    o.refresh()

    sub = r.get_subreddit(SUBREDDIT)
    comments = sub.get_comments()

    for comment in comments:
        trigger_found = get_app_name(comment.body)
        if (trigger_found and not str(comment.id) in REPLIED_COMMENTS):
            app_names = []
            for apps in trigger_found:
                if any("," in s for s in apps):
                    name = apps.split(",")
                    for app_split in name:
                        app_names.append(app_split.strip().lower())
                else:
                    app_names.append(apps.strip().lower())

            url = ""
            for name in app_names:
                url += get_url(name)
            if url:
                print("commenting...")
                done_id = post_comment(comment, url + BOT_BY)
                comment_id.append(str(done_id))
    if comment_id:
        replied_file(comment_id)
    print("Done!")

if __name__ == "__main__":
    main()

@@ -866,32 +866,39 @@
 s += %22%5B%7B0%7D%5D(%7B1%7D)
+ by %7B2%7D
 %5Cn%5Cn%22.format(%0A  
@@ -931,32 +931,57 @@
 string, univ_url
+, get_publisher(univ_url)
 )%0A              
@@ -1092,32 +1092,39 @@
 s += %22%5B%7B0%7D%5D(%7B1%7D)
+ by %7B2%7D
 %5Cn%5Cn%22.format(%0A  
@@ -1157,32 +1157,57 @@
 string, univ_url
+, get_publisher(univ_url)
 )%0A              
@@ -1355,16 +1355,23 @@
 0%7D%5D(%7B1%7D)
+ by %7B2%7D
 %5Cn%5Cn%22.fo
@@ -1420,16 +1420,41 @@
 univ_url
+, get_publisher(univ_url)
 )%0A      
@@ -1568,16 +1568,431 @@
 links%0A%0A%0A
+def get_publisher(app_url):%0A    heads = %7B%22User-Agent%22: %22Mozilla/5.0 (Windows NT 6.3; WOW64; rv:36.0)%22%0A             %22 Gecko/20100101 Firefox/36.0%22%7D%0A%0A    results = requests.get(app_url, headers=heads)%0A%0A    soup = BeautifulSoup(results.content, %22html.parser%22)%0A    publisher = soup.find(%22div%22,%0A                          %7B%22class%22: %22content m-b-n clamp-5%22%7D%0A                          ).text.strip()%0A    return publisher%0A%0A%0A
 def univ

Add Publisher details