Python Forum

Full Version: Word Search Solver
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello all,

First time here. So I've pieced together some code and can't quite figure out how to get it to work. It's not throwing any errors, but it isn't printing any results either. Basically the code reaches out, logs into a website, grabs a wordsearch grid, and is supposed to perform a word search for words 6 characters or longer. Any pointers?

#begin module imports
import re, os, sys, time, getpass
try:
  import requests
except:
  exit('[-] Importing Requests module failed')


#Begin with login to WordSearchWeb
class WordSearchWeb:

  loginUrl  = 'http://www.WordSearchWeb.net/login'
  challUrl  = 'http://www.WordSearchWeb.net/index.php'
  gridsUrl = 'http://www.WordSearchWeb.net/lettergrid/generate.php' 

  def __init__(self, username, password, s) : 
    self.login(username, password, s)
    self.sidebar(s)
    self.gridSRC = self.getgrid(s)
    self.searchgrid(self.gridSRC)
    print ' '.join(sorted(set(word for (word, path) in self.solve())))
    
  def login(self, username, password, s):
    r = s.get(self.loginUrl)
    r = s.post(self.loginUrl, data = {'username' : username, 'password' : password, 'login' : 'Login'})
    if 'Welcome back to WordSearchWeb' in r.text:
      print('[+] Login Successful')
      c = r.request.headers['Cookie']
      self.Cookie = c
      return s

  def sidebar(self, s):
    print('[+] Closing Side bar for faster page load')
    r = s.get('http://www.WordSearchWeb.net/index.php?mo=WordSearchWeb&me=Sidebar2&rightpanel=0')
  
  def getgrid(self, s):
    print('[+] Getting source code of generate.php')
    r = s.get(self.gridsUrl)
    gridSRC = r.content
    gridSRC = gridSRC.replace('<pre>','')
    gridSRC = gridSRC.replace('</pre>','')
    print gridSRC
    gridSRC = gridSRC.replace('\n',' ')
    gridSRC = str(gridSRC[1:len(gridSRC)-1])
    return gridSRC

  def searchgrid(self, grid):
    gridSRC=self.getgrid(s)
    self.grid = grid
    grid = gridSRC.split()
    print grid
    nrows, ncols = len(grid), len(grid[0])
    alphabet = ''.join(set(''.join(grid)))
    bogglable = re.compile('[' + alphabet + ']{6,}$', re.I).match

    self.words = set(word.rstrip('\n') for word in open('words') if bogglable(word))
    prefixes = set(word[:i] for word in self.words
                   for i in range(2, len(word)+1))

  def solve(self):
    grid = self.grid
    for y, row in enumerate(grid):
      for x, letter in enumerate(row):
        for result in self.extending(letter, ((x, y),)):
          yield result

  def extending(self, prefix, path):
    if prefix in self.words:
      yield (prefix, path)
      for (nx, ny) in neighbors(path[-1]):
        if (nx, ny) not in path:
          prefix1 = prefix + grid[ny][nx]
          if prefix1 in prefixes:
            for result in extending(prefix1, path + ((nx, ny),)):
              yield result

  def neighbors((x, y)):
    for nx in range(max(0, x-1), min(x+2, ncols)):
      for ny in range(max(0, y-1), min(y+2, nrows)):
        yield (nx, ny)


u = 'myuser'
p = 'mypass'
s = requests.Session()

WordSearchWeb(u,p,s)
Huh Huh Huh
I'm not big on web scripts, but if it's not printing anything, then it's not printing line 27. That would indicate a login error. If it is printing line 27, please be more clear about exactly what happens when you run the script. Also, I would assign the instance of WordSearchWeb you create to a variable name. The you can examine the attributes to make sure things are happening correctly at each step of the process.
Inside your login function, print out the response you get.  That's the first time you try printing anything to console, so logging in is probably failing.
(Oct-09-2017, 09:42 PM)nilamo Wrote: [ -> ]Inside your login function, print out the response you get.  That's the first time you try printing anything to console, so logging in is probably failing.

I should probably have been a little more clear in my post. This is what prints:

[Image: c27xeEW.png]

It get to the point where it gets the page contents and places each line into a list as an item, but the script is failing to solve the puzzle. I'm not sure that I'm passing the data between the functions properly.
Do you have a sample grid we can try it out with?  As is, it's not runnable (since we don't have a user/pass), which means we can only eyeball it without being able to run it.