import praw
import OAuth2Util
import requests
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:
                if tag.string.lower() == app_name.lower():
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, tag["href"])
                    break
                elif app_name.lower() in tag.string.lower():
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, tag["href"])
                    ctrl += 1
                    if ctrl == 3:
                        break
                else:
                    set_of_links += "[{0}]({1})\n\n".format(
                        tag.string, tag["href"])
                    ctrl += 1
                    if ctrl == 3:
                        break

    return set_of_links


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(str, ch):
    for i, ltr in enumerate(str):
        if ltr == ch:
            yield i


def main():

    app_names = set()
    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:
        if ("wpapp[" in comment.body and "]" in comment.body and
                not str(comment.id) in REPLIED_COMMENTS):
            start = list(get_app_name(comment.body, "["))
            end = list(get_app_name(comment.body, "]"))
            for i, j in zip(start, end):
                app_name = comment.body[i + 1:j]
                name_split = app_name.split(",")
                for i in name_split:
                    if i != "":
                        app_names.add(i.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))
            app_names.clear()
    if comment_id:
        replied_file(comment_id)
    print("Done!")

if __name__ == "__main__":
    main()

@@ -39,16 +39,26 @@
 equests%0A
+import re%0A
 from bs4
@@ -1662,127 +1662,157 @@
 (str
-, ch
+i
 ):%0A    
-for i, ltr in enumerat
+trigger = %22%5Cw*wpapp%5C%5B(%5B%5E%5D%5D*)%5C%5D%22%0A    exp = re.compil
 e(
-s
 tr
-):%0A        if ltr == ch:%0A            yield i%0A%0A%0Adef main():%0A%0A    app_names = set()
+igger)%0A    found = exp.findall(stri)%0A    if found:%0A        return found%0A%0A%0Adef main():%0A
 %0A   
@@ -2154,48 +2154,37 @@
     
-if (%22wpapp%5B%22 in comment.body and %22%5D%22 in 
+trigger_found = get_app_name(
 comm
@@ -2195,12 +2195,9 @@
 body
- and
+)
 %0A   
@@ -2201,23 +2201,37 @@
         
-       
+if (trigger_found and
  not str
@@ -2281,148 +2281,107 @@
     
-start = list(get_app_name(comment.body, %22%5B%22))%0A            end = list(get_app_name(comment.body, %22%5D%22))%0A            for i, j in zip(start, end
+app_names = %5B%5D%0A            for apps in trigger_found:%0A                if any(%22,%22 in s for s in apps
 ):%0A 
@@ -2399,163 +2399,168 @@
     
-app_
+    
 name = 
-comment.body%5Bi + 1:j%5D%0A                name_split = app_name.split(%22,%22)%0A                for i in name_split:%0A                    if i != %22%22:%0A    
+apps.split(%22,%22)%0A                    for app_split in name:%0A                        app_names.append(app_split.strip().lower())%0A                else:%0A
     
@@ -2590,12 +2590,18 @@
 es.a
-dd(i
+ppend(apps
 .str
@@ -2881,38 +2881,8 @@
 d))%0A
-            app_names.clear()%0A
     

Use regex to search trigger word